Menu
CoddyTech

Named Query Regions

Part of the Responsive UI Patterns section of Coddy's Tailwind CSS journey. Lesson 28 of 55.

Nested components can introduce several possible query ancestors. Name a container when a descendant must respond to a particular region rather than simply the nearest eligible container. @container/sidebar creates an inline-size container named sidebar.

<aside class="@container/sidebar"><div class="@container"><p class="hidden @min-[420px]/sidebar:block">Sidebar details</p></div></aside>

The example uses @min-[420px]/sidebar: to target that named region. The slash name belongs to the variant before the utility. A nearer unnamed container does not replace the requested named ancestor.

Names are useful for components embedded inside other components. Keep them meaningful and local to the layout role. Repeated copies may share the same name because each descendant resolves its own ancestor chain.

challenge icon

Challenge

Medium

Name the 600px outer section panel and make it an inline-size query container. The card is hidden by default, but must display as a block when the named panel reaches 500px. Its nearer unnamed 240px container must not control this decision.

Preserve the supplied content, semantic elements and IDs. Edit the requested utility classes. Press Run preview after edits, then Check/Run code to grade the result. Supplied JavaScript is read-only.

Use the supplied controls to narrow the named panel to 400px and widen it to 600px again. The summary must hide below its threshold and reappear above it while the nearer unnamed container stays 240px wide.

Try it yourself

<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Interface workshop</title><link rel="stylesheet" href="./styles.css"></head><body>
<section id="outer" class="w-[600px] "><div class="@container w-[240px]"><div id="card" class="hidden">Panel summary</div></div></section>
<button id="narrow">Narrow panel</button><button id="wide">Wide panel</button><script src="./controls.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Responsive UI Patterns