Skip to main content

css

The css function is the core styling utility in Theme UI. It processes style objects, performs theme lookups, handles responsive arrays, and transforms shorthand properties into their full CSS equivalents.

Function Signature

Parameters

ThemeUIStyleObject<TTheme>
Style object containing CSS properties, aliases, and theme-aware values. Can be:
  • Plain style object with CSS properties
  • Function that receives the theme and returns a style object
  • Object with responsive arrays for mobile-first styling
  • Object with variant references

Returns

(props) => CSSObject
Returns a function that accepts a theme object (or { theme } prop object) and returns the final CSS object with all transformations applied.

How It Works

1. Theme Lookups

The css function automatically looks up values from theme scales based on the CSS property:

2. Responsive Arrays

Arrays are transformed into mobile-first media queries using the theme’s breakpoints:

3. Property Aliases

Shorthand properties are expanded to their full CSS equivalents:

Available Aliases

  • bgbackgroundColor
  • mmargin
  • mtmarginTop
  • mrmarginRight
  • mbmarginBottom
  • mlmarginLeft
  • mxmarginX (expands to left + right)
  • mymarginY (expands to top + bottom)
  • ppadding
  • ptpaddingTop
  • prpaddingRight
  • pbpaddingBottom
  • plpaddingLeft
  • pxpaddingX (expands to left + right)
  • pypaddingY (expands to top + bottom)

4. Multiple Properties

Some properties expand to multiple CSS properties:

Available Multiples

  • marginXmarginLeft, marginRight
  • marginYmarginTop, marginBottom
  • paddingXpaddingLeft, paddingRight
  • paddingYpaddingTop, paddingBottom
  • scrollMarginXscrollMarginLeft, scrollMarginRight
  • scrollMarginYscrollMarginTop, scrollMarginBottom
  • scrollPaddingXscrollPaddingLeft, scrollPaddingRight
  • scrollPaddingYscrollPaddingTop, scrollPaddingBottom
  • sizewidth, height

5. Negative Values

Certain properties (margins, positioning) support negative values:

6. Nested Scales with __default

Theme scales can be nested with a __default key:

7. Variant References

Reference predefined variants from the theme:

Theme Scale Mappings

The css function maps CSS properties to theme scales automatically:
Maps to theme.colors:
  • color
  • backgroundColor, background
  • borderColor, borderTopColor, borderBottomColor, borderLeftColor, borderRightColor
  • borderBlockColor, borderBlockEndColor, borderBlockStartColor
  • borderInlineColor, borderInlineEndColor, borderInlineStartColor
  • caretColor
  • columnRuleColor
  • outlineColor
  • textDecorationColor
  • accentColor
  • fill, stroke (SVG)
Maps to theme.space:
  • margin, marginTop, marginRight, marginBottom, marginLeft
  • marginX, marginY
  • marginBlock, marginBlockEnd, marginBlockStart
  • marginInline, marginInlineEnd, marginInlineStart
  • padding, paddingTop, paddingRight, paddingBottom, paddingLeft
  • paddingX, paddingY
  • paddingBlock, paddingBlockEnd, paddingBlockStart
  • paddingInline, paddingInlineEnd, paddingInlineStart
  • top, right, bottom, left
  • inset, insetBlock, insetBlockEnd, insetBlockStart
  • insetInline, insetInlineEnd, insetInlineStart
  • scrollMargin, scrollMarginTop, scrollMarginRight, scrollMarginBottom, scrollMarginLeft
  • scrollMarginX, scrollMarginY
  • scrollPadding, scrollPaddingTop, scrollPaddingRight, scrollPaddingBottom, scrollPaddingLeft
  • scrollPaddingX, scrollPaddingY
  • gap, gridGap, columnGap, gridColumnGap, rowGap, gridRowGap
  • fontFamilytheme.fonts
  • fontSizetheme.fontSizes
  • fontWeighttheme.fontWeights
  • lineHeighttheme.lineHeights
  • letterSpacingtheme.letterSpacings
  • border, borderTop, borderRight, borderBottom, borderLefttheme.borders
  • borderBlock, borderBlockEnd, borderBlockStarttheme.borders
  • borderInline, borderInlineEnd, borderInlineStarttheme.borders
  • borderWidth, borderTopWidth, borderBottomWidth, borderLeftWidth, borderRightWidththeme.borderWidths
  • borderBlockWidth, borderBlockEndWidth, borderBlockStartWidththeme.borderWidths
  • borderInlineWidth, borderInlineEndWidth, borderInlineStartWidththeme.borderWidths
  • borderStyle, borderTopStyle, borderBottomStyle, borderLeftStyle, borderRightStyletheme.borderStyles
  • borderBlockStyle, borderBlockEndStyle, borderBlockStartStyletheme.borderStyles
  • borderInlineStyle, borderInlineEndStyle, borderInlineStartStyletheme.borderStyles
  • columnRuleWidththeme.borderWidths
Maps to theme.radii:
  • borderRadius
  • borderTopLeftRadius, borderTopRightRadius
  • borderBottomLeftRadius, borderBottomRightRadius
  • borderEndEndRadius, borderEndStartRadius
  • borderStartEndRadius, borderStartStartRadius
Maps to theme.sizes:
  • width, minWidth, maxWidth
  • height, minHeight, maxHeight
  • flexBasis
  • size
  • blockSize, minBlockSize, maxBlockSize
  • inlineSize, minInlineSize, maxInlineSize
  • columnWidth
  • boxShadow, textShadowtheme.shadows
  • zIndextheme.zIndices
  • opacitytheme.opacities
  • transitiontheme.transitions

Default Theme

If no theme is provided, css uses these defaults:

Default Breakpoints

When no breakpoints are defined in the theme:

Examples

Basic Usage

Responsive Styles

Nested Styles

Function Form

  • get - Extract values from theme objects
  • sx prop - Component-level styling with the sx prop