在
MUI v5 中,可以通过指定一个对象来声明断点,其中键是断点名称,值是 CSS 值,在
sx
props 中。您可以在
此处查看 MUI 的默认断点。使用
createTheme()
,可以覆盖断点名称和值:
const theme = createTheme({
breakpoints: {
values: {
xxs: 0,
xs: 300,
sm: 600,
md: 900,
lg: 1200,
xl: 1536
}
}
});
return (
<ThemeProvider theme={theme}>
<Box
sx={{
// specify one value that is applied in all breakpoints
color: 'white',
// specify multiple values applied in specific breakpoints
backgroundColor: {
xxs: "red",
xs: "orange",
sm: "yellow",
md: "green",
lg: "blue",
xl: "purple"
}
}}
>
Box 1
</Box>
</ThemeProvider>
);
在上面的示例中,
xs: "orange"
表示如果屏幕宽度在
[300, 600)
范围内,则将
Box
的颜色设置为橙色。您还可以使用由最小断点到最大断点值组成的数组来设置断点大小:
return (
<ThemeProvider theme={theme}>
<Box
sx={{
backgroundColor: [
"red",
"orange",
// unset, screen width inside this breakpoint uses the last non-null value
null,
"green",
"blue",
"purple"
]
}}
>
Box 2
</Box>
</ThemeProvider>
);
![Edit 45847090/media-queries-in-material-ui-components](https://codesandbox.io/static/img/play-codesandbox.svg)