React Reference ยท Props and Attributes
React style prop
Reference for the React style prop: pass an object with camelCase keys, understand automatic px units, use CSS variables, and know the limits of inline styles.
The style prop applies inline CSS to a DOM element. Unlike HTML, where style is a string, React expects an object: keys are camelCase CSS property names and values are strings or numbers. That is why you see double braces in JSX, style={{ color: 'red' }}: the outer pair is the JSX expression, the inner pair is the object.
Inline styles are the right tool for values that are computed at runtime, such as a progress bar's width, a dragged item's position or a color chosen by the user. For everything static, use className and a stylesheet.
<div style={{ backgroundColor: '#eee', padding: 12, borderRadius: 8 }}>Hello</div>
Try it
Syntax
<element style={{ propertyName: value, '--custom-property': value }} />
| Name | Description |
|---|---|
| Property keys | camelCase versions of CSS properties: backgroundColor, fontSize, borderTopWidth. Vendor prefixes are capitalized: WebkitTransition; the exception is msTransition. |
| Number values | Treated as pixels (12 becomes 12px), except for unitless properties such as opacity, lineHeight, zIndex, flex, flexGrow, fontWeight, order and aspectRatio. 0 is always fine. |
| String values | Passed as written. Use them for other units, keywords, functions and shorthands: '50%', '1rem', 'calc(100% - 8px)'. |
| Custom properties | Keys starting with -- are set verbatim and numbers are not given px. |
null, undefined, false, '' |
Property is left out. |
Returns: an element with an inline style attribute built from the object.
Errors: passing a string throws The style prop expects a mapping from style properties to values, not a string. A hyphenated key such as 'background-color' logs Unsupported style property background-color. Did you mean backgroundColor? in development.
Usage notes
- Create the object outside the component (or with
useMemo) when it never changes. A new object each render is harmless for correctness, but it defeatsmemoon children that receive it. - Inline styles cannot express pseudo-classes (
:hover), pseudo-elements, media queries or keyframes. Use CSS for those, and pass dynamic values in through custom properties as in the first example. !importantdoes not work in a style object value. Fix specificity in your stylesheet instead.- Specificity. Inline styles beat normal rules in stylesheets. This makes them hard to override later, another reason to keep them for dynamic values only.
- Update only changed keys. React compares the old and new objects and changes only the properties that differ. A property you remove from the object is cleared from the element.
- Shorthands and longhands together (
borderandborderColor) can produce confusing results when only one of them changes. Prefer using one form consistently. - Do not mutate the style object between renders; create a new object. Mutating a shared object makes changes invisible to React and to memoization.
styleon SVG and custom elements works the same way. Presentational SVG attributes such asfillcan also be passed directly as props.- Security. Style values are set as properties, not parsed as HTML, so they cannot inject markup. A user-controlled URL in
backgroundImagecan still load an arbitrary image; validate it.
Related
className, data-* attributes, SVG elements,<progress>- Lessons: Styling, Animation