CSS Baseline
Joy UI provides a CssBaseline component 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> <span class="token punctuation">{</span> CssVarsProvider <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@mui/joy/styles'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> CssBaseline <span class="token keyword">from</span> <span class="token string">'@mui/joy/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">CssVarsProvider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">{</span><span class="token comment">/* must be used under CssVarsProvider */</span><span class="token punctuation">}</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">CssVarsProvider</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 Joy 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> <span class="token punctuation">{</span> CssVarsProvider <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@mui/joy/styles'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> ScopedCssBaseline <span class="token keyword">from</span> <span class="token string">'@mui/joy/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">const</span> <span class="token punctuation">[</span>root<span class="token punctuation">,</span> setRoot<span class="token punctuation">]</span> <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">useState</span><span class="token punctuation">(</span><span class="token keyword">null</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 punctuation">{</span><span class="token comment">/* target the node to ScopedCssBaseline's div */</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">CssVarsProvider</span></span> <span class="token attr-name">colorSchemeNode</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>root<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">{</span><span class="token comment">/* must be used under CssVarsProvider */</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 attr-name">ref</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 parameter">element</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">setRoot</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">}</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 tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">CssVarsProvider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
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.body
for standard devices and a white background for print devices. - The CSS
color-scheme
is applied by default. You can disable it by settingdisableColorScheme
to true on theCssBaseline
orScopedCssBaseline
.
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.
Color scheme
The CSS color-scheme
is applied by default to render proper built-in components on the web. You can disable it by setting disableColorScheme
to true on the CssBaseline
or ScopedCssBaseline
.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">CssVarsProvider</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 attr-name">disableColorScheme</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">CssVarsProvider</span></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">CssVarsProvider</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 attr-name">disableColorScheme</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">ScopedCssBaseline</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">CssVarsProvider</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 default
Typography
's level (body1
) style on the<body>
element. The style comes fromtheme.typography.{default typography level prop}
. - Set the font-weight to
bold
for the<b>
and<strong>
elements. - Custom font-smoothing is enabled for better display of the default font.
Customization
CssBaseline
To custom the styles produced by the CssBaseline
component, append the GlobalStyles
next to it:
<span class="token keyword">import</span> <span class="token punctuation">{</span> CssVarsProvider <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@mui/joy/styles'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> CssBaseline <span class="token keyword">from</span> <span class="token string">'@mui/joy/CssBaseline'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> GlobalStyles <span class="token keyword">from</span> <span class="token string">'@mui/joy/GlobalStyles'</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">CssVarsProvider</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">/* CssBaseline must come first */</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">GlobalStyles</span></span>
<span class="token attr-name">styles</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 comment">// CSS object styles</span>
<span class="token literal-property property">html</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">body</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">,</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">CssVarsProvider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
ScopedCssBaseline
You can customize it using the themed components approach. The component identifier is JoyScopedCssBaseline
which contains only the root
slot.
<span class="token keyword">import</span> <span class="token punctuation">{</span> CssVarsProvider<span class="token punctuation">,</span> extendTheme <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@mui/joy/styles'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> ScopedCssBaseline <span class="token keyword">from</span> <span class="token string">'@mui/joy/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">const</span> theme <span class="token operator">=</span> <span class="token function">extendTheme</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">JoyScopedCssBaseline</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">styleOverrides</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token function-variable function">root</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> theme <span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token comment">// ...CSS object styles</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">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">const</span> <span class="token punctuation">[</span>root<span class="token punctuation">,</span> setRoot<span class="token punctuation">]</span> <span class="token operator">=</span> React<span class="token punctuation">.</span><span class="token function">useState</span><span class="token punctuation">(</span><span class="token keyword">null</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 punctuation">{</span><span class="token comment">/* target the node to ScopedCssBaseline's div */</span><span class="token punctuation">}</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">CssVarsProvider</span></span> <span class="token attr-name">colorSchemeNode</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>root<span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">{</span><span class="token comment">/* must be used under CssVarsProvider */</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 attr-name">ref</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 parameter">element</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">setRoot</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">}</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 tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">CssVarsProvider</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>