1. Introduction
Given a complex responsive layout, developers often require granular control over how the contents of an individual module will respond relative to the size of their parent container rather than the viewport size. This limitation conflicts with the goal of creating modular, independent components, often requiring a number of redundant CSS, complex exception cases, and workarounds, and the problem compounds itself depending on how dramatically a module adapts at each of its breakpoints.
For the purposes of this document, an element query refers not to a specific syntax or proposed method of addressing the use cases that follow, but a method of controlling styling based on the size of a containing element.
This document presents some of the use cases that “element queries” would solve, in allowing authors to define styles (chiefly, layouts) within an individual module on the basis of the size of the module itself rather than the viewport. The goal is to demonstrate, beyond a reasonable doubt, the need for a standardized method of allowing content to respond to its container’s dimensions (as opposed to the overall viewport size). This would facilitate the construction of layouts comprised of many modular, independent HTML components with a minimum of duplicated CSS and overrides specific to the modules’ parent containers.
In formulating the requirements, this document tries to be neutral—it is not predicated on any solution. The document only tries to describe the use cases and what the WICG understands, from practice, would be needed to address the use cases in the form of requirements. The WICG expect that a technical specification can be created to formally address each or all of the requirements (i.e., the solution).
2. Use Cases
This document reflects the efforts of members from the Responsive Issues Community Group (RICG) and Web Platform Incubator Community Group (WICG), and with ongoing feedback from the designer/developer community via blog posts, informal polls, and other social media sources. The WICG’s goal for this document is to make sure that developer requirements for element queries have been captured for the purpose of standardization.
The following use cases represent common usage scenarios:
Figure 1 is an example of a relatively simple and fully self-contained module’s layout, using only a single min-width
media query to reflow content.

In this layout, this module can occupy containing elements of varying widths, governed by multiple breakpoints. In small viewports, we’ll be using a linear layout where each of our five modules occupy the full viewport — this layout is covered by the base styles outside of our media query. At higher breakpoints, these modules will be displayed side-by-side: three across, then below that two across. The three-across layout will be covered by the global styles within our viewport-based media query. Parent-specific overrides will need to be written for the two-across layout, as these modules will need to shift to their wide-format at a smaller viewport size than the ones above them.

In order to accomplish this layout, a developer would need to duplicate all of this module’s “wide layout” styles into a second viewport-based media query—set to apply at a smaller min-width than the global breakpoint styles—with all of our styles scoped to a parent container. This now means that any future edits or bug fixes to the wide-format layout styles will need to be made in multiple places, and this maintenance cost increases exponentially with each module-level breakpoint required.

Similarly, introducing a new context unlike the previous two—shown in figure 4 with the introduction of a “sidebar” on an interior page layout—means duplicating or overriding all of a module’s media queries yet again.

The module in this new sidebar context will never need to reflow to the wider layout, and as such we’re left with two options: scoping all of our modules—with the exception of the two-across layout—to a parent class, or introducing a new media query that overrides all of our wide-layout styles based on the sidebar’s parent class. Despite the relative simplicity of our module and our overall page layouts, one is left with a bloated and difficult to maintain stylesheet.

3. Limitations of current techniques
There is currently no native CSS mechanism available to developers that achieves the primary function of container queries, which is to employ the full capabilities of CSS to a specific element tree based on the state and properties of their container. Therefore developers must look to workarounds to achieve their goals. These workarounds and their limitations are outlined here:
- Media Queries
- The use of Media Queries can only affect CSS changes based on the state and properties of the viewport. The viewport is not a good indicator of the state and properties of a particular container within the viewport.
- CSS layout methods
- CSS layout methods such as Flexbox and Grid, alongside CSS-based workarounds (such as the Fab Four technique which uses a combination of
width
,calc
,min-width
andmax-width
to affect binary changes to an elements width based on the width of its parent when compared to a static value, or breakpoint) can only deliver a small fraction of the functionality Container Queries are intended to offer. These methods cannot employ the full capabilities of CSS on an element tree. For example, they cannot affect a child element’sdisplay
property in order to affect an new layout based on the width of a container. -
iframe
s -
The use of
iframe
s produces the closest effect to what Container Queries is intended to achieve, but their use has some significant drawbacks:- Without the use of JavaScript, some browsers require that the contents of the
iframe
are stored in a separate HTML document rather than being part of the current document. This necessitates a new document composition paradigm that’s very different from the methods currently employed by developers. - There is currently no standard, declarative way for an
iframe
to auto-size to fit its contents. A container that is the subject of a Container Query is still expected to be able to adapt to its contents in the opposite dimension of the query. For example, in the case of a Container Query that is based on a container’s width, it is still expected that the container can adapt to the height of it’s contents, subject to any overflow rules. - The contents of an
iframe
do not inherit any styles from their parent document, forcing the styles used by the module to be redeclared. -
iframe
s incur additional overhead by creating additional nested browsing contexts, which can degrade performance.
- Without the use of JavaScript, some browsers require that the contents of the
- JavaScript-based solutions
-
JavaScript-based solutions (for example EQCSS) should not be considered an adequate replacement for a native CSS solution for all the same reasons any other layout- or appearance-based declarations are the domain of CSS, not JavaScript. The usual caveats of JavaScript availability and failure also apply.
JavaScript solutions also create additional network traffic, require additional parsing and perform an additional reflow after every layout that affects the matching state of a container query.
4. Requirements
The use cases give rise to the following requirements:
5. Open issues
We are tracking open issues on Github. Please help us close them!
6. Change history
A complete history of changes is available on Github.
You can also see all closed issues relating to this document.
7. Acknowledgements
A complete list of participants of the Web Platform Incubator Community Group is available at the W3C Community Group Website.