The theme.breakpoints.between
function now allows you to pass numbers instead of breakpoint keys, but make sure to follow the correct order. The lower pixel value should come first.
Your current code is producing:
@media (min-width:1200px) and (max-width:1020.95px)
This won't work because the minimum width can never be higher than the maximum width.
If you switch the values and use
theme.breakpoints.between(1021, 1200)
, you will get something that works better:
@media (min-width:1021px) and (max-width:1199.95px)
It's important to note that theme.breakpoints.x
functions are simply shortcuts for creating media query strings. You can also directly specify the media query in JSS styles as shown below:
import React from "react";
import { makeStyles, useTheme } from "@material-ui/core/styles";
const useStyles = makeStyles({
myCustomClass: {
"@media (min-width:600px) and (max-width:1199.95px)": {
backgroundColor: "green"
},
"@media (max-width:599.95px)": {
backgroundColor: "blue"
},
"@media (max-width:900px)": {
color: "yellow"
}
}
});
export default function App() {
const theme = useTheme();
const classes = useStyles();
return (
<div className={classes.myCustomClass}>
theme.breakpoints.between("1021", "1200"):{" "}
{theme.breakpoints.between("1021", "1200")}
<br />
theme.breakpoints.between("sm", "md"):{" "}
{theme.breakpoints.between("sm", "md")}
<br />
theme.breakpoints.only("md"): {theme.breakpoints.only("md")}
</div>
);
}
https://codesandbox.io/s/breakpoints-bzgjd?fontsize=14&hidenavigation=1&theme=dark