A row of chairs at a wedding reception looks tidy even when the chairs are different colours, as long as they all line up. Move one chair a hand's width forward, and every guest who walks past notices. Our eyes are very good at catching what isn't straight.
That's why alignment matters: elements share one line, whether a left edge, a centre or a right edge. Its partner is optical alignment: a small correction that makes something look aligned to the eye, even though the numbers aren't exactly aligned. The correction is needed because the eye judges shapes, not the boxes around them.
One edge for everything
The Terrace Room card holds the room name, the price, the night buttons (for choosing how many nights), the book button and the cancellation note. In the messy version, each element starts in a slightly different place because of these three CSS rules. margin-left is the left margin, and text-align: center centres the text:
.price { margin-left: 8px; } /* the price indented 8px */
.nights { margin-left: 4px; } /* the night buttons indented 4px */
.note { text-align: center; } /* the note centred */
That gives 4 different alignment points (places where an element starts) on the left:
- the card's content edge: the room name and the book button,
- 8px further right: the price,
- 4px further right: the night buttons,
- somewhere else entirely: the note, whose position depends on how long its sentence is.
Delete those three rules, and everything starts at the card's content edge, right after the card's 24px padding. You can draw one straight line from top to bottom that touches the start of every element.
That straight line is called an invisible line: it isn't drawn, but you feel it. The rule is simple: the fewer different alignment points, the tidier the page. When every element uses the same grid and the same padding, these lines appear by themselves.
Left, centre or right
- Left-aligned for text people read, such as the room description and reviews. English reads left to right. Left-aligned text makes every new line start in the same place, so the eye doesn't have to search for the start of the line.
- Centred only for short text: a one-line heading, a button label. A centred paragraph makes every line start in a different place.
- Right-aligned for numbers in a column, such as the three room prices in a table. With
font-variant-numeric: tabular-numsevery digit is made the same width, so the dollars and cents on each row line up. In this code,.priceis the price column of that table:
.price { text-align: right; font-variant-numeric: tabular-nums; }
Optical alignment: the play triangle
On the Terrace Room photo there's a round play button for the room video. The triangle on that button is 18px wide. The triangle is drawn inside a box, and that box sits exactly in the middle of the circle. Take a guess: does the triangle look centred, a bit to the left, or a bit to the right?
The box around the triangle really is in the middle of the circle. But a triangle pointing right has its centroid (its centre of mass) a third of its width from the flat side, not half. For an 18px wide triangle:
- the middle of its box: 18 / 2 = 9px from the flat side
- its centroid: 18 / 3 = 6px from the flat side
- the difference: 9 - 6 = 3px, or one sixth of the triangle's width
The eye tends to read that centroid as the triangle's centre. So a mathematically centred triangle looks a bit to the left. The fix: move the triangle right by that difference. In this code, .play is the round button. The triangle is made with .play::before, an extra element that CSS creates inside the button:
.play {
width: 48px; height: 48px; border-radius: 50%;
display: grid; place-items: center;
background: #15803d;
}
.play::before {
content: "";
width: 18px; height: 20px;
background: #fff;
clip-path: polygon(0 0, 100% 50%, 0 100%); /* a triangle pointing right */
transform: translateX(3px); /* 18 / 6 = 3px, the optical fix */
}
border-radius: 50%makes the button round.display: grid; place-items: centercentres the triangle's box in the circle, mathematically.clip-path: polygon(...)cuts the 18 × 20px box into a triangle with its point on the right.transform: translateX(3px)moves the triangle without changing the layout around it. The 3px from the arithmetic is only a first estimate. The final decision is still made by eye, with the 0px and 3px versions side by side.
Optical alignment: the hanging quotation mark
Under the card is Mr. Hadi's review, which opens with a quotation mark. Written normally, the quotation mark stands on the left edge, and the review's first letter only starts a few pixels to its right. The eye reads that line as indented compared with the "What guests say" heading above it. The fix: hang the mark outside the left edge. In this code, the quotation mark is also made with ::before:
.quote { position: relative; }
.quote::before {
content: "\201C"; /* the opening quotation mark */
position: absolute;
right: 100%; /* its right side touches the edge */
}
.quote is Mr. Hadi's review. With position: relative on .quote, the quotation mark's position is measured from the review's box. position: absolute takes the quotation mark out of the line of text, and right: 100% puts its right side exactly on the left edge of .quote. Now the quotation mark sits outside, and the review's first letter, "T", stands exactly on the same line as the heading above. CSS has a property for this, hanging-punctuation: first, but only Safari supports it today. So the ::before approach above is safer.
The squint test
A quick way to find what's out of line: the squint test. Narrow your eyes (or step back from the screen) until the text blurs. The letters become unreadable. Only the blocks and their edges remain, and a block that sticks out is easy to see right away. In the browser you can copy this effect with filter: blur(3px) (it blurs the page) on body through DevTools (the developer tools built into the browser).
⚠️ What makes a page look slightly crooked
- Extra margin or padding on one element. A
margin-left: 8pxon the price alone adds one more alignment point. - Centred paragraphs. Nice for a one-line heading, tiring for a three-line description.
- Trusting
align-items: centercompletely. This centring rule centres boxes, not the shape you see. Triangle icons, letters with descenders (the tails of g and y) and quotation marks often need a 1 to 3px correction. - Optical fixes without a comparison. Move it by the arithmetic, then put the old and new versions side by side. If you can't feel the difference, don't correct it.
TL;DR: Alignment means elements share the same line. The fewer different alignment points (places where elements start), the tidier. Left-align text people read, centre short text, and right-align numbers in a column, with tabular-nums so every digit is the same width. Optical alignment is a small correction so things look aligned to the eye. A mathematically centred triangle on a play button looks a bit to the left, because its centroid (centre of mass) sits at a third of its width, not at half. So move the triangle right by a sixth of its width: for an 18px wide triangle, that is 3px. Hang the opening quotation mark outside the left edge of the text (make it with ::before, an extra element from CSS, then move it out with right: 100%) so the first letter lines up with the heading. To find what's out of line, use the squint test: narrow your eyes until the text blurs, then look at the blocks.
⚡ One edge for everything
Meaning: Alignment means elements share the same line. The fewer different alignment points (places where elements start), the tidier the page. A margin-left: 8px (an 8px left margin) on the room price alone already adds one more alignment point.
⚡ Numbers in a column align right
Write:
.price { text-align: right; font-variant-numeric: tabular-nums; }
Meaning: .price is the column of room prices, and text-align: right right-aligns its text. Left-align text people read, centre only short text, right-align numbers in a column. tabular-nums makes every digit the same width, so the dollars and cents on each row line up.
⚡ A triangle that looks off-centre
Write:
.play::before {
width: 18px; height: 20px;
clip-path: polygon(0 0, 100% 50%, 0 100%); /* a triangle pointing right */
transform: translateX(3px); /* 18 / 6 = 3px, the optical fix */
}
Guess first: the triangle on a round play button is drawn inside an 18px wide box. That box sits exactly in the middle of the circle. Where does the triangle look like it is?
A bit to the left. A triangle's centroid (its centre of mass) sits a third of its width from the flat side (6px), not half (9px), and the eye reads that point as its centre.
Meaning: Optical alignment is a small correction so things look aligned to the eye. In this code, .play::before is the triangle inside the play button (.play). clip-path cuts its box into a triangle, and translateX(3px) moves it right by a sixth of its width. Then compare the old and new versions side by side.
⚡ The squint test
Meaning: The squint test: narrow your eyes until the text blurs. The letters become unreadable, only the blocks and their edges remain, and a block that sticks out is easy to see right away. In the browser, copy it with filter: blur(3px), which blurs the page.