As I’m coming from Vuejs background and starting with Nextjs now, I want to scope my css for each of my component like- Navbar instead of using it inside Globas.css, is there a way to do that?
I’m using tailwind css library alongside it.
I did try by making individual css files for my each component like Angular and import it inside my each component but it does not work out like as expected.
This is how my current folder structure looks like-
3
Answers
You can use CSS Modules or styled-components such as emotion to scope your css to components in React. I don’t think you’ll need to if you use tailwind because of the utility classes it serves.
You need to use CSS modules. Create
your-component.module.scss
orcss
file and import it likeSince you are coming from Vue, CSS-in-JS might suit you better. You have many options such as StyledComponents, Linaria, Emotion, etc.
You can use either use styled components or CSS modules for that.
Here’s how to use CSS modules to Navbar.jsx
Create
Navbar.module.css
file.In
Navbar.jsx
file import the CSS fileto use those classes in `Navbar.jsx’
The class variable will contain CSS classes as properties.
Lean more about CSS Modules