Atom

Box

Polymorphic layout primitive with system props for spacing, sizing, flex, colors, and more.

Overview

The Box component is a low-level layout primitive that provides system props for styling. It's polymorphic (can render as any HTML element) and accepts props for spacing, sizing, flexbox, colors, borders, and more - all using design tokens.

A simple box with padding, background, and shadow

Props

Base Props

Prop Type Default Description
as ElementType 'div' HTML element to render
children ReactNode - Box content

Spacing Props

Prop Type Description
pnumber | stringPadding (all sides)
pxnumber | stringHorizontal padding
pynumber | stringVertical padding
pt, pr, pb, plnumber | stringIndividual padding sides
mnumber | stringMargin (all sides)
mxnumber | stringHorizontal margin
mynumber | stringVertical margin
mt, mr, mb, mlnumber | stringIndividual margin sides
gapnumber | stringGap between children
rowGap, columnGapnumber | stringIndividual gap directions

Sizing Props

Prop Type Description
width, heightnumber | stringDimensions
minWidth, maxWidthnumber | stringWidth constraints
minHeight, maxHeightnumber | stringHeight constraints

Flex Props

Prop Type Description
displaystringDisplay property
flexDirectionstringFlex direction
flexWrapstringFlex wrapping
justifyContentstringMain axis alignment
alignItemsstringCross axis alignment
alignContentstringMulti-line alignment
alignSelfstringSelf alignment
flexstring | numberFlex shorthand
flexGrow, flexShrink, flexBasisstring | numberIndividual flex properties
ordernumberFlex order

Position Props

Prop Type Description
positionstringPosition type
top, right, bottom, leftnumber | stringPosition offsets
zIndexnumber | stringStack order

Style Props

Prop Type Description
bgstringBackground color
colorstringText color
borderstringBorder (all sides)
borderTop, borderRight, borderBottom, borderLeftstringIndividual borders
borderColorstringBorder color
radiusRadiusTokenBorder radius (all corners)
radiusTopLeft, radiusTopRight, radiusBottomLeft, radiusBottomRightRadiusTokenIndividual corner radii
overflow, overflowX, overflowYstringOverflow behavior
elevationElevationTokenBox shadow

Token Types Reference

  • RadiusToken: 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | 'full'
  • ElevationToken: 100 | 200 | 300 | 400 | 'none'

Examples

Flex Container

Using Box as a flex container with gap.

Item 1

Item 2

Item 3

<Box display="flex" gap={12} p={16} bg="var(--background-base)" radius="md">
  <Box p={12} bg="var(--background-primary-subtle)" radius="sm">Item 1</Box>
  <Box p={12} bg="var(--background-primary-subtle)" radius="sm">Item 2</Box>
  <Box p={12} bg="var(--background-primary-subtle)" radius="sm">Item 3</Box>
</Box>

Card-like Container

Box styled as a card with elevation.

Card Title

This is a card-like container built with Box component.

<Box p={24} bg="var(--background-base)" radius="lg" elevation={200}>
  <Text variant="title">Card Title</Text>
  <Box mt={8}>
    <Text color="muted">Card content...</Text>
  </Box>
</Box>

Polymorphic Usage

Render Box as different HTML elements.

Rendered as <section>

Rendered as <article>

<Box as="section">...</Box>
<Box as="article">...</Box>
<Box as="aside">...</Box>

Spacing with Numbers

Numeric values are converted to pixels.

p=8 m=4 (8px padding, 4px margin)

p=16 m=8 (16px padding, 8px margin)

p=24 m=12 (24px padding, 12px margin)

Accessibility

  • Semantic HTML: Use the 'as' prop to render semantic elements like section, article, nav.
  • Layout Only: Box provides layout styling, not interactive behavior.

Best Practices

  • Use semantic HTML elements via the 'as' prop when appropriate
  • Use design tokens (CSS variables) for colors and sizes when possible
  • Prefer higher-level components (Card, Stack) for common patterns
  • Keep Box usage for custom layouts that don't fit standard patterns