Sub topic 6
Layout, alignment and spacing
This chapter assumes you know the CSS box model, can open DevTools, and have a design frame open with spacing values you can read. It covers the first detailed check in a visual QA pass: where things sit and how far apart they are.
What to check
Layout problems come in three kinds:
- Structure: elements are in the wrong order, wrong column, or wrong container.
- Alignment: edges that should line up (left edges of a heading and its paragraph, tops of cards in a row) do not.
- Spacing: margins, padding and gaps differ from the design.
Check them in that order. A structure error changes every measurement after it, so there is no point measuring spacing inside the wrong container.
Find the design's spacing system
Most designs use a scale, often multiples of 4 or 8 px (4, 8, 16, 24, 32, 48). Write the scale down first. A measured value of 17 px or 22 px is a strong hint that the build is off, because the design almost certainly does not contain it.
Measure in DevTools
Select an element in the Elements panel and hover it. DevTools shades margin, border, padding and content in different colors. The Computed tab shows the exact numbers in a box-model diagram. To measure the gap between two elements, hold Alt (Option on macOS) with one element selected and hover the other.
To see every box at once, paste this into the console or a temporary stylesheet:
* {
outline: 1px solid rgba(255, 0, 0, 0.4);
}Outlines do not affect layout, so nothing moves. Misaligned edges and surprise gaps show up as doubled or offset lines.
Common causes
| Symptom | Likely cause |
|---|---|
| Gap is larger than designed | Margins on both neighbors add up, or a default browser margin was not reset |
| Gap is smaller than designed | Vertical margins collapsed into one |
| Element is 16 px too wide | `box-sizing: content-box` with padding added on top of the width |
| Cards in a row have uneven heights | The row uses floats or fixed heights instead of flex or grid stretch |
| Content hugs the screen edge | The container has no horizontal padding at that width |
A worked check
Suppose the design shows a card with 24 px padding and a 16 px gap to the next card. In DevTools you find 24 px padding but a 20 px gap. Look at the Styles pane. If you find margin-bottom: 20px on the card, the fix is a one-line change, and you can record the element, the measured value and the expected value in your report.
Exercise
The design shows 32 px between a section heading and the grid below it. DevTools reports 48 px. The heading has margin-bottom: 16px and the grid has margin-top: 32px. What is happening, and what would you report?
Show answer
The two margins are adding up to 48 px, so the gap is not collapsing here. This usually happens when the elements are flex or grid items, or sit in a container that blocks margin collapse. Report the measured 48 px against the expected 32 px, name both selectors, and suggest keeping the spacing on one element only.