Languages
[Edit]
EN

React - style components using CSS modules

0 points
Created by:
Adnaan-Robin
724

In this article, we would like to show you how to style components using CSS modules in React.

1. Install sass

Edit

Inside your project open new terminal (Terminal -> New Terminal) and use the following command to install sass as a dev-dependency:

2. Create new .module.scss file

Edit

Create new .module.scss file for your component. Below you can see an example for Navbar component:

React - Create new .module.scss file

3. Go to the file and create some styles

Edit

In this example, we create a style for navContainer class.

React - Go to .module.scss file and create some styles

4. Import .module.scss file

Edit

The next step is to import .module.scss file in your .js file. The convention here is to import the file as styles.

React - Import .module.scss file

5. Use imported styles

Edit

Now, we can use use styles to assign style to our component. To do so, we need to use a selector for the class we created earlier (navContainer).

React - Use imported styles from .module.scss file

 

Alternative titles

  1. React - how to use SCSS/CSS modules
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join