Writing Css in react
whats the best way to write css in react i mean i am writing css for all components in one single style sheet i dont think its a good idea to do that i tried using different style sheets for different components but i notised that stylesheet of parent component also effects the child component what should i do
7 Replies
Use CSS Modules to scope the styles. https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/
Adding a CSS Modules Stylesheet | Create React App
Note: this feature is available with [email protected] and higher.
<div className={NavbarCss.leftUpperNav "flex" "center"}>
how do i use css from index.css now?
do i also now have to import it?
import IndexCss from "./css/index.module.css";????
It has to be imported, but the name has to match the file name. Note that having "module" in the name makes it a module and not having it in the name makes it not a module.
how do i use css from index.css which is not a module? cant i use it like i used to do before??
You can just import it, but use its proper name. Don't put "module" in the name as you did in your earlier question.
in the name of the file?
in tutorial i watched they said i have to write module in it
You have to write that if you want it to be a module. I thought you said you didn't want it to be a module.