Your example is almost there -- to make the Container Query work, you just need to include a container-type
on the element you want to use as the container. (The square brackets in your example are unnecessary.)
In your code snippet:
const Item = styled("div")(({ theme }) => ({
border: "solid 1px",
padding: 100,
"@container (min-width: 300px)": { // Corrected your query to min-width and removed brackets
background: "red",
},
}));
export default function BasicGrid() {
return (
{/* Adds containerType to the "container" element */}
<div style={{ containerType: "inline-size" }}>
<Item>I turn red for width smaller than 300</Item>
</div>
);
}
I have provided additional examples in this sandbox that demonstrate using MUI's Grid, adjusting content based on the available size of the container.
Large example - Content constrained to 100%:
https://i.stack.imgur.com/VJDqc.png
Smaller example - Content constrained to 300px:
https://i.stack.imgur.com/HiCI2.png
Smallest example - Content constrained to 250px:
https://i.stack.imgur.com/VV649.png
Working CodeSandbox: https://codesandbox.io/s/mui-container-queries-gvl4fs
Browser Support: https://caniuse.com/css-container-queries