|
| 1 | +--- |
| 2 | +title: CSSProperties |
| 3 | +--- |
| 4 | + |
| 5 | +`CSSProperties` is the type for inline styles passed via the `style` prop. It extends [`csstype`](https://github.com/frenic/csstype)'s `Properties<string | number>`, so every standard CSS property is covered with autocompletion and value validation. |
| 6 | + |
| 7 | +## Parameters |
| 8 | + |
| 9 | +`CSSProperties` does not take any parameters. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Inline styles |
| 14 | + |
| 15 | +```tsx |
| 16 | +function Banner() { |
| 17 | + return ( |
| 18 | + <div style={{ backgroundColor: "papayawhip", padding: 16 }}>Hello</div> |
| 19 | + ); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### Reusable style objects |
| 24 | + |
| 25 | +Pull style objects out to share between elements. Annotate with `CSSProperties` so TypeScript checks the values: |
| 26 | + |
| 27 | +```tsx |
| 28 | +import { CSSProperties } from "react"; |
| 29 | + |
| 30 | +const card: CSSProperties = { |
| 31 | + borderRadius: 8, |
| 32 | + padding: 16, |
| 33 | + boxShadow: "0 1px 3px rgba(0, 0, 0, 0.1)", |
| 34 | +}; |
| 35 | + |
| 36 | +function Card({ children }: { children: ReactNode }) { |
| 37 | + return <div style={card}>{children}</div>; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Typing a `style` prop on your own component |
| 42 | + |
| 43 | +If your component forwards a style object, type the prop as `CSSProperties` directly — that's exactly what HTML elements use: |
| 44 | + |
| 45 | +```tsx |
| 46 | +type BoxProps = { |
| 47 | + style?: CSSProperties; |
| 48 | + children?: ReactNode; |
| 49 | +}; |
| 50 | + |
| 51 | +function Box({ style, children }: BoxProps) { |
| 52 | + return <div style={{ padding: 16, ...style }}>{children}</div>; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## Values: numbers vs strings |
| 57 | + |
| 58 | +For length-like properties, **numbers are interpreted as pixels** — React appends `px` automatically. Strings are passed through verbatim. |
| 59 | + |
| 60 | +```tsx |
| 61 | +<div style={{ width: 100 }} /> // → width: 100px |
| 62 | +<div style={{ width: "100%" }} /> // → width: 100% |
| 63 | +<div style={{ width: "10rem" }} /> // → width: 10rem |
| 64 | +``` |
| 65 | + |
| 66 | +A handful of properties (such as `lineHeight`, `opacity`, `zIndex`, `flexGrow`) are unitless — passing a number leaves it unitless. |
| 67 | + |
| 68 | +## Vendor prefixes |
| 69 | + |
| 70 | +Vendor-prefixed properties are written in PascalCase, not with the leading hyphen: |
| 71 | + |
| 72 | +```tsx |
| 73 | +const style: CSSProperties = { |
| 74 | + WebkitTransform: "rotate(45deg)", |
| 75 | + MozAppearance: "none", |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +## Custom properties (CSS variables) |
| 80 | + |
| 81 | +`CSSProperties` deliberately has no index signature, so writing a CSS custom property triggers a TypeScript error: |
| 82 | + |
| 83 | +```tsx |
| 84 | +// ❌ Type '{ "--accent": string; }' is not assignable to type 'CSSProperties'. |
| 85 | +<div style={{ "--accent": "tomato" }} /> |
| 86 | +``` |
| 87 | + |
| 88 | +There are three common workarounds. |
| 89 | + |
| 90 | +### 1. Type assertion (quickest) |
| 91 | + |
| 92 | +```tsx |
| 93 | +<div style={{ "--accent": "tomato" } as CSSProperties} /> |
| 94 | +``` |
| 95 | + |
| 96 | +Fine for one-off use, but you lose type-checking on the rest of the object. |
| 97 | + |
| 98 | +### 2. Intersection with an indexed type (recommended) |
| 99 | + |
| 100 | +Keep type-checking for normal properties while allowing any `--*` key: |
| 101 | + |
| 102 | +```tsx |
| 103 | +type CSSPropertiesWithVars = CSSProperties & { |
| 104 | + [key: `--${string}`]: string | number; |
| 105 | +}; |
| 106 | + |
| 107 | +const style: CSSPropertiesWithVars = { |
| 108 | + color: "white", |
| 109 | + "--accent": "tomato", |
| 110 | +}; |
| 111 | + |
| 112 | +<div style={style} />; |
| 113 | +``` |
| 114 | + |
| 115 | +### 3. Module augmentation (when you have a fixed set of variables) |
| 116 | + |
| 117 | +If your design system has a known list of CSS variables, augment `CSSProperties` once and get autocomplete everywhere: |
| 118 | + |
| 119 | +```tsx |
| 120 | +// global.d.ts |
| 121 | +import "react"; |
| 122 | + |
| 123 | +declare module "react" { |
| 124 | + interface CSSProperties { |
| 125 | + "--accent"?: string; |
| 126 | + "--spacing"?: string | number; |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +After this, `style={{ "--accent": "tomato" }}` type-checks with no assertion. |
| 132 | + |
| 133 | +## Typing individual CSS values with `csstype` |
| 134 | + |
| 135 | +If you want a prop that accepts a single CSS value — e.g. a color or a display value — import the underlying [`csstype`](https://github.com/frenic/csstype) package and use the `Property` namespace: |
| 136 | + |
| 137 | +```tsx |
| 138 | +import type { Property } from "csstype"; |
| 139 | + |
| 140 | +type BadgeProps = { |
| 141 | + color?: Property.Color; // any valid CSS <color> |
| 142 | + display?: Property.Display; |
| 143 | +}; |
| 144 | +``` |
| 145 | + |
| 146 | +`csstype` is already a transitive dependency of `@types/react`, so no extra install is needed — just import the types. |
| 147 | + |
| 148 | +## CSS-in-JS libraries |
| 149 | + |
| 150 | +Most CSS-in-JS libraries (Emotion, styled-components, Stitches, vanilla-extract, …) augment `CSSProperties` themselves to support library-specific features such as nested selectors, pseudo-classes as object keys, or theme tokens. If you see properties like `"&:hover"` accepted as keys, that's a library augmentation, not a feature of `@types/react`. |
0 commit comments