Styling React Components
Recall: We know 2 ways to style components and elements so far.
Inline styling using
styleobject passed into component asstyle={style}
Specific to component
But can't apply pseudo-selectors like
hoveror media queries
Stylesheet import using
import ./style.css"that gets compiled by Webpack
Styles will apply globally
Note: To add media queries and pseudo-selectors to inline styles, install the popular package Radium.
For pseudo-selectors, simply wrap your component inside
Radiumclass when you export component.For media queries, simply wrap your
Appreturnstatement withStyleRootcomponent.
Setting Styles Dynamically
To set inline styles dynamically, you can just update the style object!
render() {
const style = {
color: blue
};
if (this.state.isOff) style.color = "gray";
return <div style={style}>Hi!</div>;
};To set classes dynamically, just use an array with the join() method.
Scoping Styles with CSS Modules
CSS modules allow you to scope styles in a *.module.css file to a specific component, even if those styles would override other stylesheets (e.g. using same class name).
As of [email protected] and higher, CSS modules are supported. To make them work in prior versions, you need to manually configure the Webpack config:
npm run ejectto removereact-scriptsand give you full access to Webpack config (as opposed toreact-scriptsmanaging it for you).Update
webpack.config.dev.jsandwebpack.config.prod.jsfiles in newconfigfolder to turn on CSS modules.
Note: This works because you're telling Webpack how to bundle the CSS
In the
Component.jsfile,import classes from "./Component.css"to get access to aclassesobject.Go to the appropriate element in the
returnstatement and addclassName={classes.className}(whereclassNameis found in yourComponent.cssfile).
CSS modules scope your CSS because when Webpack bundles your files, it takes your class names and appends component names and unique hashes like [name]__[local]__[hash]. Then it exposes a JS object that maps your class names to their appended versions using key/value pairs of the form className: [name]__[local]__[hash]. You then use this object in your component.
Pro tip: To make a CSS class global, append :global .class { ... }.
Targeting child elements
A component always returns one element with child elements all inside. To target child elements, you can just use this CSS selector syntax: .Parent child. .Parent will be converted into a unique class name, so child will be uniquely targeted!
Note: You can also just give your child elements their own class.
Using pseudo-selectors
Simple. Just add the pseudo-selector to selector that includes a class. The class will be converted:
Using media queries
Simple again. Just make sure style rules inside your media query make use of classes.
Last updated