Responsive styles
Media queries
Configure breakpoints
theme.breakpoints
are used in all responsive Mantine components. Breakpoints are expected to be set in em
units.
You can configure these values with MantineProvider:
Default theme.breakpoints
values:
Breakpoint | Viewport width | Value in px |
---|---|---|
xs | 36em | 576px |
sm | 48em | 768px |
md | 62em | 992px |
lg | 75em | 1200px |
xl | 88em | 1408px |
Breakpoints variables in CSS modules
It is not possible to use CSS variables inside media queries – these values cannot be dynamically
generated by MantineProvider. To use Mantine theme breakpoints
in your .css
files, you will need postcss-simple-vars
package:
Add it to your PostCSS config in postcss.config.cjs
:
Then you will be able to access these variables in your .css
files:
Will be transformed to:
Dynamic breakpoints are not supported
Values that are defined in
postcss-simple-vars
config are static and are not connected to the theme – if values change, you will need to update them manually in both theme override and postcss config.
hiddenFrom and visibleFrom props
All Mantine components that have a root element support hiddenFrom
and visibleFrom
props.
These props accept breakpoint (xs
, sm
, md
, lg
, xl
) and hide the component when
viewport width is less than or greater than the specified breakpoint:
Hidden and visible from as classes
If you are building a custom component and want to use the same logic as in hiddenFrom
and visibleFrom
props
but you do not want to use Mantine components, you can use mantine-hidden-from-{x}
and mantine-visible-from-{x}
classes.
Component size based on media query
Some components support size
prop, which changes various aspects of component appearance.
size
prop is not responsive – it is not possible to define different component sizes for different
screen sizes. Instead, you can render multiple components with different sizes and show/hide them
based on media query with className
or hiddenFrom
/visibleFrom
props:
use-media-query hook
You can use use-media-query hook to change some of component props based on media query. Note that this approach is not recommended for most of the cases if you have ssr in your application (you use Next.js, Remix, Gatsby or any other framework that includes ssr) as it may cause hydration mismatch. If you do not have ssr in your application (for example, if you use Vite), then you can safely use this hook to change props of components or conditionally render components based on hook return value.
use-media-query hook can be safely used to change props of components that are not rendered
on server side (modals, tooltips, etc.). In the following example, it is safe to use useMediaQuery
hook to
change Tooltip props as it is not rendered on server side:
use-matches hook
use-matches
hook exported from @mantine/core
is an alternative to use-media-query
if you need to match multiple media queries and values. It accepts an object with media queries as keys and
values at given breakpoint as values.
Note that use-matches
hook uses the same logic as use-media-query under the hood,
it is not recommended to be used as a primary source of responsive styles, especially if you have ssr in your application.
In the following example:
- Starting from
theme.breakpoints.lg
, color will bered.9
- Between
theme.breakpoints.sm
andtheme.breakpoints.lg
, color will beorange.9
- Below
theme.breakpoints.sm
, color will beblue.9
Container queries
Container queries enable you to apply styles to an element based on the size of the element's container. If, for example, a container has less space available in the surrounding context, you can hide certain elements or use smaller fonts. Container queries are supported in all modern browsers.
You can use rem
and em
functions from postcss-preset-mantine
in container queries. Note that CSS variables do not work in container queries and because of that
rem scaling feature is not available. If you rely on this feature,
it is better to define breakpoints in px
units.
Responsive style props
You can use object syntax to add responsive styles with style props. Note that responsive style props are less performant than regular style props, it is not recommended to use them in large lists of elements.
Responsive values are calculated the following way:
base
value is used when none of breakpoint values are appliedxs
,sm
,md
,lg
,xl
values are used when the viewport width is larger that the value of corresponding breakpoint specified in theme.breakpoints
In this case the element will have the following styles: