Stack
Stack is a container component for arranging elements vertically or horizontally.
Introduction
The Stack component manages the layout of its immediate children along the vertical or horizontal axis, with optional spacing and dividers between each child.
Basics
<span class="token keyword">import</span> Stack <span class="token keyword">from</span> <span class="token string">'@mui/material/Stack'</span><span class="token punctuation">;</span>
The Stack component acts as a generic container, wrapping around the elements to be arranged.
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.)
Stack vs. Grid
Stack
is concerned with one-dimensional layouts, while Grid handles two-dimensional layouts. The default direction is column
which stacks children vertically.
Direction
By default, Stack arranges items vertically in a column.
Use the direction
prop to position items horizontally in a row:
Dividers
Use the divider
prop to insert an element between each child.
This works particularly well with the Divider component, as shown below:
Flexbox gap
To use flexbox gap
for the spacing implementation, set the useFlexGap
prop to true.
It removes the known limitations of the default implementation that uses CSS nested selector. However, CSS flexbox gap is not fully supported in some browsers.
We recommend checking the support percentage before using it.
To set the prop to all stack instances, create a theme with default props:
<span class="token keyword">import</span> <span class="token punctuation">{</span> ThemeProvider<span class="token punctuation">,</span> createTheme <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@mui/material/styles'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> Stack <span class="token keyword">from</span> <span class="token string">'@mui/material/Stack'</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> theme <span class="token operator">=</span> <span class="token function">createTheme</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token literal-property property">components</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">MuiStack</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">defaultProps</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">useFlexGap</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">App</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ThemeProvider</span></span> <span class="token attr-name">theme</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>theme<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Stack</span></span><span class="token punctuation">></span></span>…<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Stack</span></span><span class="token punctuation">></span></span> <span class="token punctuation">{</span><span class="token comment">/* uses flexbox gap by default */</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">ThemeProvider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
Interactive demo
Below is an interactive demo that lets you explore the visual results of the different settings:
<Stack
direction="row"
justifyContent="center"
alignItems="center"
spacing={2}
>
System props
As a CSS utility component, the Stack
supports all system
properties. You can use them as props directly on the component.
For instance, a margin-top:
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Stack</span></span> <span class="token attr-name">mt</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
Limitations
Margin on the children
Customizing the margin on the children is not supported by default.
For instance, the top-margin on the Button
component below will be ignored.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Stack</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Button</span></span> <span class="token attr-name">sx</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginTop</span><span class="token operator">:</span> <span class="token string">'30px'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token operator">...</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Button</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Stack</span></span><span class="token punctuation">></span></span>
white-space: nowrap
The initial setting on flex items is min-width: auto
.
This causes a positioning conflict when children use white-space: nowrap;
.
You can reproduce the issue with:
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Stack</span></span> <span class="token attr-name">direction</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>row<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Typography</span></span> <span class="token attr-name">noWrap</span><span class="token punctuation">></span></span>
In order for the item to stay within the container you need to set min-width: 0
.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Stack</span></span> <span class="token attr-name">direction</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>row<span class="token punctuation">"</span></span> <span class="token attr-name">sx</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">minWidth</span><span class="token operator">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Typography</span></span> <span class="token attr-name">noWrap</span><span class="token punctuation">></span></span>
Truncation should be conditionally applicable on this long line of text as this is a much longer line than what the container can support.
Truncation should be conditionally applicable on this long line of text as this is a much longer line than what the container can support.
Anatomy
The Stack component is composed of a single root <div>
element:
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>MuiStack-root<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token comment"><!-- Stack contents --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>