Spacing
A wide range of shorthand responsive margin and padding utility classes to modify an element's appearance.
Notation
The space utility converts shorthand margin and padding props to margin and padding CSS declarations. The props are named using the format {property}{sides}
.
Where property is one of:
m
- for classes that set marginp
- for classes that set padding
Where sides is one of:
t
- for classes that set margin-top or padding-topb
- for classes that set margin-bottom or padding-bottoml
- for classes that set margin-left or padding-leftr
- for classes that set margin-right or padding-rightx
- for classes that set both *-left and *-righty
- for classes that set both *-top and *-bottom- blank - for classes that set a margin or padding on all 4 sides of the element
Transformation
Depending on the input and the theme configuration, the following transformation is applied:
- input:
number
& theme:number
: the prop value is multiplied by the theme value.
<span class="token keyword">const</span> theme <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token literal-property property">spacing</span><span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token operator">-</span><span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: -16px;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</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 comment">// margin: 0px;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token number">0.5</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: 4px;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: 16px;</span>
- input:
number
& theme:array
: the prop value is used as the array index.
<span class="token keyword">const</span> theme <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token literal-property property">spacing</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token operator">-</span><span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: -3px;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</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 comment">// margin: 0px;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: 3px;</span>
- input:
number
& theme:function
: the function is called with the prop value.
<span class="token keyword">const</span> theme <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token function-variable function">spacing</span><span class="token operator">:</span> <span class="token parameter">value</span> <span class="token operator">=></span> value <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</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 comment">// margin: 0px;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: 4px;</span>
- input:
string
: the prop value is passed as raw CSS value.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">m</span><span class="token operator">:</span> <span class="token string">'2rem'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin: 2rem;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">mx</span><span class="token operator">:</span> <span class="token string">'auto'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span> <span class="token comment">// margin-left: auto; margin-right: auto;</span>
Example
p: 1
m: 1
p: 2
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">p</span><span class="token operator">:</span> <span class="token number">1</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">Box</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">m</span><span class="token operator">:</span> <span class="token number">1</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">Box</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">p</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>…
Horizontal centering
The CSS flex and grid display properties are often used to align elements at the center.
However, you can also use margin-left: auto;
, margin-right: auto;
, and a width for horizontally centering:
Centered element
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Box</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">mx</span><span class="token operator">:</span> <span class="token string">'auto'</span><span class="token punctuation">,</span> <span class="token literal-property property">width</span><span class="token operator">:</span> <span class="token number">200</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>…
API
<span class="token keyword">import</span> <span class="token punctuation">{</span> spacing <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@mui/system'</span><span class="token punctuation">;</span>
Import name | Prop | CSS property | Theme key |
---|---|---|---|
spacing |
m |
margin |
spacing |
spacing |
mt |
margin-top |
spacing |
spacing |
mr |
margin-right |
spacing |
spacing |
mb |
margin-bottom |
spacing |
spacing |
ml |
margin-left |
spacing |
spacing |
mx |
margin-left , margin-right |
spacing |
spacing |
my |
margin-top , margin-bottom |
spacing |
spacing |
p |
padding |
spacing |
spacing |
pt |
padding-top |
spacing |
spacing |
pr |
padding-right |
spacing |
spacing |
pb |
padding-bottom |
spacing |
spacing |
pl |
padding-left |
spacing |
spacing |
px |
padding-left , padding-right |
spacing |
spacing |
py |
padding-top , padding-bottom |
spacing |
Some people find the prop shorthand confusing, you can use the full version if you prefer:
<span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"><Box sx={{ pt: 2 }} />
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"><Box sx={{ paddingTop: 2 }} /></span></span>
<span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"><Box sx={{ px: 2 }} />
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"><Box sx={{ paddingX: 2 }} /></span></span>