Skip to main content
Variants allow you to define reusable style combinations in your theme that can be applied to components with a single prop. They’re useful for creating consistent design patterns across your application.

What Are Variants?

Variants are named style objects stored in your theme that can be referenced by name. They provide a way to apply complex, predefined styles to components without repeating code.

Theme.styles - MDX Element Styles

The theme.styles object is a special variant location for styling MDX elements and content. These styles are automatically applied to matching HTML elements within MDX.

Basic Usage

Root Styles

The root style is special - it’s applied to the <html> element:
By default, theme.styles.root is applied to the html element. Set theme.config.useRootStyles: false to disable this behavior.

Component Variants

Theme UI provides predefined variant locations for built-in components. Each component looks for its variants in a specific place in the theme.

Button Variants

Card Variants

Text Variants

Available Variant Locations

Theme UI recognizes these variant locations:
  • layout - Container component variants
  • grids - Grid component variants

Custom Variants

You can create custom variant locations for your own components:

Using Variants with sx Prop

You can reference variants in the sx prop:

Variant Helper Function

The themed helper extracts styles from theme.styles:

Nested Variants

Variants can reference other scales in your theme:

Responsive Variants

Variants support responsive arrays:

TypeScript Support

Extend the Theme type to get autocomplete for your custom variants:

Best Practices

Use Semantic Names

Name variants based on their purpose (e.g., primary, danger) rather than appearance (e.g., blue, red).

Keep Variants Focused

Each variant should serve a specific use case. Avoid creating too many variants that differ only slightly.

Reference Theme Scales

Use theme values in variants instead of hardcoding values for consistency.

Document Your Variants

Maintain documentation of available variants and their intended use cases.

Example: Complete Variant System