简体   繁体   中英

styled-component by default use !important?

I wrap existing component with styled-component but I find it annoying to have to declare !important to overwrite the existing style of that component, for example

import { Row, Col, Card, Typography } from 'antd'
const { Title } = Typography

const StyledTitle = styled(Title)`
  text-align: center;
  margin: 50px 0 20px 0 !important;
  font-weight: normal !important;
`

It's hard to see which property have to use important, I detect it via visual changes after I save the file, which is so annoying, what's the solution to this?

They recommend adding ampersands

const StyledTitle = styled(Title)`
  &&& {
      text-align: center;
      margin: 50px 0 20px 0;
      font-weight: normal;
  }
`

https://styled-components.com/docs/faqs#how-can-i-override-styles-with-higher-specificity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM