Responsive styles

@mantine/core package exports several components that can help you organize responsive styles without writing CSS. Note that in most cases that are not covered in this guide, the best way to make your app responsive is to add styles with createStyles function.

Configure breakpoints

theme.breakpoints are used in all responsive Mantine components. Breakpoints are set in px (rem, em and other values are not supported). You can configure these values with MantineProvider:

import { MantineProvider } from '@mantine/core';
function App() {
return (
<MantineProvider
withGlobalStyles
withNormalizeCSS
theme={{
breakpoints: {
xs: 500,
sm: 800,
md: 1000,
lg: 1200,
xl: 1400,
},
}}
>
<YourApp />
</MantineProvider>
);
}

Default theme.breakpoints values:

BreakpointViewport width
xs576px
sm768px
md992px
lg1200px
xl1400px

Media queries in createStyles

In most cases using createStyles function is cleanest way to add responsive styles to any element. Consider using it first before hacking your way out of writing CSS with other approaches:

import { createStyles } from '@mantine/core';
const useStyles = createStyles((theme) => ({
container: {
height: 100,
backgroundColor: theme.colors.blue[6],
// Media query with value from theme
[`@media (max-width: ${theme.breakpoints.xl}px)`]: {
backgroundColor: theme.colors.pink[6],
},
// Static media query
'@media (max-width: 800px)': {
backgroundColor: theme.colors.orange[6],
},
},
}));
function Demo() {
const { classes } = useStyles();
return <div className={classes.container} />;
}

MediaQuery component

MediaQuery component lets you apply styles to given component or element if given media query matches:

(max-width: 1200px) and (min-width: 800px) breakpoints
import { MediaQuery, Text } from '@mantine/core';
function Demo() {
return (
<MediaQuery
query="(max-width: 1200px) and (min-width: 800px)"
styles={{ fontSize: 20, '&:hover': { backgroundColor: 'silver' } }}
>
<Text>(max-width: 1200px) and (min-width: 800px) breakpoints</Text>
</MediaQuery>
);
}

Changing component size based on media query

Most of Mantine components support size prop with xs, sm, md, lg and xl values. You cannot change these values within component props. Instead you can use MediaQuery component to render different components based on media query:

<>
<MediaQuery smallerThan="sm" styles={{ display: 'none' }}>
<TextInput size="xl" />
</MediaQuery>
<MediaQuery largerThan="sm" styles={{ display: 'none' }}>
<TextInput size="md" />
</MediaQuery>
</>

The approach with MediaQuery component will work with SSR. If you do not need SSR support the more cleaner way to change size is to use use-media-query hook:

import { TextInput } from '@mantine/core';
import { useMediaQuery } from '@mantine/hooks';
function Demo() {
// Will always return false during ssr, use only when you do not have ssr
const largeScreen = useMediaQuery('(min-width: 900px)');
return <TextInput size={largeScreen ? 'xl' : 'md'} />;
}

Inline media queries with sx

All Mantine components support sx prop with which you can add styles (including responsive styles) to root component element:

import { TextInput } from '@mantine/core';
function Demo() {
return (
<TextInput
sx={(theme) => ({
background: theme.colors.gray[0],
padding: theme.spacing.md,
'@media (max-width: 755px)': {
padding: theme.spacing.sm,
},
})}
/>
);
}

If you want to add styles with sx prop to non-Mantine component, you can wrap it in Box:

// Example with react-router-dom Link
import { Link } from 'react-router-dom';
import { Box } from '@mantine/core';
function Demo() {
return (
<Box
component={Link}
to="/some-path/"
sx={(theme) => ({
fontSize: theme.fontSizes.md,
'@media (max-width: 755px)': {
fontSize: theme.fontSizes.sm,
},
})}
>
Styled react-router link
</Box>
);
}