LearnContact
Lesson 2418 min read

Inline Styling

Learn how to style React elements directly with JavaScript style objects, and understand where inline styles help and where they fall short.

Introduction

So far you have styled components using separate CSS files, whether plain or scoped with CSS Modules. React also supports a third approach that skips CSS files entirely: passing a JavaScript object directly to an element's style prop.

Inline styles are not meant to replace CSS files for most of your styling — they are a targeted tool for one specific situation: values that need to be computed at runtime. This lesson covers exactly how the style prop works, its JavaScript-flavored syntax, and where it genuinely shines versus where a CSS file is still the better choice.

What You Will Learn
  • How to pass a JavaScript object to the style prop.
  • Why CSS properties are written in camelCase inside that object.
  • How to supply values as strings or as plain numbers.
  • When inline styles are the right tool, and when they are not.

The style Prop

Every DOM element that React renders accepts a style prop. Unlike plain HTML, where style="color: red;" is a string, React's style prop takes a JavaScript object whose keys are CSS property names and whose values are the corresponding CSS values.

A basic inline style object
function Alert() {
  return (
    <div style={{ backgroundColor: '#fee2e2', color: '#991b1b', padding: '12px' }}>
      Something went wrong.
    </div>
  );
}

export default Alert;

The double curly braces often confuse beginners: the outer {} is JSX syntax for embedding a JavaScript expression, and the inner {} is the actual object literal being passed as that expression's value.

Pulling the Object Out

To avoid the double-brace look and to keep JSX cleaner, it is common to define the style object as a separate variable and reference it: const alertStyle = { backgroundColor: '#fee2e2' }; then <div style={alertStyle}>.

camelCase Property Names

Regular CSS properties use hyphens, like background-color or font-size. Because hyphens are not valid in JavaScript object keys without quoting them, React style objects use the camelCase equivalent instead: backgroundColor, fontSize, and so on.

CSS PropertyJavaScript Key
background-colorbackgroundColor
font-sizefontSize
border-radiusborderRadius
margin-topmarginTop
text-aligntextAlign
camelCase in practice
const cardStyle = {
  backgroundColor: '#f9fafb',
  borderRadius: '8px',
  fontSize: '1rem',
  textAlign: 'center',
};

function Card() {
  return <div style={cardStyle}>Hello!</div>;
}
One Exception: CSS Variables

Custom CSS properties (variables) like --main-color keep their original hyphenated, lowercase form even inside a style object, since they are not standard CSS properties: style={{ '--main-color': 'blue' }}.

Values: Strings vs Numbers

Most style values are written as strings, exactly as you would write them in CSS: '16px', '1px solid #ccc', 'center'. For a handful of properties that represent a length, React is a bit more forgiving — you can pass a plain number, and React automatically appends "px" for you.

Numbers vs strings
const boxStyle = {
  width: 200,          // becomes '200px'
  padding: 16,          // becomes '16px'
  opacity: 0.8,         // stays 0.8 (no unit — unitless property)
  border: '1px solid #ccc', // must stay a string
};
Not Every Number Gets "px"

Unitless CSS properties like opacity, zIndex, and flex do not get "px" appended — React knows the difference and leaves them as plain numbers.

When Inline Styles Are Useful

Inline styles earn their place when a value genuinely depends on data available only at render time — something a static CSS file cannot express on its own.

Computed Widths

A progress bar or chart bar whose width is a percentage calculated from props or state.

Dynamic Colors

A color picked at runtime, such as a user's chosen theme color or a status-based background.

Calculated Positioning

Elements positioned based on runtime measurements, like a tooltip following a cursor.

One-Off Overrides

A tiny, single-use style tweak that does not justify creating a whole new CSS class.

Where Inline Styles Fall Short

Inline styles operate only on the single element they are attached to, using only the properties you explicitly set. Several common CSS features simply have no equivalent through the style prop.

