To apply inline styles in React JSX, pass a JavaScript object to the style prop with camelCase property names, for example <p style={{ color: "red", fontSize: 18 }}>. The double curly braces are an expression brace pair containing an object literal, not special syntax. Inline styles suit small dynamic values, while most static styling belongs in CSS files or CSS Modules.
If you come from HTML, the style attribute was a string such as style="color: red". React replaces it with an object, which is safer and easier to compute from props and state. This guide covers the syntax, the rules for units and property names, and when to prefer a stylesheet.
In JSX, the style prop expects an object where keys are CSS properties and values are the style values.
The outer braces tell JSX that a JavaScript expression follows. The inner braces are the object literal. You can also define the object separately, which reads better once it grows:
CSS properties with hyphens become camelCase in the object. Custom property names are the exception.
| CSS property | JSX style key |
|---|---|
background-color | backgroundColor |
font-size | fontSize |
margin-top | marginTop |
border-radius | borderRadius |
z-index | zIndex |
--accent | "--accent" (quoted string) |
Vendor prefixes follow the same idea, with a capital first letter, for example WebkitLineClamp. Today most prefixes are unnecessary, so check whether you need one.
String values are written exactly as in CSS and must be quoted. Numbers are accepted for many properties, and React adds px for properties that take lengths.
React does not append px to properties that are unitless in CSS, such as opacity, zIndex, flexGrow, lineHeight and fontWeight. For any other unit, such as rem, %, vh or em, use a string.
The main strength of inline styles is computing values at render time.
Every click changes percent, React re-renders, and the bar width updates. A value that changes constantly, such as a width or a colour from data, is exactly where an inline style is the right tool.
Inline styles are applied directly to one element, so they have real limits:
:hover, :focus or ::before.transition works as a normal property.For hover effects, responsive layouts and shared themes, use CSS files, CSS Modules, or a utility framework, and attach them through className.
| Need | Better choice |
|---|---|
| A value computed from props or state | Inline style |
| Hover, focus, media queries | CSS with className |
| Reusable design tokens and themes | CSS or CSS variables |
| One-off prototype | Inline style |
| Large shared component library | CSS Modules or utility classes |
A good hybrid is to set a CSS custom property inline and read it in the stylesheet:
This keeps the layout in CSS while the data-driven part stays in JSX. In TypeScript you may need a cast such as as React.CSSProperties for custom property keys.
style="color: red" is invalid in JSX and throws an error saying the style prop expects an object.{ font-size: 16 } is a syntax error; use fontSize.{ color: red } refers to a variable named red."2rem", not 2.React.memo. Move constant objects outside the component.The outer pair marks a JavaScript expression inside JSX, and the inner pair is the object literal. Nothing else is special about it.
No. Pseudo-classes need a stylesheet. Use a CSS class and toggle it, or use CSS rules with :hover.
For numeric values on length properties, yes. Unitless properties such as opacity and lineHeight are left alone, and any other unit must be a string.
Not inherently. They are fine for dynamic values, but static and shared styling is easier to maintain in CSS.
Pass an object to style, write property names in camelCase, quote string values and use numbers for pixel lengths or unitless values. Reach for inline styles when a value comes from props or state, and use CSS with className for hover states, media queries and shared design.