Sub topic 4
Drawing the Grid: Four Lines, Four Intersections
This chapter assumes you know what an image frame is, how width, height, and aspect ratio relate, and that you have seen a grid display in a camera or editor. The Python and CSS examples are optional. You can follow the chapter without running them.
The geometry of the thirds grid
The rule of thirds divides the frame into nine equal cells. You get them with two vertical lines and two horizontal lines. Each pair splits its dimension into three equal parts.
- Vertical lines sit at 1/3 and 2/3 of the width.
- Horizontal lines sit at 1/3 and 2/3 of the height.
- Every vertical line crosses every horizontal line, which gives 2 × 2 = 4 intersections.
Those four intersections are often called power points. They are the spots where a subject tends to get the most attention without sitting dead center.
Computing the grid in Python
A grid is just arithmetic on the frame size. The function below returns the line positions and the intersections for any width and height.
def thirds_grid(width, height):
xs = [width / 3, 2 * width / 3]
ys = [height / 3, 2 * height / 3]
intersections = [(x, y) for x in xs for y in ys]
return xs, ys, intersections
xs, ys, points = thirds_grid(1920, 1080)
print(xs) # [640.0, 1280.0]
print(ys) # [360.0, 720.0]
print(points) # [(640.0, 360.0), (640.0, 720.0), (1280.0, 360.0), (1280.0, 720.0)]For a 1920 by 1080 frame, the vertical lines fall at x = 640 and x = 1280. The horizontal lines fall at y = 360 and y = 720. The four intersections are the combinations of those values.
Notice that the cells are not square unless the frame is. A 16:9 frame gives cells of 640 by 360 pixels. The grid scales with the frame, so it always matches the aspect ratio.
Drawing the grid over an image in CSS
For a web page, you can place a nine-cell CSS grid over an image. One div per cell gives you the lines through their borders. The overlay does not change the image and does not block clicks.
.photo {
position: relative;
}
.thirds-overlay {
position: absolute;
inset: 0;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
pointer-events: none;
}
.thirds-overlay > div {
border: 1px solid rgba(255, 255, 255, 0.6);
}Here is how it works:
.photoisposition: relative, so the overlay can be positioned against it.inset: 0stretches the overlay across the whole image.repeat(3, 1fr)creates three equal columns and three equal rows.pointer-events: nonelets mouse clicks pass through to the image underneath.
The markup needs nine div elements inside .thirds-overlay. Each cell draws its own border, so inner lines appear where two borders meet.
Common errors
- Dividing by two instead of three. Halving a dimension gives the center line, not a thirds line.
- Mixing up lines and cells. Three cells need only two lines. Drawing three lines per direction gives four cells.
- Using fixed pixel values. Hard-coded positions break when the image size changes. Compute them from the width and height.
- Forgetting the aspect ratio when cropping. If you crop to a new ratio, redraw the grid on the cropped frame. The old grid no longer applies.
- Placing the overlay without
position. Without a positioned parent, an absolutely placed overlay can cover the wrong area.
Summary of the numbers
| Item | Count | Position |
|---|---|---|
| Vertical lines | 2 | 1/3 and 2/3 of width |
| Horizontal lines | 2 | 1/3 and 2/3 of height |
| Cells | 9 | Each is W/3 by H/3 |
| Intersections | 4 | Every x line paired with every y line |
Exercise
A frame is 3000 by 2000 pixels. Where are the two vertical lines, the two horizontal lines, and the upper-right intersection? Assume y grows downward from the top edge.
Show answer
The vertical lines are at x = 1000 and x = 2000. The horizontal lines are at y = 666.67 (about 667) and y = 1333.33 (about 1333). The upper-right intersection is where the right vertical line meets the upper horizontal line, at about (2000, 667).
Exercise
Why does the CSS overlay use pointer-events: none?
Show answer
The overlay sits on top of the image. Without this property it would capture mouse clicks and hovers, so the image and anything under it would stop responding. With it, input passes through to the elements underneath.