Limitations to Know
  • No pseudo-classes: you cannot express :hover, :focus, or :active through a style object.
  • No pseudo-elements: things like ::before and ::after are unavailable.
  • No media queries: inline styles cannot respond to viewport size changes on their own.
  • No keyframe animations: @keyframes cannot be defined inline.
  • Harder to reuse: a style object only applies to the exact element it is passed to, unlike a shared CSS class.

Example: A Dynamic Progress Bar

A progress bar is a textbook case for inline styles — the width is a genuinely dynamic value that depends on a prop, so it cannot live in a static CSS file.

ProgressBar.jsx
function ProgressBar({ percent }) {
  const trackStyle = {
    backgroundColor: '#e5e7eb',
    borderRadius: '9999px',
    height: 10,
    width: '100%',
  };

  const fillStyle = {
    backgroundColor: '#4f46e5',
    borderRadius: '9999px',
    height: '100%',
    width: `${percent}%`,
  };

  return (
    <div style={trackStyle}>
      <div style={fillStyle} />
    </div>
  );
}

export default ProgressBar;
Usage: <ProgressBar percent={65} />
Renders a rounded track with a filled portion covering 65% of its width.

Notice the static parts (colors, border radius) could just as easily live in a CSS Module class, while only the dynamic width truly needs to be inline. Many real components mix both: a CSS class for the static look and a small inline style for the one computed value.

Common Mistakes

Avoid These Mistakes
  • Writing hyphenated CSS keys like 'background-color' inside a style object instead of the required camelCase backgroundColor.
  • Passing a CSS string like style="color: red;" the way you would in plain HTML — React requires an object, not a string.
  • Trying to write :hover or a media query inside a style object; neither is supported through inline styles.
  • Overusing inline styles for everything, which makes components harder to theme and reuse compared to shared CSS classes.

Best Practices

  • Reserve inline styles for values that are only known at runtime, like a computed width or dynamic color.
  • Keep static, reusable styling in a CSS file or CSS Module instead of inline objects.
  • Define larger style objects as named variables above the component rather than writing them directly in JSX.
  • Remember that numeric values for length properties get "px" automatically, but unitless properties like opacity do not.
  • Combine a CSS class for the static look with a small inline style for the one dynamic value when a component needs both.

Frequently Asked Questions

Can I use media queries with the style prop?

No. Inline styles apply directly to one element with no concept of viewport size. For responsive behavior, use a CSS file, CSS Module, or a utility framework instead.

Why does React use camelCase instead of matching CSS exactly?

Hyphenated property names are not valid unquoted JavaScript object keys, so React (following the DOM's own style object convention) uses camelCase equivalents instead.

Do inline styles override CSS file styles?

Generally yes. Inline styles have a very high specificity in the CSS cascade, so they will typically win over class-based styles unless those rules use !important.

Is it bad practice to use inline styles at all?

Not inherently. They are simply the wrong tool for large-scale, reusable styling. For genuinely dynamic, per-instance values, they are a perfectly reasonable choice.

Can I pass a number for every CSS property?

No, only for properties React recognizes as length-based, such as width or padding. Properties like color or border still require a string value.

Key Takeaways

  • The style prop accepts a JavaScript object, not a CSS string.
  • CSS property names become camelCase inside that object, like backgroundColor.
  • Numbers are allowed for length-based properties and get "px" appended automatically.
  • Inline styles are ideal for small, runtime-computed values like a dynamic width or color.
  • Inline styles cannot express pseudo-classes, pseudo-elements, media queries, or keyframe animations.

Summary

Inline styling gives you a direct, JavaScript-native way to style a single element, best reserved for values that genuinely depend on data available only at render time. For everything static and reusable, a CSS file or CSS Module remains the better tool.

Next, you will look at Bootstrap, a popular utility and component framework, and see how its ready-made classes can be applied directly inside your JSX.

Lesson 24 Completed
  • You can pass a JavaScript style object to any element.
  • You know CSS properties must be written in camelCase inside that object.
  • You understand when inline styles help and when they fall short.
  • You are ready to explore Bootstrap with React.
Next Lesson →

Bootstrap with React