Open-Source CSS Programming • Intermediate • Estimated study time: 35 minutes
A responsive component can occupy a narrow sidebar and a wide main panel within the same browser window. A viewport media query sees one shared viewport size, even when each placement provides different space. Container queries allow the component to respond to the dimensions of an eligible ancestor. Review CSS Grid for the underlying track layout.
Prerequisites and objectives
Use a text editor and a modern browser with developer tools. Know HTML structure, CSS selectors, responsive sizing and Grid. Learners will distinguish viewport and container queries, establish a named query container, adapt a descendant at a specified threshold, use container-relative units and verify computed styles.
Select the correct measurement boundary
A media query using width ordinarily measures the viewport. A size container query examines an eligible ancestor container instead. The container-type property establishes the sizing context, while container-name lets a query identify a particular named ancestor. The shorthand container combines both declarations. For horizontal writing modes, inline-size containment supports width queries without requiring block-axis size containment. Consult MDN container queries. A query cannot restyle its own queried container based on that container’s size; the rule targets descendants.
Build and test a reusable inventory component
The example uses identical component classes in two different ancestor widths. The narrow component starts with a single column. The wider component activates a two-column Grid when its named container reaches 400 CSS pixels. Review Flexbox for another layout method. The archived HTML exercise is available for independent reproduction. Save the following complete example as container-query.html:
<!doctype html>
<html lang=”en”>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Container query lab</title>
<style>
.slot { container: device / inline-size; }
.narrow { width: min(100%, 280px); }
.wide { width: min(100%, 560px); }
.device {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
font-size: 1rem;
overflow-wrap: anywhere;
}
.device h2 { margin: 0; font-size: clamp(1rem, 5cqi, 1.5rem); }
@container device (min-width: 400px) {
.device { grid-template-columns: 1fr 1fr; }
}
</style>
<main>
<section class=”slot narrow” aria-label=”Narrow component”>
<article class=”device”><h2>Switch A</h2><p>Ready</p></article>
</section>
<section class=”slot wide” aria-label=”Wide component”>
<article class=”device”><h2>Switch B</h2><p>Ready</p></article>
</section>
</main>
</html>
Open the file locally. At a 1000-pixel viewport, Switch A uses one column and Switch B uses two. Both share the same viewport, demonstrating that the parent’s width drives the different result. If the viewport becomes narrow enough that both wrappers are below the threshold, both use the base single-column layout.
Container query units and fallbacks
The cqi unit represents one percent of the eligible query container’s inline size. The heading uses clamp to set minimum and maximum font sizes around a container-relative preferred value. The base declaration uses one column outside the query, so a browser that ignores the container rule still has a usable layout. The rem units remain relative to the root font size, as explained in responsive CSS units. Consult MDN container-type for containment details.
Verification and troubleshooting
The sample passed five automated layout assertions in Chromium 144.0.7559.96: one column in the narrow container and two in the wide container at a 1000-pixel viewport; one column at 399 pixels; two columns at 400 pixels; and one column in the wide component after shrinking the viewport to 320 pixels. These results describe the local test, not every browser. Inspect the grid-template-columns computed value in DevTools, following Chrome’s debugging guide. If the query does not activate, verify the container name, eligible ancestor and actual content-box width. Review the cascade when other rules override a declaration.
Exercises
1. Change the wide wrapper to 399 pixels and predict its layout. 2. Set it to 400 pixels and compare. 3. Remove the named containment declaration and explain the outcome. 4. Change the threshold to 600 pixels and predict both layouts at a wide viewport. 5. Explain why a viewport media query alone cannot distinguish two component widths on one page.
Answer explanations
1. One column, because the condition is false. 2. Two columns, because the minimum includes 400 pixels. 3. The named query no longer finds an eligible ancestor, leaving the base layout. 4. Both use one column, because neither wrapper reaches 600 pixels. 5. A viewport query evaluates a common viewport rather than each wrapper. Keep meaningful HTML source order and test keyboard navigation whenever interactive content is added.
Elementary conclusion
A component can change its layout according to the space available inside its parent, rather than relying on the entire screen width. Combine container queries with CSS Grid to build reusable components, and verify their behavior at the threshold and at narrow widths.
We volunteer daily to ensure the credibility of the information on the platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiative, please scan the QR code below.

BitcoinVersus.tech is not a financial advisor. The media platform reports on financial subjects purely for informational purposes.

Leave a Reply