--- layout: default title: "Box, Stack and Grid" --- # ``, `` and `` To facilitate the creation of custom layouts, Material-UI provides three layout components that you will use all the time. It's important that you understand how they work. ## `` The `` component serves as a wrapper component for most of the CSS utility needs. By default, it renders a `
` element. ![Box](./img/box.webp) It accepts all [the `sx` prop](./SX.md), which makes it easy to style: {% raw %} ```jsx import { Box } from '@mui/material'; const MyComponent = () => ( // ... ); ``` {% endraw %} You can render any component with ``, not only a `
`. For instance, to render a `` with custom styles: {% raw %} ```jsx export default function BoxComponent() { return ( ); } ``` {% endraw %} If you need to customize the spacing of a react-admin or a Material-UI element, use their `sx` prop instead of a `` wrapper: {% raw %} ```jsx // prefer // to ``` {% endraw %} **Tip**: If you use `` to lay out elements with `display:flex` or `display:grid`, prefer the dedicated `` and `` components instead. To learn more, read the [Material-UI Box documentation](https://mui.com/material-ui/react-box/). ## `` `` is a container component for arranging elements vertically or horizontally. Use it whenever you need to lay out elements in a column or a row. ![Stack](./img/stack.webp) ```jsx import { Stack } from '@mui/material'; const Items = () => ( Item 1 Item 2 Item 3 ); ``` `` renders a `
` with `display:flex`. It accepts any React element as child - even other `` components. `` accepts all the FlexBox properties as props, so you can write layouts as follows: ```jsx ``` Use the `spacing` prop to control the space between children. The spacing value can be any number, including decimals, or a string. The prop is converted into a CSS property using the `theme.spacing()` helper. ```jsx Item 1 Item 2 Item 3 ``` By default, `` arranges items vertically in a column. Use the `direction` prop to position items horizontally in a row: ![Stack row](./img/stack-row.webp) ```jsx Item 1 Item 2 Item 3 ``` Use the `divider` prop to insert an element between each child. {% raw %} ```jsx import { Stack } from '@mui/material'; export const DividerStack = () => (
`1px solid ${theme.palette.mode === 'dark' ? '#262B32' : '#fff'}`, }} /> } spacing={2} > Item 1 Item 2 Item 3
); ``` {% endraw %} To learn more, read the [Material-UI Stack documentation](https://mui.com/material-ui/react-stack/). ## `` `` is a container component for arranging elements in a grid. It is based on a 12-column grid layout. ![Grid](./img/grid.webp) {% raw %} ```jsx import Grid from '@mui/material/Grid'; export const BasicGrid = () => ( xs=8 xs=4 xs=4 xs=8 ); ``` {% endraw %} Column widths are integer values between 1 and 12; they apply at any breakpoint and indicate how many columns are occupied by the component. A value given to a breakpoint applies to all the other breakpoints wider than it (unless overridden, as you can read later in this page). For example, `xs={12}` sizes a component to occupy the whole viewport width regardless of its size. Components may have multiple widths defined, causing the layout to change at the defined breakpoint. Width values given to larger breakpoints override those given to smaller breakpoints. For example, `xs={12} sm={6}` sizes a component to occupy half of the viewport width (6 columns) when viewport width is 600 or more pixels. For smaller viewports, the component fills all 12 available columns. To control space between children, use the `spacing` prop. The spacing value can be any positive number, including decimals and any string. The prop is converted into a CSS property using the `theme.spacing()` helper. ```jsx ``` The `rowSpacing` and `columnSpacing` props allow for specifying the row and column gaps independently. It's similar to the `row-gap` and `column-gap` properties of CSS Grid. {% raw %} ```jsx 1 2 3 4 ``` {% endraw %} The Auto-layout makes the items equitably share the available space. That also means you can set the width of one item and the others will automatically resize around it. ```jsx xs xs=6 xs ``` You can switch the props' value based on the active breakpoint. {% raw %} ```jsx {Array.from(Array(6)).map((_, index) => ( xs=2 ))} ``` {% endraw %} Responsive values is supported by: - `columns` - `columnSpacing` - `direction` - `rowSpacing` - `spacing` Check the [Material-UI Grid documentation](https://mui.com/components/grid/) for more information.