4.10 šŸŽØ Styling in React – From CSS to Styled Components

Hey there! šŸ‘‹ I’m Vikas Sankhla, a Full Stack Developer and founder of Web Codder.

Today, we’re going to learn how to make your React apps look beautiful using different styling methods – from plain CSS to powerful tools like styled-components.

I’ll explain everything like I’m teaching my younger cousin šŸ‘¦šŸ‘§ — super simple, super fun!


🧩 Why Styling Matters

Imagine building a cool website that does amazing stuff… but looks boring 😓.

That’s where styling comes in! It adds colors, spacing, fonts, animations — all the visual magic ✨.

In React, we have many ways to style components. Let’s explore each one step-by-step.


🧵 1. Styling with Traditional CSS

This is the classic way — just like in HTML!

šŸ”§ How it works

  1. Create a .css file
  2. Write your CSS styles
  3. Import the file into your component

āœ… Example

cssCopyEdit/* styles.css */
.title {
  color: blue;
  font-size: 24px;
  text-align: center;
}
jsxCopyEdit// App.js
import './styles.css';

function App() {
  return <h1 className="title">Hello World šŸŒ</h1>;
}

šŸ‘ Pros

  • Easy to use
  • Familiar to everyone

šŸ‘Ž Cons

  • All styles are global (can affect other components)

šŸ“¦ 2. CSS Modules – Safer, Scoped Styles

CSS Modules solve the global problem. They scope your CSS to just one component.

šŸ›  How it works

  • Create a .module.css file
  • Import it like a JavaScript object

āœ… Example

cssCopyEdit/* Button.module.css */
.btn {
  background-color: green;
  color: white;
  padding: 10px;
}
jsxCopyEditimport styles from './Button.module.css';

function Button() {
  return <button className={styles.btn}>Click Me šŸš€</button>;
}

šŸ‘ Pros

  • Styles are scoped to components
  • No class name conflicts

šŸŽØ 3. Styled-Components – CSS in JS

Now things get fancy! šŸ’…

With styled-components, you write CSS inside JavaScript using a special function.

šŸ“¦ Install First

bashCopyEditnpm install styled-components

āœ… Example

jsxCopyEditimport styled from 'styled-components';

const Title = styled.h1`
  color: tomato;
  font-size: 28px;
`;

function App() {
  return <Title>Styled with Style šŸ’…</Title>;
}

šŸ‘ Pros

  • Styles are scoped
  • You can use JavaScript logic inside styles
  • Great for theming and dynamic styles

šŸ‘Ž Cons

  • A little more complex
  • Slightly larger bundle size

🌈 4. Emotion – Another CSS-in-JS Library

emotion is similar to styled-components but offers more flexibility.

You can write styles in multiple ways:

  • Styled components
  • css prop
  • Object styles

It’s faster and highly customizable.

bashCopyEditnpm install @emotion/react @emotion/styled

Then use it just like styled-components.


šŸ“± Responsive Design in React

Your app should look great on all devices — phones, tablets, and desktops.

šŸ’” Best Tips:

  • Use media queries in your styles
  • Use flexbox and grid
  • Test on different screen sizes

🧪 Example

jsxCopyEditconst Box = styled.div`
  width: 100%;
  padding: 20px;
  background: lightblue;

  @media (max-width: 600px) {
    background: lightcoral;
  }
`;

🧱 Best Practices for CSS in React

Tip šŸ’”Why it’s helpful
Use CSS Modules or styled-componentsAvoid global CSS mess
Name classes clearlyUse BEM or camelCase
Avoid inline styles for large componentsHard to manage
Keep styles close to componentsEasier to maintain
Reuse common stylesDRY (Don’t Repeat Yourself)

šŸ”§ Comparison Table

MethodScopedEasyDynamic StylesTools Needed
Traditional CSSāŒāœ…āŒNone
CSS Modulesāœ…āœ…āŒBuilt-in
Styled-componentsāœ…āš ļøāœ…Yes
Emotionāœ…āš ļøāœ…Yes

šŸŽ Conclusion – Style It Your Way

There’s no ā€œone perfect wayā€ to style in React.
It depends on your project size, team, and personal preference.

Start with simple CSS for small apps.
Use CSS Modules or styled-components for bigger ones.


šŸ“¢ Stay Connected!

Loved learning this? There’s more fun stuff coming! šŸŽ‰

šŸ‘‰ Subscribe & follow us for daily React tips:

Let’s build beautiful apps together! šŸš€šŸ’»

Leave a Comment