-
Vanilla CSS:
- We can use plain CSS to give styles to our app.
- We create an
index.cssfile and add all the CSS rules there.
-
SASS / SCSS:
- SASS stands for Syntactically Awesome StyleSheets.
- It is a stylesheet language that is compiled to CSS.
- Allows us to make use of Nested rules, variables, functions.
- SASS supports 2 syntaxes: SASS and SCSS.
- SCSS: Uses the file extension
.scss. It is a superset of CSS, using curly braces and semicolon syntax. - SASS (The Indented Syntax): Uses file extension
.sass. It uses indentation instead of curly braces, and line-end instead of semicolon.
SCSS SASS .button { padding: 3px 10px; .button font-size: 12px; padding: 3px 10px border-radius: 3px; font-size: 12px border: 1px solid #e1e4e8; border-radius: 3px } border: 1px solid #e1e4e8 -
Styled Components:
- It allows us to write styles just as we write React components.
- It removes the mapping between components and styles.
- Utilizes tagged template literals to style your components.
-
Using External CSS Libraries like Material UI, Tailwind CSS, Bootstrap, Ant Design, Chakra UI, etc.
- These libraries provide pre-styled components.
- Tailwind CSS is the most modern method used extensively to style web apps.
- Step 1: Create the project.
- Step 2: Install Tailwind CSS via npm and initialize it to generate
tailwind.config.js:npm install -D tailwindcss postcss npx tailwindcss init
- Step 3: Create a
.postcssrcfile at the root level of our app. This tells Parcel to use PostCSS to read and understand Tailwind CSS. - Step 4: Configure the
.postcssrcfile:{ "plugins": { "tailwindcss": {} } } - Step 5: Configure
tailwind.config.jsby filling thecontentattribute:module.exports = { content: ["./src/**/*.{html,js}"], theme: { extend: {}, }, plugins: [], };
- Step 6: Add Tailwind directives in
index.css:@tailwind base; @tailwind components; @tailwind utilities;
content: Specifies paths to HTML, JS components, and source files that contain Tailwind class names.theme: Defines project’s color palette, fonts, breakpoints, border radius values, etc.extend: Extends the default spacing scale. Used to add new values without removing defaults.plugins: Allows adding custom utilities and components using JavaScript instead of CSS.
.postcssrcis a configuration file for PostCSS.- It tells Parcel to use PostCSS for processing Tailwind CSS.
- Required at the root level of the project.
PROS:
- All styles are inside JS files, avoiding switching between JS and CSS files.
- Lightweight: Only used classes are included in the final CSS bundle.
- Can build complex UI with Tailwind.
- Responsive UI without writing media queries.
- Supports hover and focus states easily.
- Prevents redundancy, ensuring minimal CSS code.
CONS:
- Excessive use of utility classes makes code less readable.
- Learning curve: Tailwind has its own learning process before becoming productive.