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
Thetheme.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
Theroot 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
- Typography
- UI Elements
- Forms
layout- Container component variantsgrids- 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 thesx prop:
Variant Helper Function
Thethemed 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.
