styled-components: style in theme is overriding component style [duplicate]

狂风中的少年 提交于 2019-12-25 12:13:35

问题


Reproduction

Expected: palevioletred color of Title, and red of TitleWithoutColor (theme styles triggered)

Got: red everywhere

Why: The rule that leads to this is like: p.column { text-align: right; } can be overwritten by body p.column { text-align: left; }, cause it more specific

Question: how to write themes so that styles of components would have bigger priority?


回答1:


Reactjs

<div id = "random_component"></div>

Css

#random_component {
    color: #0000 !important ;
}

Sometimes reactjs styling is not recognized as more specific than the framework you may be using. This comes from the fact that most frameworks have their styling coded in Css. To fix this, use Css to override current framework by defining a className or an id. Also, you can add the "!important" command to make sure all other level of specificity are overruled.




回答2:


Ok, I think I found solution, problem was that

const globalStyles = styled.div`
p {
  color: ${props => props.theme.somecolor};
}
a {
  color: ${props => props.theme.somecolor};
}`

isnt right usage of styled-components, instead I have to define components and extend them

const P = styled.p`
  color: ${props => props.theme.somecolor};
`
const A = styled.A`
  color: ${props => props.theme.somecolor};
`
const CustomA = A.extend`
  color: mycolor;
`


来源:https://stackoverflow.com/questions/45628743/styled-components-style-in-theme-is-overriding-component-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!