Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 6 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20263 min read

Styling Components: A Guide to React CSS and Modular Design

Learn how to style your React components using standard CSS and modular techniques. We'll build a clean look for our movie-browser app.

ReactCSSStylingFrontendWeb Developmentjavascript

Previously in this course, we discussed Creating Static Components: Building Your First React UI Layout. Now that we have our basic UI structure in place, it’s time to move beyond raw HTML elements. This lesson adds visual polish to our movie-browser app by teaching you how to apply CSS, manage classes, and adopt modular styling practices that keep your codebase clean.

The Foundation of Component Styling

In traditional web development, you might be used to having one massive style.css file that grows uncontrollably as your project expands. In React, we shift our mindset to align with Introduction to Component-Based Architecture in React.

If a component is a self-contained unit of functionality, its styles should ideally live close to that component. By the end of this lesson, you will be able to map specific styles to specific components, making your UI predictable and easy to maintain.

Applying CSS to Components

React uses standard CSS, but there is one major syntax difference you must remember: The class attribute in HTML is className in JSX. Because class is a reserved keyword in JavaScript, React requires this camelCase alternative.

Step 1: Linking a Stylesheet

To start, create a file named MovieCard.css in your src/components folder.

CSS
#9CDCFE">color:#6A9955">/* src/components/MovieCard.css */
color:#4EC9B0">.movie-card {
  #9CDCFE">border: 1px solid #ddd;
  #9CDCFE">border-radius: 8px;
  #9CDCFE">padding: 16px;
  #9CDCFE">margin: 10px;
  #9CDCFE">width: 200px;
  #9CDCFE">box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

#9CDCFE">color:#4EC9B0">.movie-title {
  #9CDCFE">font-size: 1.2rem;
  #9CDCFE">color: #333;
}

Now, import this file directly into your MovieCard.jsx component file.

JSX
// src/components/MovieCard.jsx
import CE9178">'./MovieCard.css';

function MovieCard() {
  return (
    <div className="movie-card">
      <h2 className="movie-title">Inception</h2>
      <p>A mind-bending thriller.</p>
    </div>
  );
}

export default MovieCard;

When you import the CSS file this way, your bundler (Vite, which we set up in our earlier lesson on Setting Up with Vite) ensures that these styles are injected into the document head, applying them to the rendered output.

Modular Component Styling

While importing a CSS file works, it still puts styles into the global scope. If you have two components that both use the class .title, they will conflict. To solve this, we often use CSS Modules.

A CSS Module is a file ending in .module.css. When you import it, React treats the class names as unique identifiers.

  1. Rename MovieCard.css to MovieCard.module.css.
  2. Update your import and usage:
JSX
import styles from CE9178">'./MovieCard.module.css';

function MovieCard() {
  return (
    <div className={styles.movieCard}>
      <h2 className={styles.movieTitle}>Inception</h2>
    </div>
  );
}

By using styles.movieCard, the bundler automatically generates a unique class name like MovieCard_movieCard__a1b2c, preventing any style leakage across your application.

Hands-on Exercise

It's time to style the layout we built previously:

  1. Create a Header.module.css file.
  2. Add styles to make the header have a dark background (#282c34) and white text.
  3. Apply these styles to your Header component using the className attribute and the imported styles object.
  4. Verify the changes in your browser.

Common Pitfalls

  • Forgetting className: Beginners often reflexively type class="header". React will log a warning in your console, and the styles simply won't apply.
  • Global Style Pollution: Avoid writing selectors like div { ... } in your CSS files. These are global and will affect every div in your entire application. Always scope your styles to specific classes.
  • Layout Jank: If you notice elements jumping around as the page loads, ensure your CSS is bundled correctly. If you encounter performance issues due to complex animations, remember to check for Forced Synchronous Layout: How to Fix Reflow Bottlenecks.

Recap

In this lesson, we moved from raw JSX to a styled UI. We learned that:

  • CSS is linked via standard imports at the top of component files.
  • className is the required attribute for applying CSS classes in JSX.
  • CSS Modules provide a powerful way to avoid style collisions by scoping classes to individual components.

By organizing your styles alongside your logic, you're building a scalable architecture that will make your movie-browser app feel like a production-grade application.

Up next: We will begin making our components dynamic by learning how to pass data using props.

Previous lessonCreating Static ComponentsNext lesson Passing Data with Props
Back to Blog

Similar Posts

ReactReactJune 24, 20263 min read

Setting Up with Vite: A Professional React Development Environment

Learn how to initialize a professional React project using Vite. We’ll cover the project directory structure and how to launch your fast development environment.

Read more
ReactReactJune 25, 2026

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 6 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min
3 min read

Fetching Data from an API: A Practical React Guide

Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.

Read more
ReactReactJune 25, 20264 min read

Mastering useEffect Dependencies: Control Your React Lifecycle

Learn to master the useEffect dependency array to control exactly when your side effects run. Avoid infinite loops and optimize your React components today.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    3 min
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course