A Friendly Introduction to Container Queries Container queries are a recently introduced CSS feature that allows developers to apply conditional styles based on the size of a specific parent container, rather than the global viewport. Although they have been supported in all major browsers for nearly two years and were the most requested CSS feature, adoption has been slow due to confusion about how they work and the need to explicitly define a container using the `container-type` property. The article explains that while media queries are limited to global properties like viewport width, container queries solve the "impossible problem" of creating responsive components that adapt to their local available space. Introduction For a very long time, the most-requested CSS feature has been container queries. That’s been our holy grail, the biggest missing piece in the CSS toolkit. Well, container queries have finally arrived. They’ve been supported in all major browsers for almost two years. Our prayers have been answered We can now apply conditional CSS based on an element’s container , using familiar syntax: @container min-width: 40rem { .some-elem { font-size: 1.5rem; } } Curiously, though, very few of us have actually been using container queries. Most of the developers I’ve spoken with have only done a few brief experiments, if they’ve tried them at all. We finally have the tool we’ve been asking for, but we haven’t adopted it. There are lots of reasons for this, but I think one of the biggest is that there’s been a lot of confusion around how they work. Container queries are not as straightforward as media queries. In order to use them effectively, we need to understand what the constraints are, and how to work within them. I’ve been using container queries for a few months now, and they really are quite lovely once you have the right mental model. In this blog post, we’ll unpack all of this stuff so that you can start using them in your work Link to this heading the-basic-idea-1 The basic idea So for the past couple of decades, our main tool for doing responsive design has been the media query. Most commonly, we use the width of the viewport to conditionally apply some CSS: @media min-width: 40rem { .mobile-only { display: none; } } Media queries are great, but they’re only concerned with global properties, things like the viewport dimensions or the operating system’s color theme. Sometimes, we want to apply CSS conditionally based on something local , like the size of the element’s container. For example, suppose we have a ProfileCard component, to display critical info about a user’s profile: In this particular circumstance, each ProfileCard is pretty narrow, and so the information stacks vertically in 1 tall column. In other circumstances, though, we might have a bit more breathing room. Wouldn’t it be cool if our ProfileCard could automatically shift between layouts, depending on the available space? Maybe something like this: In some cases, we can use media queries for this, if our ProfileCard scales with the size of the viewport… But this won’t always be the case. For example, maybe we’re arranging these cards in a flex grid like this: With dynamic layouts like this, each ProfileCard will use whichever layout makes the most sense given the amount of space available. It has nothing to do with the size of the viewport Clearly, media queries aren’t the right tool for this job. Instead, we can use container queries to solve this problem. Here’s what it looks like: .child-wrapper { container-type: inline-size; } .child { / Narrow layout stuff here / @container min-width: 15rem { / Wide layout stuff here / } } Pretty cool, right? I'm using native CSS nesting to place the @container at-rule right inside the .child block so that all of the CSS declarations for this element are in the same chunk of CSS. But wait, what’s the deal with .child-wrapper? What is that container-type property doing??Well, this is where things get a bit tricky. In order to use a container query, we first need to explicitly define its container. This can have some unintended consequences. It’s worth spending a few minutes digging into this. Understanding this core mechanism will save us hours of frustration down the line. Let’s talk about the “impossible problem” with container queries. Link to this heading solving-an-impossible-problem-2 Solving an impossible problem For something like 20 years now, ever since “responsive design” became a thing, developers have been asking for container queries. So why are we only being introduced now?? Well, for something like 20 years, the CSS Working Group has been saying the same thing: It’s impossible to implement container queries. It can’t be done. This’ll be much easier to understand with an example. Consider this scenario: Code Playground Result If you’re not familiar with the fit-content keyword, it’s a dynamic value that grows/shrinks based on the element’s content. If you add/remove some words to the paragraph, you’ll notice the paragraph change size: Hello world Now, let’s suppose we want to bump up the font-size of that bold text, depending on the size of its container. We can imagine doing something like this: p { width: fit-content; @container max-width: 10rem { strong { font-size: 3rem; } } } This seems to make sense… When our parent