Build an adaptive course directory and learn how tracks, gaps and automatic repetition work.
Grid is useful when the position of an item depends on both a row and a column. A directory of classes, products, or articles often needs cards in separate rows to share the same column edges. This guide builds a community course catalogue that adapts as the viewport changes. You will see how the browser sizes tracks, why long content can cause overflow, and when to use explicit column counts instead of asking the browser to choose a count automatically.
Name the container and the items
Start with a list of course cards. Every card has a title and description. The element surrounding all the cards becomes the grid container. Each direct child becomes an item. The heading inside a card stays part of that card. If you add an extra wrapper between the container and all the individual cards, that wrapper may become the only item. Before changing the grid rule, inspect the browser’s document tree to check which elements really are direct children.
Keep the content in a sensible reading order. In this example the cards appear in source order from first to last, and the browser places them into grid cells. A reader navigating by keyboard or reading the markup can follow the same sequence. If the business logic says that one course is more important, position it first in the data or the HTML. Avoid using visual placement to disguise an order that makes little sense when the page is read without styles.
Build an adaptive directory
Paste this complete document into the HTML editor. The grid starts from a reasonable minimum card width, then uses spare space to expand the cards. It does not use a fixed count tied to a list of phone models.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Community course catalogue</title>
<style>
* { box-sizing: border-box; }
body { margin: 2rem auto; max-width: 58rem; padding: 0 1rem; color: #10244a; font: 1rem/1.55 system-ui; }
.directory { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%, 15rem),1fr)); gap: 1rem; }
.card { border: 1px solid #c6deee; background: #edf7ff; padding: 1rem; border-radius: .6rem; overflow-wrap: anywhere; }
.card h2 { margin-block-start: 0; font-size: 1.2rem; }
.card p { margin-block-end: 0; }
</style>
</head>
<body>
<main>
<h1>Community course catalogue</h1>
<p>Find a short introductory course to try this month.</p>
<div class="directory">
<article class="card"><h2>Repair a bicycle tube</h2><p>Practice removing a wheel and finding a puncture.</p></article>
<article class="card"><h2>First steps in sewing</h2><p>Learn how to make a tidy hand stitch.</p></article>
<article class="card"><h2>Make a garden plan</h2><p>Sketch where to put plants that need different amounts of sunlight.</p></article>
<article class="card"><h2>Make an audio recording</h2><p>Compare microphone positions and save a short recording.</p></article>
<article class="card"><h2>Keep a field notebook</h2><p>Record the date, weather and observations from a local walk.</p></article>
</div>
</main>
</body>
</html>
Try it in the HTML editor
A track is the space between two grid lines. The column declaration in the sample repeats a track whose minimum aims for fifteen rem. The minimum is limited to the full available width, so a very narrow viewport can still accommodate one column. The maximum is one fraction of available space. Grid creates as many columns as fit and shares the remaining space among them. Resize the preview to watch the first row change from several cards to one or two.
Compare auto-fit with a fixed number of tracks
Change the declaration to repeat three columns with a minimum of zero and a maximum of one fraction. The result is three equally sized columns at desktop width. At phone width the same three columns now remain. If the cards become too narrow, either add a breakpoint or restore the adaptive version. A fixed count can be useful when two neighbouring sections must align. The adaptive version is useful when the content should find its own column count from the available space.
Compare auto-fit with auto-fill by temporarily keeping just two cards in a wide viewport. Both can create room for more tracks, but auto-fit collapses empty repeated tracks so the occupied tracks can expand. Auto-fill can keep those empty tracks in the layout. If two items fill most of the available width when you expected them to stay compact, this is a useful distinction to check. A maximum width on the container is another way to stop cards becoming too wide.
Try copying a fixed track rule from the CSS Grid Generator once it is available on the public site. The generator makes it easy to see the effect of changing the count or the gap. Use the full page example to decide whether the count should respond to content width. The two approaches solve the same arrangement problem at different levels of control.
Discover why a supposedly flexible grid overflows
Put a very long unbroken identifier in one card title. The example allows it to wrap. Remove the wrapping rule and observe how the text behaves when the viewport narrows. A track and the content inside it can overflow for different reasons. The track may have a minimum that is larger than the available width, or the content may be too wide for a perfectly valid track. Inspect the borders of the grid and its item to identify which size is causing the problem.
Images can create a similar effect. Add a wide image to a card and cap its width at the item width, with its height allowed to adjust. If the media still escapes the card, inspect whether the image itself or an enclosing component has a fixed size. Avoid hiding the entire overflowing area if that would cut off information or interactive controls. Resize the asset or give the relevant part of the card an intentional scroll area when appropriate.
Test differences that a polished mock-up can hide
Try one card, then five, then six. With an incomplete last row, check whether the visual alignment still matches your expectation. Try a long title, a short description, and an internationalised version with different word lengths. Check at a scale that increases text size. An example with five identical cards can make a fragile grid look correct. Real content forces the layout to show whether it can accommodate unequal heights and widths.
The grid can stretch items across a row to a common height, but that does not require you to give every card a fixed height. A taller description can make the row taller. If an action belongs at the bottom of every card, give the card its own internal layout and let the action align within that space. Keep those inner card rules separate from the outer grid, which is responsible for column placement and spacing.
Finish by using the inspector’s grid overlay to compare track edges with card edges. Adjust one gap and one track declaration and identify the visible result of each change. Then decide whether the directory should use automatic tracks or a fixed number at chosen widths. The CSS editor is a place to compare both versions side by side in separate snippets. See MDN’s CSS Grid guide for a more detailed account of track sizing and placement.
See where new rows come from
The example declares its columns and leaves rows to the browser. When it runs out of room in the current row, automatic placement creates another row. That is an implicit track. Its height can follow the content, so longer descriptions do not force all cards on the page to use the height of the longest one. Change the description in only the fourth card and look at the height of the row containing it. Compare that row with the row above.
If an item unexpectedly appears in a new row, first check how many columns fit. Then inspect whether an explicit placement rule made a cell unavailable. An item spanning two columns cannot fit in a single remaining cell. Automatic placement can leave a gap and put the item on a later row, which may be the correct result. Avoid automatically packing later items into those gaps if it makes the visual sequence diverge from how the links are navigated.
For a catalogue that contains a highlighted introduction, you can keep the introduction outside the grid and let the grid focus on cards. When a featured item belongs to the same sequence, decide whether it should span more tracks. Test that choice at the smallest width before shipping. A two-column span on a one-column grid can create an unwanted extra track. A responsive placement rule or a separate section may make the design easier to maintain.