I would like to use media queries to resize elements based on the size of a div element they are in. I cannot use the screen size as the div is just used like a widget within the webpage, and its size can vary.
I would like to use media queries to resize elements based on the size
Share
Yes, CSS Container Queries are what you’re looking for. The CSS Containment Module is the specification that details this feature. You can check browser support here.
You can read more about the decade of work, including proposals, proofs-of-concept, discussions and other contributions by the broader web developer community here! For more details on how such a feature might work and be used, check out Miriam Suzanne’s extensive explainer.
Media queries aren’t designed to work based on elements in a page. They are designed to work based on devices or media types (hence why they are called media queries).
width,height, and other dimension-based media features all refer to the dimensions of either the viewport or the device’s screen in screen-based media. They cannot be used to refer to a certain element on a page.If you need to apply styles depending on the size of a certain
divelement on your page, you’ll have to use JavaScript to observe changes in the size of thatdivelement instead of media queries.Alternatively, with more modern layout techniques introduced since the original publication of this answer such as flexbox and standards such as custom properties, you may not need media or element queries after all. Djave provides an example.