⚛️ React CSS Styling

Chapter 11 of 15

Styling React Components

There are many ways to style React components. Each method has its own advantages and use cases.

💡 Multiple Options: React doesn't enforce a specific styling method. You can choose what works best for your project!

1. Inline Styling

Apply styles directly to elements using the style attribute:

const Header = () => {
  return (
    <h1 style={{color: "red", fontSize: "24px"}}>
      Hello Style!
    </h1>
  );
}
⚠️ Note: Style property names use camelCase (fontSize, not font-size) and values are strings.

Style Object

You can also create a style object:

const Header = () => {
  const myStyle = {
    color: "white",
    backgroundColor: "DodgerBlue",
    padding: "10px",
    fontFamily: "Sans-Serif"
  };
  
  return (
    <h1 style={myStyle}>Hello Style!</h1>
  );
}

2. CSS Stylesheet

Write CSS in a separate file and import it:

App.css

.header {
  background-color: #282c34;
  color: white;
  padding: 40px;
  text-align: center;
}

.button {
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
}

App.jsx

import './App.css'

const App = () => {
  return (
    <div className="header">
      <h1>Hello Style!</h1>
      <button className="button">Click Me</button>
    </div>
  )
}

export default App

3. CSS Modules

CSS Modules scope styles to specific components, preventing naming conflicts:

my-style.module.css

.bigblue {
  color: DodgerBlue;
  padding: 40px;
  font-family: Sans-Serif;
  text-align: center;
}

.button {
  background-color: green;
  color: white;
}

Car.jsx

import styles from './my-style.module.css'

const Car = () => {
  return (
    <div>
      <h1 className={styles.bigblue}>Hello Car!</h1>
      <button className={styles.button}>Click</button>
    </div>
  )
}

export default Car
✨ Advantage: CSS Modules automatically generate unique class names, preventing style conflicts between components!

4. Styled Components (CSS-in-JS)

Write CSS directly in your JavaScript files using tagged template literals:

Installation

npm install styled-components

Usage

import styled from 'styled-components';

// Create a styled component
const Button = styled.button`
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  
  &:hover {
    background-color: darkblue;
  }
`;

const Container = styled.div`
  display: flex;
  justify-content: center;
  padding: 20px;
`;

// Use the styled components
const App = () => {
  return (
    <Container>
      <Button>Click Me</Button>
    </Container>
  );
}

Props in Styled Components

const Button = styled.button`
  background-color: ${props => props.primary ? "blue" : "gray"};
  color: white;
  padding: 10px 20px;
`;

// Usage
<Button primary>Primary Button</Button>
<Button>Secondary Button</Button>

5. Sass/SCSS

Use Sass for more powerful CSS with variables, nesting, and mixins:

Installation

npm install sass

styles.scss

$primary-color: #667eea;
$secondary-color: #764ba2;

.header {
  background: linear-gradient(135deg, $primary-color 0%, $secondary-color 100%);
  color: white;
  padding: 20px;
  
  h1 {
    font-size: 2em;
    margin: 0;
  }
  
  .button {
    background-color: $primary-color;
    
    &:hover {
      background-color: darken($primary-color, 10%);
    }
  }
}

Usage

import './styles.scss';

const App = () => {
  return (
    <div className="header">
      <h1>Hello!</h1>
      <button className="button">Click</button>
    </div>
  );
}

6. Tailwind CSS

Use utility-first CSS classes directly in your JSX:

const App = () => {
  return (
    <div className="bg-blue-500 text-white p-4 rounded-lg">
      <h1 className="text-2xl font-bold mb-4">Hello!</h1>
      <button className="bg-green-500 hover:bg-green-700 px-4 py-2 rounded">
        Click Me
      </button>
    </div>
  );
}

Comparison

Method Pros Cons
Inline Quick, dynamic No pseudo-classes, verbose
CSS Files Familiar, full CSS features Global scope, name conflicts
CSS Modules Scoped, no conflicts Extra syntax
Styled Components Dynamic, props-based Learning curve, bundle size
Sass/SCSS Powerful features Build step required
Tailwind Fast, consistent Learning curve, HTML clutter
💡 Which to Choose?
  • Inline: Quick prototypes, dynamic styles
  • CSS Files: Traditional projects, simple apps
  • CSS Modules: Medium to large projects
  • Styled Components: Component libraries, dynamic themes
  • Sass: Complex styling needs
  • Tailwind: Rapid development, design systems

What's Next?

Now that you know how to style components, you're ready to learn about the useEffect Hook for handling side effects!