Course contentsShow
← React
Lesson 1894 of 2,39644. Styling ApproachesFree lesson

When Inline Styles Make Sense

Understanding the rare legitimate use cases for inline styles: dynamic values from state, runtime calculations, and component-specific overrides.

When Inline Styles Make Sense

You'll learn the rare but valid scenarios where inline styles are actually the right tool for the job in React.

The Core Idea

Inline styles get a bad reputation—and usually for good reason. They can't handle pseudo-selectors, media queries, or proper cascading. But there are three specific scenarios where inline styles are not just acceptable, but optimal: when styles depend on dynamic state values, when they require runtime calculations, or when you need component-instance-specific overrides that don't belong in global CSS.

Valid Use Cases

1. Dynamic Values from State

When a style value comes directly from component state and changes frequently:

const [opacity, setOpacity] = useState(1);
return <div style={{ opacity }}>Fading...</div>;

2. Runtime Calculations

When dimensions or positions must be computed on-the-fly:

const width = useMemo(() => data.percentage * 100, [data]);
return <div style={{ width: `${width}%` }}>Progress</div>;

3. Component-Specific Overrides

When a reusable component accepts custom styling props:

<Card backgroundColor={user.themeColor} />

The Anti-Pattern to Avoid

Don't use inline styles for static values that could live in CSS modules or styled-components. style={{ padding: '20px', color: 'blue' }} for unchanging values is wasteful—it creates new objects on every render and bypasses CSS optimization.

Key Takeaway: Inline styles shine when values are truly dynamic—driven by state, props, or calculations. For everything else, reach for proper styling solutions like CSS modules or CSS-in-JS libraries.