Sub topic 7
Calculating column and module sizes
A grid on a layout tool is only reliable if its numbers add up. This chapter turns the grid's parts into arithmetic. You will derive the width of a column, the width of an element that spans several columns, and the height of a module. You will also fit the whole grid to the baseline so no space is left over.
This chapter assumes you know these terms from earlier chapters: margin, gutter, column, row, module, and leading (the distance from one text baseline to the next). It also assumes you can read short Python and CSS snippets.
The one equation behind every grid
A grid divides a fixed length into equal cells separated by equal gaps. The same logic applies to width and height. Take an axis of total length T, with margins m on both sides, n cells, and a gutter g between neighbours:
usable = T - 2m
cell = (usable - (n - 1) * g) / nThere are n cells but only n - 1 gutters, because gutters exist only between cells. Forgetting this is the most common error in hand-built grids.
You can also run the equation backwards. Given a column width you want, the page width follows:
T = 2m + n * cell + (n - 1) * gUse this form when the column is fixed by the type, as in the next chapter section on measure, and the page or container must adapt.
Column width in a worked example
The function below applies the equation to the horizontal axis.
def column_width(page_width, margin, gutter, columns):
"""Width of one column when both side margins are equal."""
usable = page_width - 2 * margin
return (usable - (columns - 1) * gutter) / columns
# A4 portrait is 210 mm wide: 20 mm margins, 5 mm gutters, 4 columns
print(column_width(210, 20, 5, 4)) # 38.75Check the result by adding everything back up: 2 × 20 (margins) + 4 × 38.75 (columns) + 3 × 5 (gutters) = 40 + 155 + 15 = 210. Always run this check. If the sum does not equal the page width, the grid will not close on the right edge.
Spanning and positioning
Content rarely sits in a single column. An element that spans k columns covers k column widths and also the k - 1 gutters inside it. Its left edge sits at the margin plus i column-and-gutter steps, where i counts columns from zero.
def span_width(col, gutter, span):
"""Width of an element that spans `span` columns, gutters included."""
return span * col + (span - 1) * gutter
def column_x(margin, col, gutter, index):
"""Left edge of the column at zero-based `index`."""
return margin + index * (col + gutter)
col = 38.75
print(span_width(col, 5, 1)) # 38.75
print(span_width(col, 5, 2)) # 82.5
print(span_width(col, 5, 4)) # 170.0, the full usable width
print(column_x(20, col, 5, 2)) # 107.5Two results are worth remembering. A span of all columns equals the usable width, which confirms the grid is consistent. A two-column span is wider than twice a column (82.5 mm against 77.5 mm) because it swallows one gutter.
| Span | Width (mm) | Gutters inside |
|---|---|---|
| 1 column | 38.75 | 0 |
| 2 columns | 82.50 | 1 |
| 3 columns | 126.25 | 2 |
| 4 columns | 170.00 | 3 |
Module height: tie rows to the baseline
A modular grid adds rows, and the row height is where most grids go wrong. If you divide the usable height by the row count and accept a fractional result, module edges fall between text baselines. Text in neighbouring modules then fails to align across the page.
The reliable method is to work in text lines, not in millimetres or points:
- Compute the usable height and divide by the leading to get the number of baseline lines.
- Make the gutter between rows exactly one line, so a row gutter keeps text on the baseline grid.
- Choose rows and lines per module so that
rows × lines + (rows − 1) = total lines.
Rearranging gives rows × (lines + 1) = total lines + 1. So the row counts that work are the divisors of total lines + 1.
def module_options(total_lines, max_rows=12):
"""Row counts that fit `total_lines` text lines when one blank line
separates modules. Returns (rows, lines_per_module) pairs."""
options = []
for rows in range(2, max_rows + 1):
per_row, remainder = divmod(total_lines + 1, rows)
if remainder == 0:
options.append((rows, per_row - 1))
return options
PAGE_W, PAGE_H = 595, 842 # A4 in points, rounded
MARGIN = 36
LEADING = 14
usable_h = PAGE_H - 2 * MARGIN # 770
total_lines = usable_h // LEADING # 55
leftover = usable_h - total_lines * LEADING
print(total_lines, leftover) # 55 0
print(module_options(total_lines)) # [(2, 27), (4, 13), (7, 7), (8, 6)]
rows, lines_per = 8, 6
module_h = lines_per * LEADING # 84
check = rows * module_h + (rows - 1) * LEADING
print(module_h, check == usable_h) # 84 True
usable_w = PAGE_W - 2 * MARGIN # 523
module_w = (usable_w - 3 * LEADING) / 4
print(module_w) # 120.25Here 55 lines plus one gives 56, whose divisors in range are 2, 4, 7 and 8. So the page supports 2, 4, 7 or 8 rows. The 8-row option gives modules 120.25 pt wide and 84 pt tall, separated by 14 pt gutters in both directions. Using the leading as the gutter in both directions also makes the gaps look the same horizontally and vertically.
The bottom check confirms the rows fill the usable height exactly: 8 × 84 + 7 × 14 = 770.
When the numbers do not divide
Real pages seldom work out as neatly as 770 ÷ 14. You have three honest options, and they differ in what they sacrifice.
- Adjust the margins. Change the top or bottom margin by a few points until the lines fit. Margins are the most forgiving element because readers rarely notice a 2 pt difference there.
- Change the leading. Move from 14 pt to 13.5 pt or 14.5 pt. This changes the text colour slightly, so check it against the type chapter's guidance.
- Accept a short last row. Let the final module run short. This is acceptable for flowing text, but it breaks a strict modular layout.
Do not push the remainder into the gutters. Unequal gutters are visible and undermine the grid's purpose.
Rounding on screens
In CSS, you usually let the browser do this arithmetic. The fr unit shares the free space after gutters, which is the same equation:
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
column-gap: 5mm;
max-width: calc(210mm - 40mm);
}At a 170 mm container width, each 1fr track resolves to 38.75 mm, matching the Python result. The browser may place track edges at fractional device pixels. This is normally invisible, but it can cause hairline gaps between adjacent images that have background colours. If you need whole-pixel columns, choose a container width where (width − (n − 1) × gutter) is divisible by n. For example, a 4-column grid with 24 px gutters and a 984 px container gives (984 − 72) ÷ 4 = 228 px columns.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Content ends short of the right margin | Subtracted `n` gutters instead of `n − 1` | Recount gutters |
| Two-column image is narrower than expected | Used `2 × col` and forgot the inner gutter | Use `span_width` |
| Text in side-by-side modules drifts out of line | Module height is not a whole number of lines | Compute rows from lines, not points |
| Last row is cut off or short | Remainder left over after rounding | Adjust margins |
| Page-size units mixed | Mixed millimetres and points | Convert once, at the start |
Unit mixing deserves care: 1 pt is 0.3528 mm, so 14 pt is about 4.94 mm. Pick one unit for the whole calculation and convert only at the input and output.
Exercise
A page is 148 mm wide (A5). It has 12 mm margins on both sides, 4 mm gutters and 3 columns. What is the column width, and how wide is an element that spans two columns?
Show answer
Usable width = 148 − 2 × 12 = 124 mm. Column width = (124 − 2 × 4) / 3 = 116 / 3 ≈ 38.67 mm. A two-column span = 2 × 38.67 + 4 = 81.33 mm. Check: 3 × 38.67 + 2 × 4 = 124.
Exercise
Your page has 62 baseline lines of usable height and you want a gutter of one line between modules. Which row counts from 2 to 8 fit exactly, and how many lines is each module for each?
Show answer
62 + 1 = 63. The divisors of 63 between 2 and 8 are 3 and 7. With 3 rows each module has 63 / 3 − 1 = 20 lines. With 7 rows each module has 63 / 7 − 1 = 8 lines. Check: 3 × 20 + 2 = 62 and 7 × 8 + 6 = 62.