Styling React Components
Recall: We know 2 ways to style components and elements so far.
Inline styling using
style
object passed into component asstyle={style}
Specific to component
But can't apply pseudo-selectors like
hover
or 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
Radium
class when you export component.For media queries, simply wrap your
App
return
statement withStyleRoot
component.
Setting Styles Dynamically
To set inline styles dynamically, you can just update the style
object!
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 react-scripts@2.0.0
and higher, CSS modules are supported. To make them work in prior versions, you need to manually configure the Webpack config:
npm run eject
to removereact-scripts
and give you full access to Webpack config (as opposed toreact-scripts
managing it for you).Update
webpack.config.dev.js
andwebpack.config.prod.js
files in newconfig
folder to turn on CSS modules.
Note: This works because you're telling Webpack how to bundle the CSS
In the
Component.js
file,import classes from "./Component.css"
to get access to aclasses
object.Go to the appropriate element in the
return
statement and addclassName={classes.className}
(whereclassName
is found in yourComponent.css
file).
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