Sub topic 4
Specifying layout, grid, and spacing
This chapter covers how to turn the positions and gaps in a design into rules a developer can build. It assumes you can use frames, auto layout (or stacks), and components in a design tool. It also assumes you know basic CSS: the box model, flexbox, and the idea of a custom property. Design tokens from the previous chapter supply the named spacing values used here.
Why layout specs fail
A developer who opens a design sees pixel positions. A browser, though, has no fixed canvas. The page width changes, the text length changes, and the content may be translated into a longer language. A spec that only says "this card is 312 px wide, 48 px from the left" describes one screenshot. It does not say what should happen at any other width.
Good layout specs describe relationships and rules instead of coordinates:
- Which container holds the content, and how wide can it grow?
- How many columns does the grid have, and how big are the gutters?
- Which gaps are fixed and which stretch?
- Which elements keep their size and which fill the space that is left?
The goal is that two developers reading the same spec produce the same layout, without measuring the mockup.
The layout container
Start with the outermost rules. Every page has a container, and the spec should state three numbers for it:
- The maximum content width, for example 1200 px.
- The side margin (the space between the viewport edge and the content) at each breakpoint.
- How the container is aligned when the viewport is wider than the maximum, usually centered.
A matching CSS pattern looks like this:
:root {
--space-3: 16px;
--space-4: 24px;
--container-max: 1200px;
}
.container {
box-sizing: border-box;
width: 100%;
max-width: var(--container-max);
margin-inline: auto;
padding-inline: var(--space-3);
}
@media (min-width: 960px) {
.container {
padding-inline: var(--space-4);
}
}Note that the spec must say whether the 1200 px includes the side padding. In CSS with box-sizing: border-box, max-width: 1200px includes padding, so the content is narrower than 1200 px. If the designer meant 1200 px of content, the developer needs to know that. State it in words: "content width 1200 px, excluding margins."
Grid specs
Most web layouts sit on a column grid. A grid spec has four parts:
| Part | What to state | Example |
|---|---|---|
| Columns | Number of columns at each breakpoint | 4 on mobile, 8 on tablet, 12 on desktop |
| Gutter | Fixed gap between columns | 16 px mobile, 24 px desktop |
| Margin | Space outside the first and last column | 16 px mobile, 24 px desktop |
| Column width | Fluid or fixed | Fluid, equal fractions |
On the web, columns are almost always fluid. The designer's 12 columns of 76 px are a result of one viewport width, not a requirement. The spec should say "12 equal fluid columns, 24 px gutter" and let the browser compute the width.
CSS Grid expresses this directly:
.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: var(--space-4);
}
.card--wide {
grid-column: span 8;
}
.card--narrow {
grid-column: span 4;
}Using minmax(0, 1fr) instead of plain 1fr stops long words or wide images from forcing a column to grow beyond its share. That is a common source of "the layout breaks with real content" bugs, and it is worth a note in the spec when a column holds user-generated text.
In the design file, annotate each block with its span, not its pixel width. "Spans 8 of 12" tells the developer more than "736 px".
Spacing: use a scale, not measurements
Spacing is the largest source of small mismatches. A designer nudges an element 5 px to look right, and the developer, measuring, writes margin-top: 5px. After a few screens, the page has dozens of unrelated values.
The fix is a spacing scale: a short list of allowed values, each with a name. The tokens from the previous chapter already provide one:
| Token | Value | Typical use |
|---|---|---|
| `space-1` | 4 px | Icon-to-label gap |
| `space-2` | 8 px | Related items inside a component |
| `space-3` | 16 px | Padding inside cards, gaps between fields |
| `space-4` | 24 px | Gutters, gaps between groups |
| `space-6` | 48 px | Gaps between page sections |
Many teams base the scale on a 4 px or 8 px unit so that values stay consistent and divide evenly. The scale itself matters less than the rule that every gap in the design uses one of its values.
In the spec, write the token name, not only the number. "Gap: space-3" survives a later change of the scale, while "gap: 16 px" has to be found and edited everywhere.
Padding, margin, and gap are different jobs
Designers often draw a distance between two boxes and call it "spacing". Developers must choose a CSS mechanism, and the choice affects how the layout behaves.
- Padding is space inside an element's border. Use it for the distance between a component's edge and its content.
- Gap is space between siblings in a flex or grid container. Use it for repeated items, such as a row of cards or a list of form fields.
- Margin is space outside an element. Use it sparingly, for the space between sections or for a one-off exception.
Prefer gap for repeated items, because it adds space only between items. Margins on each item create extra space at the end of the row unless someone remembers to remove it.
.card-row {
display: flex;
flex-wrap: wrap;
gap: var(--space-4);
}
.card {
padding: var(--space-3);
flex: 1 1 280px;
}When you annotate, say which kind of space you mean: "padding inside card: space-3" and "gap between cards: space-4".
Sizing rules: fixed, fluid, and content-based
Each element in a layout has a width behavior. Name it for every element where it is not obvious.
- Fixed: the size never changes, such as a 24 px icon or a 40 px avatar.
- Fill: the element takes the space available, such as a text column or a search input.
- Hug (content-based): the element is as large as its content, such as a button with a label.
- Constrained: the element is fluid but within limits, such as "fills the row, minimum 280 px, maximum 480 px".
Design tools with auto layout already use words like "fixed", "fill", and "hug". Use them in the spec. They map to CSS reasonably well:
| Design term | Typical CSS |
|---|---|
| Fixed | `width: 40px; flex: none;` |
| Fill | `flex: 1;` or `width: 100%` |
| Hug | `width: auto;` or `width: fit-content;` |
| Constrained | `min-width` and `max-width`, often with `flex: 1 1 280px` |
Minimum and maximum values are the part most often missing. Without a maximum, a paragraph can stretch across a very wide screen and become hard to read. A common note is "body text column max 65 characters", which developers can set with max-width: 65ch.
Alignment and ordering
Specify how elements line up with each other. State horizontal and vertical alignment separately, for example "items aligned to the top, text left-aligned". In a flex row, the difference between align-items: flex-start and center changes how cards of unequal height look.
Also state the reading order if it differs from the visual order. When the design moves an image above the text on mobile, the HTML order should still make sense for keyboard and screen reader users. The accessibility chapter returns to this.
Edge cases to document
A layout is only fully specified once it covers content that is not in the mockup.
- Long text. Say whether a title wraps, truncates with an ellipsis, or grows the container. "Card title: wraps up to 2 lines, then truncates" is a complete rule.
- Short or missing content. If a card has no image, does the space collapse or stay reserved?
- Many items. Does a row of tags wrap, scroll, or collapse into a "+3" label?
- Different image ratios. State the aspect ratio and the crop behavior, for example
aspect-ratio: 16 / 9; object-fit: cover. - Translated text. Some languages run 30 percent longer than English. Check that buttons and navigation do not overflow.
A short table in the design file for each component keeps these rules close to the visuals:
| Case | Behavior |
|---|---|
| Title longer than 2 lines | Truncate with ellipsis |
| No image | Hide the media block, no reserved space |
| More than 5 tags | Wrap to a new line |
| Image ratio | 16:9, cropped to fill |
A worked example
Take a product card. A weak spec says: "Card 320 × 400. Image 320 × 180. Title 16 px below image. 20 px padding."
A stronger spec says:
- Card width: fills its grid cell, minimum 280 px.
- Image: aspect ratio 16:9, cropped to fill, flush with the card's top edge.
- Content padding: space-3 on all sides.
- Gap between title and price: space-2.
- Title: wraps up to 2 lines, then truncates.
- Cards in a row: gap space-4, wrap to a new line when less than 280 px remains.
The matching CSS follows the spec line by line:
.product-card {
display: flex;
flex-direction: column;
flex: 1 1 280px;
min-width: 0;
}
.product-card__image {
aspect-ratio: 16 / 9;
width: 100%;
object-fit: cover;
}
.product-card__body {
display: flex;
flex-direction: column;
gap: var(--space-2);
padding: var(--space-3);
}
.product-card__title {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}Each line of the second spec has a direct counterpart in the code. That mapping is the test of a good layout spec.
Common errors
- Giving pixel positions (x and y offsets) instead of relationships.
- Writing raw numbers where a token exists.
- Leaving out minimum and maximum widths.
- Mixing up padding, margin, and gap.
- Showing only one content length, so no behavior is defined for long or empty content.
- Listing a column width in pixels for a fluid grid.
Exercise
A designer's note says "Card row: each card 360 px wide, 20 px between cards." Rewrite it as a layout spec a developer can implement across screen widths. Include sizing behavior, the gap, and one edge case, and name the CSS mechanism you would expect for the gap.
Show answer
One acceptable answer: "Cards fill their available width with a minimum of 280 px and a maximum of 360 px. Cards sit in a flex row that wraps to a new line when less than 280 px remains. Gap between cards: space-4 (24 px, on the scale), set with the CSS gap property on the row container, not with margins on each card. Edge case: a card title wraps up to 2 lines and then truncates with an ellipsis." The 20 px value is off the spacing scale, so it should be replaced with the nearest token and the change flagged to the designer.
Exercise
A page container has max-width: 1200px, padding-inline: 24px, and box-sizing: border-box. How wide is the content area on a 1440 px viewport, and what should the spec say if the designer wanted 1200 px of content?
Show answer
The content area is 1152 px wide (1200 minus 24 px of padding on each side). If the designer wanted 1200 px of content, the spec should say "content width 1200 px, excluding margins", and the developer would set the maximum width to 1248 px, or apply the padding differently.