Learn how to style your React components using standard CSS and modular techniques. We'll build a clean look for our movie-browser app.
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.
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.
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.
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.
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.
MovieCard.css to MovieCard.module.css.JSXimport 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.
It's time to style the layout we built previously:
Header.module.css file.#282c34) and white text.Header component using the className attribute and the imported styles object.className: Beginners often reflexively type class="header". React will log a warning in your console, and the styles simply won't apply.div { ... } in your CSS files. These are global and will affect every div in your entire application. Always scope your styles to specific classes.In this lesson, we moved from raw JSX to a styled UI. We learned that:
className is the required attribute for applying CSS classes in JSX.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.
Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.
Styling Components
Polishing the UI
Finalizing the Movie Browser
Review of Component Lifecycle
Review of State Management
Building a Modal Component
Introduction to PropTypes
Performance Optimization Basics
Handling Browser History
Working with LocalStorage
Building a Favorites List
Handling Media in React
Introduction to Testing
Debugging React Apps
Deployment Basics
Using External Libraries
Advanced