通常,当使用纯CSS时,我有一个样式表,其中包含:
html {
height: 100%;
}
body {
font-size: 14px;
}
在React项目中使用styled-components
时,如何设置html
或body
标记的样式?我是否为此维护了一个单独的CSS文件?
通常,当使用纯CSS时,我有一个样式表,其中包含:
html {
height: 100%;
}
body {
font-size: 14px;
}
在React项目中使用styled-components
时,如何设置html
或body
标记的样式?我是否为此维护了一个单独的CSS文件?
当然,您可以通过一个<link>
标记来维护一个单独的CSS文件,该文件包含在HTML中.
v4
:使用createGlobalStyle个样式化组件.
import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
body {
color: ${props => (props.whiteColor ? 'white' : 'black')};
}
`
<React.Fragment>
<GlobalStyle whiteColor />
<Navigation /> {/* example of other top-level stuff */}
</React.Fragment>
v4
:Styled components还导出一个injectGlobal
助手,以从JavaScript注入全局CSS:
import { injectGlobal } from 'styled-components';
injectGlobal`
html {
height: 100%;
width: 100%;
}
`
更多信息请参见API documentation!