Right now, we have the power to write CSS that only applies when the browser window itself is at certain widths or heights. Breakpoints, as it were. Super useful. What we don’t have (natively) is the ability to write conditional CSS based on any particular elements (or “containers”) properties.
It’s going on 2 years since the RICG decided to tackle this. I’m not exactly sure what the status is there. It seems a bit on pause, but that doesn’t mean the entire discussion is on pause.
From what I hear, and echo, the refrain amongst front-end devs is: if I had container queries available, 90% of the media queries I write would be container queries. The thought being, you’re usually trying to adjust some particular element’s properties that are tied to something a bit more scoped than the entire browser window.
Ethan Marcotte recently wrote:
I don’t want to suggest that the technical challenges of specifying container queries are in any way easy. But some kind of movement would be deeply appreciated by the entire responsive design community. Speaking just for myself, I know container queries would revolutionize my design practice, and better prepare responsive design for mobile, desktop, tablet—and whatever’s coming next.
Hear hear.
He pointed to some of his own work in which modules that are in extremely different situations that aren’t directly related to the viewport, but more directly related to a parent container.
But now lately, in a wacky surprise twist, there has been a good bit of “hmmmm, perhaps we don’t need these as much as we think we do” going on.
For instance, Dave Rupert, while playing with CSS Grid Layout, found he was able to ditch some media queries entirely:
I refactored a ~50 line Flexbox grid to just ~5 lines of CSS with CSS Grid. … The best part is, we don’t need media queries! This will save a lot of code down the line. In this particular project, we actually have three Flexbox grids with slightly different breakpoints. The
auto-fill
keyword auto-generates columns when space is available.
See the Pen Flexbox Grid vs. CSS Grid by Dave Rupert (@davatron5000) on CodePen.
Flexbox can do some really fancy dancing with its ability to wrap, its ability to be told if it can grow or not, and how much and to what limits gives us a lot of control without explicit media queries.
Grid gives us even more tools with things like auto layout, minmax()
, and keywords like min-content
and auto-fill
. The fact that you can nest Flex containers and Grid containers any-which-way makes for some powerful possibilities.
For example, see Jonathan Snook playing around (who, self-admittedly, is just starting to wrap his head around these types of layouts) have a lot of success in container-query-like control over some modules.
Paul Robert Lloyd explictly questions the need for container queries:
In my mind, container queries appear to be yesterday’s answer to today’s problems. I’d much rather we use the great new tools we have, and embrace a future that’s finally here.
Even rebuilding Ethan’s example with only a page-layout affecting media query:
See the Pen Rebuilding The Toast’s Recirculation Module by Paul Lloyd (@paulrobertlloyd) on CodePen.
Jeremy Keith filling in as baby bear:
… this is a good well-reasoned post about why container queries might not be the all-healing solution for our responsive design problems. Thing is, I don’t think container queries are trying to be an all-encompassing solution, but rather a very useful solution for one particular class of problem.
So I don’t really see container queries competing with, say, grid layout (any more than grid layout is competing with flexbox), but rather one more tool that would be really useful to have in our arsenal.
Even container queries can’t solve all RWD problems, of course. As Paul said:
The final reason I question the need for container queries is that a change in layout sometimes requires a change in behavior, too. If accomplishing this involves restructuring the DOM, we’re essentially swapping one component for another.
So yes, newfangled layout tools may save us in many situations from needing container queries specifically for layout changes. But layout isn’t the only thing that may need to change in an element depending on the container situation. Ethan responds:
Depending on a module’s placement, height, and width, I might want to change several different aspects of its design. These changes would include, but aren’t limited to:
- Visual weight. Depending on where the module’s positioned, I’ll frequently want to change how visually prominent it is. This might include changing its color, its background color, or the size of individual elements within it, all depending on the space allocated to the module.
- Typography. Related to that last point, I frequently need to change the typography of an element, based on the size of its container. Grid Layout and flexbox aren’t any help here, sadly; and as much as I love flexible typography—Trent can vouch for me here—the utilities we have to work with there are still very viewport-focused.
- Content hierarchy. I often need to change an element’s priority, depending on the size of its container. Maybe I’ll conditionally
position
an element higher (or lower) to make it more (or less) visible, as the design warrants. In a flexbox-ier layout, I might want to change theorder
of a given element, or perhaps change theflex-direction
of the module—and again, all based on the dimensions of the container.
Brad Frost makes a simple, logical argument:
… having a mechanism that says “if this component lives in a container that’s at least X-wide, make these style changes” feels like it makes sense.
It’s a smidge like responsive images. You can do a lot with srcset
and sizes
, but there is also <picture>
when you need to be super explicit about how to behave.
Personally, I’d like to see about 100 different use cases fleshed out. If it turns out some of them can be done sans container queries, awesome, but it still seems highly likely to me that having container queries available to us would be mighty handy.
For what it’s worth, I’ve come across this JavaScript plugin that allows element queries to be written within CSS. Never tried it myself, but I appreciate the attempt at providing a solution:
http://elementqueries.com/
Heh, you beat me to it. :)
I’ve been using this one which has been around since 2013:
http://marcj.github.io/css-element-queries/
I have been thinking that I would use container queries to show or hide certain UI components when the size of a higher-level component gets too small. For example, if I have a toolbar of buttons, I could switch them out for a space-saving dropdown when there isn’t enough space for them. There’s still quite a bit of jiggery-pokery involved in figuring out when to do that switch, (and admittedly, something like resizeObserver might be better suited for that) but at the moment it’s completely impossible to write a reusable version of such a toolbar with CSS alone, as far as I know.
That said, EQCSS would probably be a great way to test the waters and figure out what the bare-minimum components of a container queries spec should be. I haven’t had a chance to play with it much myself, but if anyone’s wondering whether they need container queries to solve something, it’s worth checking out.
Also worth exploring/supporting/checking out: ELQ and @ausi’s cq-prolyfill project. They all have different approaches to element/container queries.
Thead about resizeObserver starts here:
I feel like container queries are going to even more necessary as Web Components and frameworks like Angular and React support scoped CSS. It would allow you to make really flexible components that could be more easily reused and shared.
I always use media queries as a last resort anyway, especially for layout. In my experience, a well-written interface that isn’t hugely idiosyncratic should be 80%-90% mobile-optimized by default, because its elements are already naturally responsive to their surroundings. Not that media queries are inherently bad – for a small subset of things, there’s really no other solution – but it can be very tempting to overuse them, at which point they become bad. I think having container queries would both provide the ability to do things that weren’t possible before, and also amplify this problem. So, uh, I guess I support adding them but I also think we need better education on their use.
This article I came across yesterday sums up my feelings nicely:
I put together a Pen of Mat Marquis container/element query example from his ALA article. I added some feature queries that use a combination of Grid and Element Queries (using element-query.js EQCSS).
I think Element Queries will be an incredibly useful tool to use alongside Flexbox and Grid and other layout methods that we will be seeing in the near future. I appreciate Paul’s article but I think we are going to need more than Grid or Flexbox to create layouts that are not dependent on the viewport but on the content.
I echo nearly everyone’s thoughts here I would really love element/container queries. I think it’s absolutely essentially for web components.
Imagine say an audio player component from a service like spotify/google play.
You could embed this audio player on a page and not need to say pick a special player just to accomodate the space you have but the service itself could control it’s appearance based on the size.
So for example say you only have a 50px by 50px space. it could display just a play button. If you had a 300px by 100px space you could display a play button, brand name(google play… spotify etc.) and the song name as well as the time.
If you had a 800px by 600 px space. It could show a playlist, the time, brand.
I think you get the idea, and you might be thinking – well you could do this same thing by using an iFrame, well you’re right but you’re basically just cheating element queries. Using element queries you could use javascript to render an embed directly in the page.
Coming from the design side, our team thinks exactly in the terms that Ethan outlines above – we haven’t found a great solution. Why isn’t EQCSS/css-element-queries used more commonly – is there a trade-off involved in using it?