Lesson 15 of 60 – JSX Styling
25%

JSX Styling

Styling is an important part of building React interfaces. JSX allows you to apply CSS classes, inline styles, CSS modules, and dynamic styles to React elements.

Note: JSX uses className instead of class. Inline styles are written using JavaScript objects, and CSS files can be imported into React components.

1. What is JSX Styling?

JSX styling means applying CSS styles to elements written inside JSX.

function App() {

    return (
        <h1 className="title">
            Hello React
        </h1>
    );

}

The title class can be defined in a CSS file.

2. Using className

The most common way to apply a CSS class in JSX is with className.

<div className="container">
    <h1 className="heading">
        React Tutorial
    </h1>
</div>

3. Why className Instead of class?

JSX follows JavaScript property naming conventions. Therefore, React commonly uses className for CSS classes.

// JSX

<div className="card">
    Content
</div>

Do not normally write class as you would in regular HTML.

4. Creating a CSS File

Create a CSS file inside the src folder.

src/
 |
 +-- App.jsx
 +-- App.css
 +-- main.jsx

You can write CSS rules inside App.css.

5. Writing CSS Rules

.title {
    color: blue;
    font-size: 32px;
    text-align: center;
}

This CSS rule styles elements having the title class.

6. Importing a CSS File

A CSS file can be imported into a React component.

import "./App.css";

function App() {

    return (
        <h1 className="title">
            Hello React
        </h1>
    );

}

export default App;

7. Multiple CSS Classes

Multiple CSS classes can be assigned using a space-separated string.

<div className="card shadow border">
    Student Information
</div>

The element receives all three classes.

8. Inline Styling in JSX

React supports inline styles using a JavaScript object.

function App() {

    return (
        <h1
            style={{
                color: "blue",
                fontSize: "30px"
            }}
        >
            React
        </h1>
    );

}

9. Style Object

Instead of writing the style object directly, you can store it in a variable.

const headingStyle = {
    color: "green",
    fontSize: "32px"
};

return (
    <h1 style={headingStyle}>
        React Tutorial
    </h1>
);

10. CSS Property Names in Style Objects

CSS property names containing hyphens are generally written using camelCase in JavaScript style objects.

const style = {
    backgroundColor: "lightblue",
    fontSize: "24px",
    marginTop: "20px"
};

11. color Property

const style = {
    color: "red"
};

return (
    <p style={style}>
        Red Text
    </p>
);

12. backgroundColor Property

const style = {
    backgroundColor: "yellow"
};

return (
    <div style={style}>
        Highlighted Content
    </div>
);

Notice that CSS background-color becomes backgroundColor in the JavaScript style object.

13. fontSize Property

const style = {
    fontSize: "28px"
};

return (
    <h2 style={style}>
        Large Heading
    </h2>
);

14. Multiple Inline Styles

const style = {
    color: "white",
    backgroundColor: "black",
    padding: "20px",
    textAlign: "center"
};

return (
    <div style={style}>
        React Course
    </div>
);

15. Numeric Style Values

Some numeric style values can be provided as numbers and React will interpret them as pixel values for properties where appropriate.

const style = {
    fontSize: 24,
    marginTop: 20
};

return (
    <h2 style={style}>
        React
    </h2>
);

16. Percentage and Other Units

When a CSS value needs a specific unit such as percentage, rem, or em, the unit can be included in a string.

const style = {
    width: "50%",
    padding: "2rem",
    fontSize: "1.2rem"
};

17. Dynamic Styling

Styles can be changed according to JavaScript values.

const isActive = true;

const style = {
    color: isActive ? "green" : "gray"
};

return (
    <p style={style}>
        Student Status
    </p>
);

18. Dynamic className

A CSS class can also be selected dynamically.

const isActive = true;

return (
    <div className={isActive ? "active" : "inactive"}>
        Student
    </div>
);

19. Conditional Styling

Conditional expressions can determine which CSS class is applied.

const score = 80;

return (
    <p
        className={
            score >= 40
                ? "pass"
                : "fail"
        }
    >
        Result
    </p>
);

20. Combining Classes Dynamically

Template literals can be used to combine static and dynamic classes.

const active = true;

return (
    <div
        className={`card ${active ? "active" : ""}`}
    >
        Student Card
    </div>
);

21. CSS Modules

CSS Modules allow styles to be associated with individual components. A common file name is Component.module.css.

