Component Width Queries
Part of the Responsive UI Patterns section of Coddy's Tailwind CSS journey. Lesson 26 of 55.
A viewport breakpoint cannot distinguish a sidebar card from a main-column card on the same page. Mark a containing region with @container to establish inline-size containment, then let a descendant query that region’s width.
<section class="@container"><div class="grid grid-cols-1 @min-[360px]:grid-cols-2 gap-3"><h2>Library event</h2><p>Details</p></div></section>The example switches from one to two columns when its host reaches 360px. @min-[360px]: is a container minimum; ordinary md: still refers to the viewport. The queried region must be an ancestor of the element being styled.
Container queries make a reusable component respond to the room it actually receives. Test both sides of the threshold while keeping the browser width unchanged so a mistaken viewport query cannot appear correct.
Challenge
MediumMake the host an inline-size query container. Its card uses one column below a host width of 400px and two equal columns from 400px upward, with 16px gaps on both axes. The supplied width buttons change only the host, not the viewport.
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.
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="host" class="w-[320px] "><div id="card" class="grid grid-cols-1 gap-4"><h2>Studio news</h2><p>New evening sessions</p></div></section><button id="size399" class="m-2">Width 399</button><button id="size400" class="m-2">Width 400</button><button id="size560" class="m-2">Width 560</button>
<script src="./controls.js"></script>
</body></html>
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Responsive UI Patterns
1Page Shells
Growing Page ShellsFlexible Content RailsBounded Scroll RegionsFull-Bleed RegionsRecap: Page Shells4Form Layouts
Responsive Field GroupsFocused Field GroupsSelectable Form OptionsNative Validation CuesRecap: Form Layouts2Navigation Patterns
Wrapping NavigationCurrent Page CuesNative Menu DisclosuresKeyboard Skip LinksRecap: Navigation5Data Tables
Scrollable Data TablesPredictable Table ColumnsRow State PresentationSticky Table HeadersRecap: Data Tables8Interactive Panels
Expanded Control CuesChoice Card StatesFocus-Aware Help PanelsContent-Aware Empty StatesRecap: Interactive Panels11Project: Event Portal
Portal ShellSession Browser3Content Cards
Aligned Card ActionsCard Media FramesResponsive Feature CardsShared Card RowsRecap: Content Cards6Container Queries
Component Width QueriesBounded Component StatesNamed Query RegionsBounded Fluid ComponentsRecap: Container Queries