React CSS Styling
There are three ways to style in React:
- Inline Styling
- CSS Stylesheets
- CSS Modules
Inline Styling
To inline style an element, we can make a Javascript Object, like this:
In this first curly brace is to write javascript and second is to make a javascript object. We can also write it like:
Note: CSS property name must be camelCase. background-color
would be backgroundColor
CSS Stylesheets
You can save the whole CSS in a separate file with file extension .css
and import it in your application.
App.css
Here we are writing CSS, so we don't need to make JS Object or do it in camelCase.
Index.js
Just import it like this:
CSS Modules
In this you don't have to worry about name conflicts as it is component specific. The CSS is available only for the file in which it is imported.
Make a file with extension .module.css
, example: index.module.css
Make a new file index.module.css
and insert some CSS into it, like this:
Import it in component like this:
0 Comments