Skip to main content
React Introduction
CHAPTER 15 Beginner

React Styling Techniques

Updated: May 13, 2026
20 min read

# React Styling Techniques

1. Introduction

React handles your HTML markup (via JSX) and your logic (via JavaScript/State). But what about CSS? How do you make your components look beautiful? React is unopinionated when it comes to styling, meaning there is no single "right way" to do it. In this chapter, we will explore the most common foundational techniques for styling React components.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Apply standard external CSS stylesheets to React.
  • Use Inline Styles with JavaScript objects.
  • Dynamically change styles based on React state.
  • Understand the pros and cons of different styling approaches.

3. Beginner-Friendly Explanations

Approach 1: Traditional CSS Files

This is the easiest transition from standard web development. You write a .css file and import it directly into your JavaScript file. Any classes defined in that CSS file can be applied using the className attribute.

Approach 2: Inline Styles

In HTML, inline styles are strings: style="color: red; font-size: 14px;". In React, inline styles are JavaScript Objects. CSS properties with hyphens are converted to camelCase. Example: font-size becomes fontSize.

4. Syntax Explanation

Using External CSS

styles.css
css
123456
.card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  padding: 20px;
}

App.jsx ```jsx id="ch15_ex1" import './styles.css'; // Import the CSS file!

function App() { // Use className instead of class return <div className="card">Hello World</div>; }

123
### Using Inline Styles
Because inline styles are objects, you need two sets of curly braces. The outer braces tell JSX "I am injecting JavaScript". The inner braces are the actual object literal.

jsx id="ch15_ex2" function InlineDemo() { return ( <div style={{ backgroundColor: 'blue', color: 'white', padding: '10px' }}> I am styled inline! </div> ); }

12345
*Note: Numbers are usually interpreted as pixels by React (e.g., `padding: 10` becomes `10px`).*

### Dynamic Styling with State
Because inline styles are just JavaScript, you can easily tie them to React State!

jsx id="ch15_ex3" import { useState } from 'react';

function DynamicStyle() { const [isActive, setIsActive] = useState(false);

// Define an object based on state const btnStyle = { backgroundColor: isActive ? 'green' : 'gray', color: 'white', padding: '10px 20px', border: 'none', cursor: 'pointer' };

return ( <button style={btnStyle} onClick={() => setIsActive(!isActive)}> Toggle Color </button> ); }

123456789101112131415161718192021222324
## 5. Output Explanations
In `DynamicStyle`, clicking the button flips `isActive`. React recalculates the `btnStyle` object and updates the DOM, instantly changing the button's background color. This is powerful because it bridges the gap between logic and visuals.

## 6. Real-World Examples
- **External CSS:** Used for global styling (e.g., reset CSS, typography, standard grid layouts).
- **Inline Styles:** Used for highly dynamic visual elements that change frequently (e.g., a progress bar where `width: ${progress}%` is tied directly to state).

## 7. Common Mistakes
- **Using `class` instead of `className`:** Causes console warnings.
- **Stringifying inline styles:** `<div style="color: red">` will crash React. You must use an object: `<div style={{color: 'red'}}>`.
- **Forgetting camelCase:** Writing `{ background-color: 'red' }` is invalid JavaScript syntax. It must be `backgroundColor`.

## 8. Best Practices
- **Avoid heavy use of inline styles.** They are hard to read, lack features like pseudo-classes (`:hover`), and don't scale well. Use them only for highly dynamic values.
- **Use Template Literals for dynamic classes:** Instead of inline styling, you can apply classes dynamically: `className={"btn " + (isActive ? "btn-active" : "btn-inactive")}`.

## 9. Exercises
1. Create a React component that imports an external CSS file to style an `<h1>`.
2. Add an inline style to a paragraph tag setting the `fontSize` to `24px` and `fontWeight` to `'bold'`.

## 10. Mini Project: Styled Dashboard Cards
Let's build a component that uses template literals to apply dynamic CSS classes based on props.

**Card.css**

css .dash-card { padding: 24px; border-radius: 12px; color: white; margin-bottom: 16px; font-family: sans-serif; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }

.theme-primary { background-color: #4f46e5; } .theme-success { background-color: #10b981; } .theme-danger { background-color: #ef4444; }

1
**Dashboard.jsx**

jsx id="ch15_proj1" // Example React component import React from 'react'; import './Card.css'; // Assuming the CSS is in the same folder

const StatCard = ({ title, value, theme }) => { // Dynamically combining the base class with the theme class const classNames = dash-card theme-${theme};

return ( <div className={classNames}> <h3 style={{ margin: 0, opacity: 0.8, fontSize: '14px', textTransform: 'uppercase' }}> {title} </h3> <h2 style={{ margin: '10px 0 0 0', fontSize: '32px' }}> {value} </h2> </div> ); };

export default function Dashboard() { return ( <div style={{ padding: '40px', maxWidth: '800px', margin: '0 auto', display: 'flex', gap: '20px' }}> <StatCard title="Total Users" value="14,231" theme="primary" /> <StatCard title="Revenue" value="$42,500" theme="success" /> <StatCard title="Errors" value="3" theme="danger" /> </div> ); } ``

11. Coding Challenges

Challenge 1: Build a simple progress bar component. It should take a
progress prop (number 0-100) and use an inline style to set the width of a colored div to {progress}%.

12. MCQs with Answers

Q1: How do you format the CSS
margin-top property when using inline styles in React? A) margin-top B) MarginTop C) marginTop D) margin: top *Answer: C*

Q2: Why must inline styles be wrapped in double curly braces {{}}? A) To increase specificity. B) The outer braces evaluate JavaScript, and the inner braces represent a JavaScript Object. C) It is a requirement of Babel. *Answer: B*

13. Interview Questions

  • Q: What are the drawbacks of using Inline Styles in React? *(Answer: No support for media queries, pseudo-classes like :hover, and poorer performance).*
  • Q: How do you dynamically apply a class name based on state?

14. FAQs

Are there better ways to style React? Yes. While external CSS and inline styles are the foundation, the industry has largely moved to more advanced solutions like CSS Modules, Styled Components, and TailwindCSS. We will cover these next!

15. Summary

Styling in React requires a slight paradigm shift. While you can still use external CSS stylesheets using
className, React's component architecture allows you to easily bind visual styles to your application's state using inline JavaScript objects and dynamic class name concatenation.

16. Next Chapter Recommendation

Standard CSS can lead to class naming conflicts (e.g., two components both using
.button`). In Chapter 16: React CSS Modules and TailwindCSS, we will explore the modern, enterprise-grade solutions to styling React applications.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·