Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


← Back to Lessons

Introduction to Styling in React

Styling Approaches in React

Styling is crucial for React applications. A well-designed UI enhances user experience and makes your application stand out. Here's a basic example of styling a React component:

1import React from 'react'; 2import './styles.css'; 3 4function Button() { 5 return <button className="primary-button">Click me!</button>; 6} 7 8export default Button;

React offers several ways to style your components, each with distinct advantages for different scenarios.

Styling Approaches in React

1. CSS Stylesheets

The most traditional approach using separate CSS files:

1// MyComponent.jsx 2import React from 'react'; 3import './MyComponent.css'; 4 5function MyComponent() { 6 return <div className="component-container">Hello, styled world!</div>; 7}

Pros: Familiar approach, separation of concerns Cons: Global scope can lead to style conflicts

2. Inline Styling

Apply styles directly in JSX using JavaScript objects:

1function MyComponent() { 2 const styles = { 3 container: { 4 padding: '20px', 5 backgroundColor: '#f5f5f5', 6 borderRadius: '8px' 7 } 8 }; 9 10 return <div style={styles.container}>Hello, styled world!</div>; 11}

Pros: Styles are scoped to the component, can be dynamic Cons: No support for pseudo-classes, can become verbose

3. CSS Modules

CSS Modules create locally scoped class names to prevent conflicts:

1import styles from './MyComponent.module.css'; 2 3function MyComponent() { 4 return <div className={styles.container}>Hello, styled world!</div>; 5}

Pros: Local scoping prevents style collisions, uses standard CSS Cons: Additional setup in some environments

4. CSS-in-JS

Libraries like styled-components allow writing CSS directly in JavaScript:

1import styled from 'styled-components'; 2 3const Container = styled.div` 4 padding: 20px; 5 background-color: #f5f5f5; 6 border-radius: 8px; 7 8 &:hover { 9 background-color: #e0e0e0; 10 } 11`; 12 13function MyComponent() { 14 return <Container>Hello, styled world!</Container>; 15}

Pros: Component-scoped styles, dynamic styling based on props Cons: Runtime overhead, additional dependency

5. Utility-First CSS

Tailwind CSS provides utility classes to apply styles directly in JSX:

1function MyComponent() { 2 return ( 3 <div className="p-5 bg-gray-100 rounded-lg hover:bg-gray-200"> 4 Hello, styled world! 5 </div> 6 ); 7}

Pros: Rapid development, consistent design system Cons: HTML can become verbose, requires configuration

Choosing the Right Approach

Consider these factors when selecting a styling method:

  • Project size and complexity
  • Team preferences and expertise
  • Performance requirements
  • Design system needs

Modern React applications often combine approaches. For example, you might use utility classes for layout, component libraries for complex UI elements, and CSS Modules for custom components.

Conclusion

React provides flexible styling options that let you choose what works best for your project. No single approach is universally superior—each has strengths for different scenarios. Consider your project requirements and team preferences when making your decision.