CSS Containment and container queries
It looks like [CSS containment](https://publishing-project.rivendellweb.net/css-containment/) will finally bring container queries into browsers without the need for a polyfill. This post will discuss the current implementation in Chromium browsers. **Note:** The specification for container queries hasn't been finalized. It is possible but unlikely that the specification and the corresponding CSS will change. Don't use `@container` in production until the feature is finalized. ## What problem do container queries solve? Container queries give developers finer control over the layout of components. Rather than using [media queries](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries) that provide responsiveness based on the viewport state, they give responsiveness based on the parent container or the next ancestor that has containment applied to it. This means that we can have both large elements and layouts that are use media queries and smaller components that use container queries to provide finer-controlled layouts based on the status of elements on the page, not on the page overall. ## How they work? ```html
…
```
The example component will:
* be customized when its width is 400 or smaller ("<" is a synonym for `max-width`, not “less than”)
* be customized when its width is 700 or greater (">" is a synonym for `min-width`, not “greater than”).
* apply the following classes `media-object-eqio-<400` and `media-object-eqio->700` as appropriate
The final step is to create the CSS for each matching condition. The query is a combination of the `data-eqio-prefix` HTML attribute and the value of the query we want to match, `<400` or `>700`.
`<` and `>` are special characters in CSS so they need to be escaped as `\<` and `\>`.
```css
@supports not (contain: inline-size) {
.media-object-eqio-\<400 {
/* less than or equal to 400px */
}
.media-object-eqio-\>700 {
/* greater than or equal to 700px */
}
}
```
Both solutions should be functionally identical but, as with everything on the web, please test it with your own project to make sure it works as intended in all browsers.
## Links and resources
* [An introduction to CSS Containment](https://blogs.igalia.com/mrego/2019/01/11/an-introduction-to-css-containment/)
* [The new responsive: Web design in a component-driven world](https://web.dev/new-responsive/)
* [Next Gen CSS: @container](https://css-tricks.com/next-gen-css-container/)
* [CSS Container Queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries) — MDN
* [CSS Podcast, Episode 43: Containment](https://thecsspodcast.libsyn.com/043-containment)
* [CSS Containment Module Level 1](https://www.w3.org/TR/css-contain-1/)
* [Can I use: CSS containment](https://caniuse.com/#feat=css-containment)
* [CSS Triggers](https://csstriggers.com/) — What gets triggered by mutating a given property
* [CSS Containment in Chrome 52](https://developers.google.com/web/updates/2016/06/css-containment)
* [Avoid Large, Complex Layouts and Layout Thrashing](https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing)
* [CSS Contain](https://developer.mozilla.org/en-US/docs/Web/CSS/contain)