Sub topic 3
Grouping Cards and Page Sections
Assume you know basic HTML, CSS selectors, the box model, and rem and em units. This chapter uses a spacing scale defined earlier, such as --space-2, --space-3, and --space-5.
Grouping cards and page sections
Cards belong together when they share a purpose. Use equal internal padding for each card and equal gaps between cards. Use larger spacing between page sections. This creates a visible hierarchy without adding unnecessary borders.
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: var(--space-4);
}
.card {
padding: var(--space-4);
border: 1px solid #ccc;
border-radius: 0.5rem;
}
.section {
padding-block: var(--space-5);
}A card's padding separates its content from its edge. The grid's gap separates one card from another. Section padding separates a group from the groups around it.
Grouping form fields and their labels
Keep a label close to its input. Use more space between fields and still more space between field groups. The increasing distances show the structure of the form.
<form class="form">
<section class="group">
<h2>Shipping address</h2>
<label class="field">
Street
<input name="street">
</label>
<label class="field">
City
<input name="city">
</label>
</section>
</form>
<style>
.form { display: grid; gap: var(--space-5); }
.group { display: grid; gap: var(--space-3); }
.field { display: grid; gap: var(--space-1); }
</style>Scaling spacing across viewports
Large screens can support more breathing room than small screens. clamp() sets a minimum, a flexible value, and a maximum.
.section {
padding-block: clamp(2rem, 1rem + 4vw, 5rem);
}Accessible spacing and text settings
Leave enough space around controls for touch input. Aim for touch targets around 44 pixels or larger. Do not use spacing alone to communicate meaning; headings, labels, and borders can reinforce the groups.
Users may increase line height, letter spacing, or word spacing. Test that content still fits when these settings change.
Checking your groupings with the squint test
Blur your eyes or squint at the page. Related items should appear as visual clusters. If two cards merge, reduce their internal contrast or increase the gap between them. If one card breaks apart, reduce its internal spacing or add a heading.
Exercise
Which spacing should usually be larger: the gap between a label and input, or the gap between two form groups?
Show answer
The gap between form groups should be larger because the groups represent weaker relationships.