The Box component is the foundational layout primitive in Theme UI. It provides access to spacing utilities, colors, and the sx prop for theme-aware styling.
import { Box } from 'theme-ui'
<Box
sx={{
padding: 3,
backgroundColor: 'primary',
borderRadius: 4,
}}
>
Content
</Box>
The Box component accepts all standard HTML div attributes plus the following:
Render the component as a different HTML element or React component. Defaults to 'div'.<Box as="section">Section content</Box>
Theme-aware style object for applying CSS with access to theme values.<Box sx={{ color: 'primary', fontSize: [2, 3, 4] }}>
Responsive text
</Box>
Apply a variant style from theme.variants. Use dot notation to access nested variants.<Box variant="card">Card style</Box>
<Box variant="buttons.primary">Button style</Box>
Spacing Props
Box includes shorthand props for margin and padding that map to theme space values:
m | margin
ResponsiveValue<string | number>
Margin on all sides.
mt | marginTop
ResponsiveValue<string | number>
Margin top.
mr | marginRight
ResponsiveValue<string | number>
Margin right.
mb | marginBottom
ResponsiveValue<string | number>
Margin bottom.
ml | marginLeft
ResponsiveValue<string | number>
Margin left.
mx | marginX
ResponsiveValue<string | number>
Margin left and right.
my | marginY
ResponsiveValue<string | number>
Margin top and bottom.
p | padding
ResponsiveValue<string | number>
Padding on all sides.
pt | paddingTop
ResponsiveValue<string | number>
Padding top.
pr | paddingRight
ResponsiveValue<string | number>
Padding right.
pb | paddingBottom
ResponsiveValue<string | number>
Padding bottom.
pl | paddingLeft
ResponsiveValue<string | number>
Padding left.
px | paddingX
ResponsiveValue<string | number>
Padding left and right.
py | paddingY
ResponsiveValue<string | number>
Padding top and bottom.
Color Props
Text color from theme.colors.
Background color from theme.colors.
Examples
Responsive Spacing
<Box p={[2, 3, 4]} mx="auto">
Padding increases with viewport size
</Box>
Color and Background
<Box color="white" bg="primary" p={3}>
Themed colors
</Box>
Custom Element
<Box as="article" sx={{ maxWidth: 768 }}>
Article content
</Box>