Auto-Fitting Collections
Part of the Responsive UI Patterns section of Coddy's Tailwind CSS journey. Lesson 31 of 55.
An intrinsic card grid can choose its column count from available space rather than a list of viewport breakpoints. repeat(auto-fit,minmax(160px,1fr)) creates as many 160px-minimum tracks as fit and distributes remaining width among them.
<section class="grid grid-cols-[repeat(auto-fit,minmax(min(100%,160px),1fr))] gap-4"><article>A</article><article>B</article><article>C</article></section>For a region that might become narrower than that minimum, use min(100%,160px) as the lower bound. The example preserves the preferred minimum when possible and lets one track shrink to the available width when necessary. Commas keep the nested expression within one arbitrary utility.
Track count depends on both minimum width and gaps. Choose deterministic fixtures when checking geometry, and preserve every card instead of hiding items to make a layout fit.
Challenge
MediumCreate an auto-fitting card grid with 200px minimum tracks that can shrink to the full container width when narrower than 200px. Share remaining width equally and use 16px gaps on both axes. Preserve all four cards.
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.
The supplied Compact allocation control limits the grid to 180px without resizing the browser. The single track must shrink to that available width.
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="grid" class="w-full grid grid-cols-1 gap-4"><article>Studio 1</article><article>Studio 2</article><article>Studio 3</article><article>Studio 4</article></section>
<button id="compact">Compact allocation</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 Layouts7Intrinsic Layouts
Auto-Fitting CollectionsResilient Label RowsMulti-Line PreviewsReadable Long TokensRecap: Intrinsic 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 Browser