sx prop is the primary way to style elements with Theme UI. It provides theme-aware styling with automatic lookups to your theme’s design scales.
Basic Usage
Thesx prop accepts a style object with CSS properties:
Theme Lookups
Thesx prop automatically maps CSS properties to theme scales:
Example with Theme
Given this theme:Property Aliases
Theme UI provides shorthand aliases for common CSS properties:Nested Selectors
You can use nested selectors for more complex styling:Pseudo-Selectors
All CSS pseudo-selectors are supported:Example from Theme UI Source
Here’s a real example from the Theme UI codebase:Media Queries
You can use media queries directly:For responsive styles, use responsive arrays instead of media queries for a cleaner syntax.
Nested Scale Access
Access nested values in theme scales using dot notation:Negative Values
Use negative values for margins and positioning:Raw CSS Values
You can use raw CSS values that won’t be transformed:The css Function
The sx prop uses the css function internally. You can use it directly:
Variants
Apply pre-defined style variants from your theme:Function Values
Access the theme object directly with function values:Type Safety
Thesx prop is fully typed in TypeScript:
The
sx prop provides a powerful, theme-aware styling API that makes it easy to build consistent, maintainable designs.