src/
 |
 +-- Button.jsx
 +-- Button.module.css

22. Using a CSS Module

A CSS Module can be imported into a component.

import styles from "./Button.module.css";

function Button() {

    return (
        <button className={styles.button}>
            Click Me
        </button>
    );

}

export default Button;

23. CSS Module Example

The CSS Module file can contain a normal CSS class.

/* Button.module.css */

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

The class can then be accessed through the imported styles object.

24. Styling Child Elements

CSS can style nested elements in the normal way when using a regular CSS file.

.card {
    padding: 20px;
    border: 1px solid #ddd;
}

.card h2 {
    margin-bottom: 10px;
}

.card p {
    color: gray;
}

25. Hover Styling

Pseudo-classes such as :hover are normally handled in a CSS stylesheet rather than directly through a simple inline style object.

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

.button:hover {
    background-color: darkblue;
}

26. Styling a React Card

function StudentCard() {

    return (
        <div className="student-card">

            <h2>Rahul Kumar</h2>

            <p>
                Course: React
            </p>

            <button className="button">
                View Profile
            </button>

        </div>
    );

}
.student-card {
    padding: 20px;
    border: 1px solid #ddd;
    border-radius: 10px;
}

.button {
    padding: 10px 20px;
    cursor: pointer;
}

27. Choosing a Styling Method

Different styling methods are useful in different situations.

Regular CSS
    ↓
Reusable global or shared styles

Inline Styles
    ↓
Small dynamic styles

CSS Modules
    ↓
Component-scoped styles

UI Libraries
    ↓
Ready-made components and utilities

28. Styling Best Practices

  • Use meaningful class names.
  • Keep reusable styles in CSS files.
  • Use inline styles when dynamic values are needed.
  • Use CSS Modules when component-level style isolation is useful.
  • Avoid putting large amounts of CSS directly inside JSX.
  • Keep styling consistent across components.
  • Use responsive CSS for different screen sizes.
  • Keep component structure and styling easy to understand.

29. Complete Styling Example

import "./App.css";

function App() {

    const isActive = true;

    return (
        <div className="student-card">

            <h1 className="title">
                Rahul Kumar
            </h1>

            <p
                className={
                    isActive
                        ? "active"
                        : "inactive"
                }
            >
                {isActive
                    ? "Active Student"
                    : "Inactive Student"}
            </p>

            <button className="button">
                View Profile
            </button>

        </div>
    );

}

export default App;
.student-card {
    width: 300px;
    padding: 20px;
    border: 1px solid #ddd;
    border-radius: 10px;
}

.title {
    margin-bottom: 10px;
}

.active {
    color: green;
}

.inactive {
    color: gray;
}

.button {
    padding: 10px 20px;
    border: none;
    cursor: pointer;
}

30. Complete JSX Styling Example with Inline Style

function App() {

    const isActive = true;

    const cardStyle = {
        padding: "20px",
        border: "1px solid #ddd",
        borderRadius: "10px",
        width: "300px"
    };

    const titleStyle = {
        color: "blue",
        fontSize: "26px"
    };

    return (
        <div style={cardStyle}>

            <h1 style={titleStyle}>
                React Student
            </h1>

            <p
                style={{
                    color: isActive
                        ? "green"
                        : "gray"
                }}
            >
                {isActive
                    ? "Active Student"
                    : "Inactive Student"}
            </p>

            <button
                style={{
                    backgroundColor: "black",
                    color: "white",
                    padding: "10px 20px",
                    border: "none"
                }}
            >
                View Profile
            </button>

        </div>
    );

}

export default App;

This example combines JSX styling, JavaScript objects, dynamic styles, conditional expressions, and inline CSS.

📌 Key Points

  • Use className to apply CSS classes in JSX.
  • Use CSS files for reusable styles.
  • Import CSS files into React components.
  • Inline styles are written using JavaScript objects.
  • CSS property names in style objects generally use camelCase.
  • Dynamic values can be used inside style objects.
  • CSS classes can also be selected dynamically.
  • Conditional expressions can control styling.
  • CSS Modules can provide component-scoped styles.
  • Use regular CSS for pseudo-classes such as :hover.
  • Choose the styling approach according to the project's needs.
  • Keep styling organized and reusable.

🧠 Quick Quiz

Question: Which attribute is used to apply a CSS class to a JSX element?