CSS Baseline
The CssBaseline component helps to kickstart an elegant, consistent, and simple baseline to build upon.
Global reset
You might be familiar with normalize.css, a collection of HTML element and attribute style-normalizations.
<span class="token keyword">import</span> <span class="token operator">*</span> <span class="token keyword">as</span> React <span class="token keyword">from</span> <span class="token string">'react'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> CssBaseline <span class="token keyword">from</span> <span class="token string">'@mui/material/CssBaseline'</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">MyApp</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">React.Fragment</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">CssBaseline</span></span> <span class="token punctuation">/></span></span>
<span class="token punctuation">{</span><span class="token comment">/* The rest of your application */</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">React.Fragment</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
Scoping on children
However, you might be progressively migrating a website to Material UI, using a global reset might not be an option.
It's possible to apply the baseline only to the children by using the ScopedCssBaseline
component.
<span class="token keyword">import</span> <span class="token operator">*</span> <span class="token keyword">as</span> React <span class="token keyword">from</span> <span class="token string">'react'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> ScopedCssBaseline <span class="token keyword">from</span> <span class="token string">'@mui/material/ScopedCssBaseline'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> MyApp <span class="token keyword">from</span> <span class="token string">'./MyApp'</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">MyApp</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">ScopedCssBaseline</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">{</span><span class="token comment">/* The rest of your application */</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">MyApp</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">ScopedCssBaseline</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
⚠️ Make sure you import ScopedCssBaseline
first to avoid box-sizing conflicts as in the above example.
Approach
Page
The <html>
and <body>
elements are updated to provide better page-wide defaults. More specifically:
- The margin in all browsers is removed.
- The default Material Design background color is applied.
It's using
theme.palette.background.default
for standard devices and a white background for print devices. - If
enableColorScheme
is provided toCssBaseline
, native components color will be set by applyingcolor-scheme
on<html>
. The value used is provided by the theme propertytheme.palette.mode
.
Layout
box-sizing
is set globally on the<html>
element toborder-box
. Every element—including*::before
and*::after
are declared to inherit this property, which ensures that the declared width of the element is never exceeded due to padding or border.
Scrollbars
The colors of the scrollbars can be customized to improve the contrast (especially on Windows). Add this code to your theme (for dark mode).
<span class="token keyword">import</span> darkScrollbar <span class="token keyword">from</span> <span class="token string">'@mui/material/darkScrollbar'</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">MuiCssBaseline</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token function-variable function">styleOverrides</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">themeParam</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token literal-property property">body</span><span class="token operator">:</span> themeParam<span class="token punctuation">.</span>palette<span class="token punctuation">.</span>mode <span class="token operator">===</span> <span class="token string">'dark'</span> <span class="token operator">?</span> <span class="token function">darkScrollbar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">null</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 punctuation">;</span>
Be aware, however, that using this utility (and customizing -webkit-scrollbar
) forces macOS to always show the scrollbar.
Color scheme
This API is introduced in @mui/material (v5.1.0) for switching between "light"
and "dark"
modes of native components such as scrollbar, using the color-scheme
CSS property.
To enable it, you can set enableColorScheme=true
as follows:
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">CssBaseline</span></span> <span class="token attr-name">enableColorScheme</span> <span class="token punctuation">/></span></span>
<span class="token comment">// or</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">ScopedCssBaseline</span></span> <span class="token attr-name">enableColorScheme</span> <span class="token punctuation">></span></span>
<span class="token punctuation">{</span><span class="token comment">/* The rest of your application using color-scheme*/</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">ScopedCssBaseline</span></span><span class="token punctuation">></span></span>
Typography
- No base font-size is declared on the
<html>
, but 16px is assumed (the browser default). You can learn more about the implications of changing the<html>
default font size in the theme documentation page. - Set the
theme.typography.body1
style on the<body>
element. - Set the font-weight to
theme.typography.fontWeightBold
for the<b>
and<strong>
elements. - Custom font-smoothing is enabled for better display of the Roboto font.
Customization
Head to the global customization section of the documentation to change the output of these components.