Sub topic 9
Grouping cards and page sections
This chapter assumes you know the CSS box model, rem units, basic CSS Grid, and the spacing scale from the earlier chapters (--space-1 through --space-6). It covers how to use that scale to make cards and page sections read as clear groups.
Why cards and sections need a plan
A page with many cards has two grouping problems at once. Each card must read as one unit, so its title, text and button need to belong together. The cards must also read as a set, separate from the heading above them and from the next section below.
White space does both jobs. The rule is the proximity principle: elements that are closer together look related. If the space inside a card is larger than the space between cards, the eye cannot tell where one card ends and the next begins.
The working rule for this chapter is:
- Space inside a group is smaller than the space between groups.
- Each level of nesting uses a larger step on the spacing scale.
Three levels of space
A typical landing page has three nested levels of grouping. Each level gets its own spacing role.
| Level | What it groups | Spacing tool | Example token |
|---|---|---|---|
| Inside a card | Title, text, button | `gap` or flow spacing | `--space-2` to `--space-3` |
| Card padding | Card content from its edge | `padding` | `--space-4` |
| Between cards | Card from card | `gap` on the grid | `--space-4` to `--space-5` |
| Between sections | Section from section | `padding-block` on the section | `--space-6` |
Notice how the numbers grow as the relationship gets weaker. Items inside a card are the most closely related, so they get the smallest gap. Whole sections are the least related, so they get the largest.
Cards: padding inside, gap outside
Use padding for the space inside a card and gap on the container for the space between cards. This keeps the spacing in one place for each job.
.card {
padding: var(--space-4);
border: 1px solid #ccc;
}
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-4);
}The grid places as many columns of at least 16rem as fit. gap adds space only between cards, never on the outer edge. If you used margins on the cards instead, you would need to remove the extra margin on the first and last cards, and you could get uneven spacing when the grid wraps.
The border problem
Look at the example again. The card has padding of --space-4 and the gap is also --space-4. With a visible border on each card, this works. The border draws the group boundary, so the equal values are fine.
Remove the border and the problem appears. The space inside the card (24px or so from the text to the invisible edge) now matches the space between cards. Text from neighboring cards sits as close to each other as the text sits to its own card's edge. The groups blur together.
You have two ways to fix this:
- Keep a visible edge: a border, a background color, or a shadow. The edge tells the eye where the group ends, so the spacing can be equal.
- Make the gap clearly larger than the padding, for example padding of
--space-3and a gap of--space-5.
.card-plain {
padding: var(--space-3);
}
.card-list-plain {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-5);
}A ratio of about 1:1.5 or larger between inner padding and outer gap is a good starting point for borderless cards. Adjust by eye.
Spacing inside the card
Inside a card, the title, description and button also form a hierarchy. A simple way to handle this is to make the card a grid or flex column with a small gap.
.card {
display: grid;
gap: var(--space-2);
align-content: start;
padding: var(--space-4);
border: 1px solid #ccc;
}
.card h3 {
margin: 0;
}
.card .button {
margin-block-start: var(--space-2);
justify-self: start;
}Two details matter here:
- Reset the default margins on headings. Browsers give
h3margins, and those margins would add to the gap and make the spacing unpredictable. (Inside a grid or flex container, margins do not collapse, so they stack on top of the gap.) - The button gets one extra step of space above it. This sets the action slightly apart from the text, which signals that it is a different kind of content.
Page sections
Sections are the largest groups on the page. Give each one vertical padding so that a clear band of space separates it from its neighbors.
:root {
--space-section: clamp(2rem, 1rem + 4vw, 5rem);
}
.section {
padding-block: var(--space-section);
}
.section > h2 {
margin-block: 0 var(--space-4);
}Here the section heading sits --space-4 above its cards. That distance is smaller than the section padding, so the heading stays attached to its cards, not to the section above it.
Heading attachment
A common mistake is to space a heading evenly between the content above and below it. Then the heading floats and belongs to neither group. The heading should be closer to the content it introduces.
Compare these two cases:
| Setup | Space above heading | Space below heading | Result |
|---|---|---|---|
| Floating heading | `--space-5` | `--space-5` | Unclear which group it belongs to |
| Attached heading | `--space-6` | `--space-3` | Heading reads as the start of the content below |
The space above is at least double the space below. That ratio is a good default.
Section boundaries without lines
You do not need a horizontal rule between sections. Large padding does the job. If you also alternate background colors, the band of color adds a second signal.
.section:nth-child(even) {
background: #f5f5f5;
}When you use background bands, the padding must go on the colored element itself. If you put the padding on a wrapper inside the band, or use margins between bands, the white page background shows through as gaps between the colors. Padding extends the background; margin does not.
Keeping content width under control
Large sections on wide screens can spread cards too far apart. Limit the content width with a container so that the distances between groups stay within a readable range.
.container {
width: min(100% - 2 * var(--space-4), 70rem);
margin-inline: auto;
}The min() function keeps a side gutter of --space-4 on narrow screens and caps the width at 70rem on wide ones. Without the cap, a three-column grid on an ultra-wide monitor would have very wide cards, and the proportions between inner padding and outer space would drift.
Edge cases
- Cards with different content heights. Grid rows stretch cards to equal height by default, so a short card gets empty space at the bottom. This is fine for grouping. Use
align-content: startas shown above so the content stays at the top and the empty space falls at the bottom. To pin the button to the bottom of every card instead, usemargin-block-start: autoon the button in a flex column. - A single card in the last row. With
auto-fill, the last row may hold one card at the left and leave empty tracks. The gap stays consistent, so the grouping holds. If you prefer the tracks to collapse, useauto-fitinstead, which stretches the cards to fill the row. - Nested sections. If a section holds sub-sections, give the sub-sections a smaller step than the top-level sections, for example
--space-5against--space-6. Each level of nesting steps down so the outer boundaries stay the strongest. - Stacking on mobile. On narrow screens the grid collapses to one column. Now the gap between cards is vertical, and each card's own content is also stacked vertically. Check that the gap is still larger than the spacing inside a card, or the cards will merge into one long list.
A complete example
This markup and CSS put the pieces together.
<section class="section">
<div class="container">
<h2>Features</h2>
<div class="card-list">
<article class="card">
<h3>Fast</h3>
<p>Pages load quickly on slow connections.</p>
<a class="button" href="#fast">Learn more</a>
</article>
<article class="card">
<h3>Simple</h3>
<p>One stylesheet and a handful of tokens.</p>
<a class="button" href="#simple">Learn more</a>
</article>
<article class="card">
<h3>Accessible</h3>
<p>Spacing that survives user text settings.</p>
<a class="button" href="#accessible">Learn more</a>
</article>
</div>
</div>
</section>:root {
--space-2: 0.5rem;
--space-4: 1.5rem;
--space-5: 2.5rem;
--space-section: clamp(2rem, 1rem + 4vw, 5rem);
}
.section {
padding-block: var(--space-section);
}
.container {
width: min(100% - 2 * var(--space-4), 70rem);
margin-inline: auto;
}
.section h2 {
margin: 0 0 var(--space-4);
}
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-5);
}
.card {
display: grid;
gap: var(--space-2);
align-content: start;
padding: var(--space-4);
border: 1px solid #ccc;
}
.card h3,
.card p {
margin: 0;
}
.card .button {
justify-self: start;
margin-block-start: var(--space-2);
}Read the result from the inside out. Text elements sit 0.5rem apart. The card edge sits 1.5rem from the text. Cards sit 2.5rem from each other. The section heading sits 1.5rem above the cards, and the section as a whole has up to 5rem of padding above and below. Each step is larger than the one inside it.
Common errors
- Using the same value everywhere. If every gap is
1rem, nothing is grouped. - Putting margin on cards for spacing and then fighting the extra margin at the grid edge. Use
gap. - Leaving default heading margins in place inside a card, so the real spacing is the sum of margin and gap.
- Using margins between colored sections and getting white stripes.
- Making the space above a heading equal to or smaller than the space below it.
Exercise
A borderless card list has .card { padding: 1.5rem; } and .card-list { gap: 1.5rem; }. Users say they cannot tell which button belongs to which card. Give two different fixes and explain why each works.
Show answer
The padding and the gap are equal, so the space inside a card matches the space between cards, and proximity does not separate them. Fix 1: increase the gap, for example to 2.5rem, or reduce the padding to 1rem, so the space between cards is clearly larger than the space inside them. Fix 2: add a second grouping signal such as a border or a background color on .card, which marks each card's edge so equal spacing is acceptable.
Exercise
A section heading has margin-block: 2.5rem (above and below) between a previous section and a card grid. How should you change it so the heading groups with its cards, and what rule of thumb did you apply?
Show answer
Make the space above larger than the space below, for example margin-block: 4rem 1rem, or move the large space into the previous section's padding and keep a small margin below the heading. The rule of thumb is that a heading should be at least about twice as far from the content before it as from the content it introduces.