Box
Box is the basic layout component, and Flex, Stack and Grid are all built from it.
Box is the basic layout component. Flex, Stack and Grid are each a Box with a fixed display and a shorter list of props, so use one of those when you already know the layout. Use Box when the layout has to change display at a container size, or when you need one-off spacing around something.
Box props cover padding, margin, gap, width and height, position, and the props a flex or grid child needs. Spacing values are steps on the space scale rather than lengths, so spacing stays consistent across your app. Anything the props don't cover can go in style, which is applied last.
Controls never set the space around themselves, so Box is how you add it. To put space above a button, write <Box mt="4"><Button /></Box>.
Every prop takes either a single value or an object with a value for each container size: initial, sm, md and lg. The sizes follow the width of the nearest Box marked container, or the root Theme when there is none. Only mark a Box as a container when its width comes from the layout or from width, because a container can't size itself from its content. In a plain flex row it would collapse to zero width, and a development build warns you when that happens.
Three more props cover the edge cases. Setting a margin prop to bleed, as in mx="bleed", cancels the padding of the surrounding Card or Surface so that a picture or a divider can reach its edge. backdrop marks a region where content shows behind the controls, so they use the theme's material. render applies the Box props to an element you choose, such as a <section>.
Props
Generated from the types. It lists what this component DECLARES: a prop that arrives from a shared type or from the platform is real and is not repeated here.
| Prop | Type | What it does |
|---|---|---|
backdrop? | boolean | Marks an area where the components sit over other content, such as a toolbar over a canvas or a panel over an image. |
container? | boolean | Make this Box measurable. |
render? | RenderElement | Renders the box as a different element, so you don't add a wrapper. |
It also takes every prop of a <div>.
- Utility classes — Box passes its values to the stylesheet as inline custom properties, so the stylesheet stays the same size no matter which values your app uses.
- A separate bleed prop — Bleed is a value you give a margin prop, such as
m="bleed"ormx="bleed". Separate props would repeat every margin spelling a second time. - Containment by default — A container can't take its width from its content, so it would collapse to zero in a flex row. Add
containeronly on a Box whose width the layout already sets.