Menu
CoddyTech

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 icon

Challenge

Medium

Make 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>
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