Sub topic 9
Grouping cards and page sections
This chapter assumes you know the box model, rem units, and the spacing scale from the earlier chapter. It also assumes you know basic flexbox and grid. You will use the scale to space cards and page sections so that the groups are clear.
Two jobs: inside a card, between cards
A card is a small group. Its content (a heading, text and a link) belongs together, so the space inside the card should be tighter than the space between cards. If the two are equal, the eye cannot tell where one card ends and the next begins.
Use two different tools:
- Padding puts space inside the card, between its edge and its content.
- Gap on the parent grid puts space between the cards.
.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 gap property adds space only between items. The first and last cards get no extra outer space, so you do not need :last-child rules. repeat(auto-fill, minmax(16rem, 1fr)) fits as many columns of at least 16rem as the container allows and shares the leftover width.
Choosing the ratio
Here padding and gap are both --space-4. That works because the border draws the card edge. The border is what separates the groups.
If you remove the border and the background, the card has no visible edge. Then the space outside must be clearly larger than the space inside. Try these values:
| Situation | Inside the card | Between cards |
|---|---|---|
| Border or background present | `--space-4` | `--space-4` |
| No border, no background | `--space-3` | `--space-5` |
A good rule is that the outer space should be at least 1.5 times the inner space when nothing else marks the edge.
Spacing inside the card
A card also has its own inner groups. A title sits closer to its description than to the button below. You can use the flow rule from the previous chapter:
.card > * + * {
margin-block-start: var(--space-2);
}
.card > .card-action {
margin-block-start: var(--space-4);
}The extra top margin on the action sets the button apart from the text. This follows the same proximity principle at a smaller scale.
Spacing between page sections
Sections are larger groups, so they need larger space. Use the top of the scale for them.
.section {
padding-block: var(--space-6);
}
.section > h2 {
margin-block-end: var(--space-4);
}Section padding is larger than the gap between cards, and the card gap is larger than the padding inside a card's text blocks. The result is a ladder of distances:
Each step up in the ladder signals a weaker relationship. Content one step apart is related. Content several steps apart is not.
Common errors
- Equal spacing everywhere. If every gap is
--space-3, nothing looks grouped. Vary the values on purpose. - Margins on the cards instead of gap. Margins add space at the outer edges of the list and need special cases for the first and last card. Use
gap. - Section spacing that is too small. If a section's padding is similar to a card's gap, the heading of the next section looks like it belongs to the cards above it.
- Using a different value per component. Pick from the scale. Random values like
37pxmake the ladder hard to read and hard to maintain.
Exercise
A card list has gap: 1rem, and each card has padding: 1.5rem but no border or background. Users say the text of neighbouring cards runs together. What is wrong, and how would you fix it using the scale --space-3: 1rem, --space-4: 1.5rem, --space-5: 2.5rem?
Show answer
The space between cards (1rem) is smaller than the space inside them (1.5rem), and nothing marks the card edges, so the cards look like one block. Make the outer space larger than the inner space. For example, set gap: var(--space-5) and reduce the padding to var(--space-3), or add a border or background so the edge is visible.