React Reference ยท Built-in DOM Components
The React style element
Reference for the React style element: render a style tag in a component, hoist it to the head with href and precedence, and learn how React deduplicates it.
A <style> element holds CSS text. Used plainly, React treats it like any element: it appears exactly where you render it, with its text as the children. That works, but it has problems: the CSS lives in the middle of the body, and ten copies of a component render ten identical style tags.
React 19 adds a managed mode. When a <style> has both a string href and a string precedence, React moves it into the <head>, groups it with other stylesheets of the same precedence, and renders it once per href however many components ask for it. The href here is not a URL to load; it is a unique key naming this block of CSS. This is the tool for component-scoped CSS that ships with the component.
Do not confuse this element with the style prop (<div style={{ color: 'red' }}>), which takes an object of inline declarations. See Props and attributes.
function Card({ children }) { return ( <> <style href="card-styles" precedence="default"> {'.card { border: 1px solid #ccc; padding: 12px; }'} </style> <div className="card">{children}</div> </> ); }
Try it
Syntax
<style href="unique-key" precedence="default">{cssText}</style> <style>{cssText}</style>
| Name | Description |
|---|---|
children |
The CSS text, as a single string. Build it with {'...'} or a template literal; JSX would otherwise parse the braces of CSS as expressions. |
href |
A unique name for this CSS block. Required for managed mode. Two styles with the same href are the same resource. |
precedence |
A string naming the group (default, base, theme). Required for managed mode. Groups are ordered by first use. |
media, nonce, title, blocking |
Same as in HTML. nonce is needed with a strict Content Security Policy. |
Returns: an HTMLStyleElement. In managed mode React adds data-href and data-precedence attributes to it in the head.
Edge cases: the first style rendered for an href wins. A second one with the same href is not added again. Without href and precedence, the tag is plain and stays in place.
Usage notes
- Put the CSS in a string.
<style>.a { color: red }</style>fails to parse in JSX because of the braces. Always use{'...'}or a template literal. - Pick
hrefcarefully. Name it after the thing it styles (card-styles). If two different components use the samehrefby accident, one of them loses its CSS. - Order inside a precedence group. Rules in the same group are in the order the styles were first rendered. Use separate precedence names (
reset,base,components) when order matters. - Scope your selectors. The CSS is global once in the head. Use specific class names, or a library that generates scoped names, to avoid clashes.
- Plain tags repeat. A plain
<style>inside a component that renders 100 times creates 100 elements. Use managed mode or put the CSS in a stylesheet. - Dynamic CSS. The
hrefis the identity of a managed block, so it is meant for fixed CSS. For values that change at runtime, set CSS variables with thestyleprop and let the managed CSS read them. - Server rendering. Managed styles are emitted into the head of the HTML, so first paint is styled. Plain styles are streamed in place.
- Compare with
<link>. A managed<style>inlines CSS (no extra request);<link rel="stylesheet" precedence>references a file (cacheable, but a request). Both take part in the same ordering.
Related
<link>,<script>,<meta>, Stylesheet precedence- Lessons: JSX in depth, useEffect