T2 CSS · Lesson 4
Typography
Font families, stacks and web fonts; size, weight and slant, and why meaning stays in the HTML; line height, letter spacing and line length; aligning, decorating and transforming text; and links in all their states.
- choose a font family with fallbacks, and say how web fonts are loaded
- set font sizes, weights and italics, and say why the HTML tags still matter
- space lines and letters for easy reading, with a unitless line height
- align, transform and decorate text, knowing what each changes and what it does not
- style links in all their states, in the right order, without losing what makes them usable
The back cover
Text is most of what a page holds, and the look of its letters decides whether it gets read. Printers learned this over centuries. They chose letter designs, set the spaces between lines and letters, and kept lines from running too long.
These pages show how CSS chooses the letters, from a list of fonts down to a font sent with the page. They cover size, weight and slant, and why meaning stays in the HTML. They show the space between lines and letters, aligning and decorating text, and links that look and act like links in every state.
Before you start
The story: Back to the type cases
Meher looks at your Fair notice and frowns. "The colours are right and the box fits," she says. "But the letters are cramped, the lines are jammed together, and nobody can tell the links from the text. Take it to Ilse. Letters were her trade before they were ours."
In the print shop, Old Ilse pulls out a wooden case of metal letters, then another, then a third. "One family of letters, many weights," she says. "And these thin strips of lead go between the lines. Too few, and the eye loses its place."
She hands you a stick to set type in. "Show me a notice people can read across a square."
Most of any page is text, so how the text looks decides whether the page is read at all. Typography is the craft of setting text: choosing the letters, their size and weight, the space between lines, and how links stand out. It began in print shops like Old Ilse's, and many of its words still come from there. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Inheritance. | A colour set on body passes to every paragraph. | Fonts are inherited too, so one rule on body sets the whole page. |
| rem follows the reader. | 1.5rem is 24px, or 30px if the reader set 20px. | Every font size here is in rem. |
| The browser's defaults. | An h1 is 32px and bold; a link is blue and underlined. | Typography changes those defaults on purpose. |
| strong and em mean something. | Importance and stress, from the HTML step. | font-weight and font-style change only the look. |
| Contrast and colour alone. | 4.5 to 1; never only a colour as a sign. | A link must not be told from text by colour alone. |
| The focus ring. | Keyboard users need to see where they are. | Link styles must keep a clear focus style. |
| Rules that tie win by order. | Of two equal rules, the later wins. | The four link states must come in the right order. |
| A print shop's type. | Cases of metal letters, in families and weights. | Font families and weights are the same idea on a screen. |
| Checks read computed styles. | getComputedStyle(). | Every font, size and spacing here is read back from the browser. |
By the end of this lesson you will be able to:
- choose a font family with fallbacks, and say how web fonts are loaded;
- set font sizes, weights and italics, and say why the HTML tags still matter;
- space lines and letters for easy reading, with a unitless line height;
- align, transform and decorate text, knowing what each changes and what it does not;
- style links in all their states, in the right order, without losing what makes them usable.
In one line
Typography sets the letters, their size, weight and spacing, and how links stand out; you need inheritance, rem and the HTML step's rules on contrast and focus.
Choosing the letters: font-family
At a glance
A font family is one design of letters in all its sizes and weights, such as Arial or Georgia. The font-family property chooses one, as a list of wishes in order: the browser uses the first family the device has.
The list always ends in a generic family, such as serif or sans-serif, so that something sensible shows everywhere. A family the device does not have can be sent with the page as a web font, through @font-face, best kept to two families on a site.
Take a guess: If the font-family list is Noto Sans, Arial, sans-serif and the device has only Arial, which font does the text use?
Old Ilse's print shop has cases of letters in different designs: plain upright ones for notices, curly ones for wedding cards, square ones for price lists. Each design, in all its sizes and weights, is a font family, or typeface. The font-family property chooses one.
<!DOCTYPE html>
<style>
body { font-family: "Noto Sans", Arial, sans-serif; }
</style>
<h1>Asha Stores</h1>
<p>Fresh vegetables every morning.</p>
Checked in Chrome
- the paragraph inherits the family list from body
- and so does the heading: one rule on body sets the whole page
Read the value as a list of wishes, in order: "Noto Sans if this device has it; if not, Arial; if not, any sans-serif font". A name with spaces goes in quotes. This list is called a font stack. The browser uses the first one it can find, so the page still looks reasonable on a device without your first choice.
Notice what the check reads back: the whole list, exactly as written. The browser does not tell a script which font in the list it actually used; DevTools does, as the debugging corner shows.
Generic families: the last safe wish
The last item should always be a generic family, a kind of font every device has some version of.
| Generic family | Looks like | Good for |
|---|---|---|
serif | small strokes at the ends of letters, like a newspaper | long reading in print, formal pages |
sans-serif | plain letters with no end strokes (sans is French for without) | most screens, menus, notices |
monospace | every letter the same width, like a typewriter | code, tables of numbers |
system-ui | the device's own interface font | apps that should feel at home on each phone |
<!DOCTYPE html>
<style>
.notice { font-family: Georgia, serif; }
code { font-family: "DejaVu Sans Mono", monospace; }
</style>
<p class="notice">Admissions close on 15 June.</p>
<p>Write <code>margin: 0 auto</code> to centre a box.</p>
Checked in Chrome
- a serif stack for the notice
- a monospace stack for the code
A memory aid
Wish list, then a safe word. Name your favourite fonts first, and always end with serif, sans-serif or monospace, so every device has something to fall back on.
Web fonts: sending the font with the page
A font stack can only use fonts the visitor's device already has. To use a particular font everywhere, the site can send the font file itself, as a web font. The CSS rule @font-face gives a font file a family name, which font-family can then use.
On a real site
@font-face {
font-family: "Ashoka";
src: url("/fonts/ashoka.woff2") format("woff2");
font-display: swap;
}
body { font-family: "Ashoka", sans-serif; }
The src is the font file's path, in WOFF2, the small format made for the web. font-display: swap shows the text at once in a fallback font and swaps to the web font when it arrives, so the visitor never waits on a blank page. Font services such as Google Fonts write this for you: you link their stylesheet in the head, then name the family in your stack.
Too many fonts
Every web font is a file to download, and each weight and style of it is often another file. A page with four families in four weights asks for sixteen files, and on a slow phone the text jumps around as each arrives. Two families, one for headings and one for text, are almost always enough.
Worked example 1
A school's site wants headings in Georgia and body text in a plain sans-serif font, with safe fallbacks. Write the two rules.
What we have: a wish for Georgia in headings and a sans-serif for text
What we need: two font stacks, each ending in a generic family
The plan: body gets the sans-serif stack, which every element inherits; h1 and h2 get the serif stack
<!DOCTYPE html>
<style>
body { font-family: Arial, Helvetica, sans-serif; }
h1, h2 { font-family: Georgia, "Times New Roman", serif; }
</style>
<h1>Green Valley School</h1>
<p>Admissions are open.</p>
Checked in Chrome
- the text has the sans-serif stack
- the heading has the serif stack, with a quoted name
body { font-family: Arial, Helvetica, sans-serif; } and h1, h2 { font-family: Georgia, "Times New Roman", serif; }
Check: each stack ends in a generic family, and the heading rule beats the inherited one because it chooses the heading itself.
Worked example 2
A railway timetable shows train numbers and times in a column, and the digits of each row should sit exactly under the row above. Which generic family helps, and why? Prove it in the browser.
What we have: a column of numbers that must line up
What we need: a family whose characters all take the same width, and a measurement that shows it
The plan: monospace gives every character the same width; measure a narrow word and a wide word in each family
<!DOCTYPE html>
<style>
.mono { font-family: monospace; }
.serif { font-family: serif; }
</style>
<p><span class="mono a">iiii</span> <span class="mono b">mmmm</span></p>
<p><span class="serif a">iiii</span> <span class="serif b">mmmm</span></p>
Checked in Chrome
- in monospace, four i letters are exactly as wide as four m letters
- in serif, the i letters are much narrower
monospace: every character is the same width, so columns of numbers line up row under row.
Check: the browser measured iiii and mmmm as equal in monospace and very different in serif.
When you are stuck: font families
Write a wish list: your first choice, one or two common fonts, then a generic family.
A name with spaces needs quotes: "Noto Sans".
Not sure which font the browser used? DevTools' Computed tab lists the rendered fonts at the bottom.
Your turn
What should every font stack end with?Tap to reveal
- A kind of font every device has.
A generic family: serif, sans-serif or monospace.
Which kind of font has every letter the same width?Tap to reveal
- Like a typewriter.
monospace.
What does font-display: swap do for a web font?Tap to reveal
- Think of a slow phone.
It shows the text at once in a fallback font, then swaps in the web font when it arrives.
Did you know?
Web fonts: in, out and back again
Web fonts had a false start. The CSS2 standard, a W3C Recommendation of 12 May 1998, already had font descriptions and the @font-face rule. CSS 2.1 then removed the font descriptors and the @font-face declaration altogether. The rule came back in the CSS Fonts Level 3 standard, which lets a page link to fonts that are fetched and activated when needed.
Why it fits here: The @font-face rule that this topic uses to send a font with the page is the one that went away and came back.
Sources: W3C: CSS level 2 (1998); W3C: CSS level 2, Fonts (1998); W3C: CSS 2.1, Changes; W3C: CSS Fonts Level 3
Arial was made to fit Helvetica's widths
Arial was designed in 1982 by Robin Nicholas of Monotype, for low-resolution laser printers, and later developed with Patricia Saunders into a full typeface. It was made to match the character widths of Helvetica, so a line set in one takes the same space in the other. In 1992 Microsoft made Arial one of the four core fonts of Windows 3.1, announced as an alternative to Helvetica.
Why it fits here: This topic's example list names Arial as its second wish; this is where that font came from.
Sources: Microsoft Typography: Arial; Wikipedia: Arial
In one line
The font-family property takes a stack of wishes, the first available one used, always ending in a generic family such as sans-serif. Web fonts are sent with the page through @font-face, best kept to two families.
Size, weight and slant
At a glance
One family of letters comes in many forms: huge and heavy for a headline, small and regular for the story, slanted for a quotation. The font-size property sets the size, best in rem so that it follows the reader's chosen size. The font-weight property sets the thickness, as a number: 400 is normal and 700 is bold.
font-style: italic slants the letters. These properties change only the look. Meaning, such as importance or stress, belongs in the HTML, with strong and em, so that a screen reader can carry it too.
Take a guess: If a heading only needs to look heavier, should you use font-weight: 700 or the strong element?
A newspaper's front page uses one family of letters in many forms: the headline huge and heavy, the article small and regular, a quotation in slanted letters. Three properties make those forms: size, weight and style.
font-size, in rem
Lesson 2 settled the unit: font sizes in rem, so they follow the reader's chosen size. A simple, readable scale is enough for most pages.
| Use | Size | Pixels at the default 16px root |
|---|---|---|
| small print | 0.875rem | 14px |
| body text | 1rem | 16px |
| a sub-heading | 1.25rem | 20px |
| a section heading | 1.5rem | 24px |
| the page's main heading | 2rem | 32px |
CSS also has size words such as small, large and x-large, and larger and smaller, which step up or down from the parent by about 1.2. They work, but rem numbers are easier to keep consistent.
font-weight: how heavy the letters are
Weight is written as a number from 100 (very thin) to 900 (very heavy), in steps of 100. Two have names: normal is 400 and bold is 700.
<!DOCTYPE html>
<style>
.light { font-weight: 300; }
.regular { font-weight: normal; }
.heavy { font-weight: bold; }
</style>
<p class="light">300: light</p>
<p class="regular">400: regular</p>
<p class="heavy">700: bold</p>
Checked in Chrome
- 300, light
- normal is 400
- bold is 700
Whether a 300 looks lighter than a 400 depends on the font: many fonts come only in a few weights, and the browser uses the nearest one it has. The values bolder and lighter step up or down from the parent's weight.
font-style: slanted letters
font-style: italic uses the font's slanted letters. font-style: normal turns slant off, for example on an em that should not look slanted in a heading that is already italic.
<!DOCTYPE html>
<style>
.quote { font-style: italic; }
h1 { font-weight: normal; }
</style>
<h1>Asha Stores</h1>
<p class="quote">Best kaju katli in Pune.</p>
Checked in Chrome
- the quotation is italic
- the heading, bold by default, is made regular
Looks are not meaning
Lesson 2 of the HTML step said strong means important and em means stressed; bold and italic are only how they usually look. CSS changes the look and leaves the meaning alone. So: a heading made regular is still a heading, and screen readers still treat it as one. And a word made bold with CSS is not important to a screen reader; if it matters, use strong in the HTML.
| You want | Use | Not |
|---|---|---|
| a word that is important | <strong> in the HTML | only font-weight: bold |
| a heading to look lighter | font-weight: normal on the h1 | a big p, which is no longer a heading |
| a decorative slant, such as a tagline | font-style: italic | <em>, which would mean stress |
Worked example 3
A product card's price, in a span, should be bold and 1.25rem, and its old price should be regular, slanted and smaller. Neither is more important than the text, so no strong or em is wanted. Write the rules.
What we have: a price and an old price, both decorations rather than meaning
What we need: rules for weight, size and slant
The plan: the price: font-weight bold and font-size 1.25rem; the old price: font-weight normal, font-style italic, font-size 0.875rem
<!DOCTYPE html>
<style>
.price { font-weight: bold; font-size: 1.25rem; }
.old { font-weight: normal; font-style: italic; font-size: 0.875rem; }
</style>
<p>Toor dal <span class="price">₹180</span> <span class="old">was ₹200</span></p>
Checked in Chrome
- the price: bold, 20px
- the old price: italic, 14px
.price { font-weight: bold; font-size: 1.25rem; } and .old { font-weight: normal; font-style: italic; font-size: 0.875rem; }
Check: the browser reads back 700 and 20px for the price, italic and 14px for the old price.
When you are stuck: size, weight and slant
Size in rem; weight as a number (400 regular, 700 bold); slant with font-style: italic.
A weight that does not change: the font may not have it. Try 400 and 700 first.
Ask whether a look also means something; if it does, use strong or em in the HTML.
Your turn
What number is font-weight: bold?Tap to reveal
- normal is 400.
700.
How do you make a heading regular instead of bold?Tap to reveal
- One declaration on the heading.
font-weight: normal.
Does font-weight: bold tell a screen reader that a word is important?Tap to reveal
- CSS changes looks.
No: only strong in the HTML does.
Did you know?
Italic is named after Italy
Italic type is named after the country where it began. It was first used by an Italian printer, Aldo Manuzio, also known as Aldus Manutius, around 1500, and one of his early italics appears in a 1501 edition of Virgil. Italic letters are based on a stylised form of handwriting.
Why it fits here: This topic's font-style: italic chooses these letters, whose design began in that printer's books.
Sources: Wiktionary: italic; Wikipedia: Italic type
Any weight from 1 to 1000
Font weights are numbers. The CSS Fonts standard says the keyword normal is the same as 400 and bold is the same as 700. Level 4 of that standard accepts any number from 1 to 1000, and each number is at least as dark as the one before it. So a font with a whole range of weights can show one such as 450.
Why it fits here: This topic's 400 for normal and 700 for bold come from this very rule.
Source: W3C: CSS Fonts Level 4
In one line
Set font sizes in rem, weights as numbers from 100 to 900 (normal 400, bold 700) and slant with font-style: italic. CSS changes only the look, so meaning stays with strong and em.
Space between lines and letters
At a glance
Lines of text need room between them, or the eye slips to the wrong line. The line-height property sets the height of each line, and about 1.5 times the text size reads well for body text. Written as a plain number, without a unit, it scales with each element's own size.
letter-spacing adds room between letters, which helps capitals and tiny text. A line should not run too long either: around 65 characters, written as 65ch, keeps the eye from getting lost on its way back to the next line.
Take a guess: If body text is 16px and its line-height is 1.5, how tall is each line?
Old Ilse slips thin strips of lead between lines of metal type. Too few strips, and the lines crowd together and the eye slips to the wrong one. Printers still call line spacing leading, after those strips. In CSS it is line-height.
line-height: room between lines
line-height sets the height of each line of text. For body text, about 1.5 times the font size reads well: enough room that the eye finds the next line at once.
<!DOCTYPE html>
<style>
.tight { font-size: 20px; line-height: 1; }
.easy { font-size: 20px; line-height: 1.5; }
</style>
<p class="tight">Line height 1: the lines touch, and a long paragraph is tiring to read on a phone.</p>
<p class="easy">Line height 1.5: each line has room, and the eye finds the next one at once.</p>
Checked in Chrome
- 1 × 20px: lines exactly as tall as the letters
- 1.5 × 20px = 30px: room between the lines
Why line-height takes no unit
line-height accepts a plain number, with no unit, and that is the best way to write it. A plain number is passed down to children as a factor, and each child multiplies it by its own font size. A percent or a px value is passed down as a fixed height, which is wrong for any child with bigger text.
<!DOCTYPE html>
<style>
.factor { font-size: 20px; line-height: 1.5; }
.fixed { font-size: 20px; line-height: 150%; }
.big { font-size: 40px; display: block; }
</style>
<div class="factor">Text <span class="big">A big heading</span></div>
<div class="fixed">Text <span class="big">A big heading</span></div>
Checked in Chrome
- a plain 1.5 is passed down as a factor: 1.5 × the child's 40px = 60px
- (the trap: 150% becomes 30px on the parent, and the 40px child inherits 30px, so its lines overlap)
A memory aid
Line height: a number, no unit. A plain 1.5 grows with every child's text; 150% or 30px stays stuck at the parent's size.
letter-spacing and word-spacing
letter-spacing adds space between letters; word-spacing between words. A little extra spacing helps text in capitals or very small print; ordinary text is best left alone, since the font's designer already spaced it.
<!DOCTYPE html>
<style>
.label { font-size: 0.75rem; letter-spacing: 0.1em; }
</style>
<p class="label">NEW ARRIVALS</p>
Checked in Chrome
- 0.1em of a 12px label: 1.2px between letters
Line length
Lines that run across a whole wide screen are hard to read: the eye loses its place travelling back to the start of the next one. About 45 to 75 characters a line is comfortable. The unit ch, the width of the character 0 in the current font, makes that easy: max-width: 65ch keeps a column of text near 65 characters.
<!DOCTYPE html>
<style>
article { max-width: 65ch; line-height: 1.5; }
</style>
<article>A column of reading text, never much wider than about 65 characters, with room between its lines, is comfortable on a phone and on a wide screen alike.</article>
Checked in Chrome
- the browser turns 65ch into pixels: 65 widths of the character 0
- and the column is no wider than that
Worked example 4
A news site's article text is 18px with line-height 18px, and readers complain it is cramped. Its headings inside the article are 32px. Fix the spacing so that both the text and the headings have room.
What we have: a line height written in px, the same as the text, and bigger headings inside
What we need: a line height that suits the text and every heading
The plan: use a plain number on the article, so each element multiplies it by its own size
- 18px of line height on 18px text leaves no room. 1.5 would give 27px.
- Written as a plain 1.5 on the article, the headings inherit the factor: 1.5 × 32px = 48px.
<!DOCTYPE html>
<style>
article { font-size: 18px; line-height: 1.5; }
article h2 { font-size: 32px; }
</style>
<article><h2>Monsoon arrives early</h2><p>The rains reached Kerala on Monday, five days early.</p></article>
Checked in Chrome
- the text: 1.5 × 18px = 27px
- the heading: 1.5 × 32px = 48px
article { line-height: 1.5; }: 27px for the text and 48px for the headings.
Check: the browser multiplied the factor by each element's own size.
A path that fails: Writing line-height: 27px on the article. The text is fine, but the 32px headings inherit 27px, smaller than their letters, and their lines overlap.
When you are stuck: spacing
Cramped text: line-height about 1.5, written with no unit.
Big text with overlapping lines: its line height was inherited as px or %; switch the parent to a plain number.
Long lines that tire the eye: max-width around 65ch.
Your turn
Text is 16px with line-height: 1.5. How tall is each line?Tap to reveal
- Multiply.
24px.
Why is a plain number better than 150% for line-height?Tap to reveal
- What does a child with bigger text inherit?
A plain number is passed down as a factor; 150% is passed down as fixed pixels, too small for bigger text.
What does max-width: 65ch do to a column of text?Tap to reveal
- ch is about one character wide.
Keeps its lines near 65 characters, a comfortable length.
Did you know?
Leading is said like the metal
Printers say leading as "ledding", like the metal, not "leeding". In hand typesetting, leading was the thin strips of lead, or aluminium, put between lines of type to move them apart, and the practice became popular in the eighteenth century. In CSS, the space between lines is set with line-height.
Why it fits here: This topic's line-height is the modern name for those strips, and the old word still names line spacing.
Sources: Wikipedia: Leading; American Heritage Dictionary: lead
The accessibility rule for blocks of text
WCAG, the web's accessibility standard, has a success criterion for blocks of text, 1.4.8 Visual Presentation. It asks that a reader can get lines no wider than 80 characters, text that is not justified, and line spacing of at least space-and-a-half within paragraphs. This topic's line-height of about 1.5 and lines near 65ch fit inside those limits.
Why it fits here: It is the standard behind this topic's numbers for line height and line length.
Source: W3C: WCAG 2.2
ch is the width of a zero
The unit ch, which this topic uses for line length, is measured from one character. The CSS Values standard takes the width of the digit zero, 0, in the font in use, to stand for the typical width of European letters and digits. So max-width: 65ch is as wide as 65 zeros in that font.
Why it fits here: It explains what this topic's 65ch measures, and why the width changes when the font changes.
In one line
Give body text a line-height of about 1.5, written as a plain number so children multiply it by their own size; letter-spacing helps capitals and tiny text; keep lines near 65ch.
Aligning, decorating and transforming text
At a glance
A wedding card centres the names, a bill lines up its amounts on the right, and a sale tag strikes through the old price. The text-align property lines up the text inside a block, and centring suits short things only. The text-decoration property draws lines under, over or through the text.
text-transform changes how the letters look, for example into capitals, but the text in the HTML stays as it was. Each of the three properties has one small surprise, shown on these pages.
Take a guess: If text-transform shows a heading in capitals, what do the letters in the HTML look like?
A wedding card has its names centred; a bill has its amounts lined up on the right; a sale tag strikes through the old price. Three more properties set text out like that, each with its own small surprise.
text-align
| Value | Lines up the text | Good for |
|---|---|---|
left (the default for English) | on the left edge, ragged on the right | almost all reading text |
center | in the middle | short headings, a card's title |
right | on the right edge | numbers in a column, such as prices |
justify | on both edges, by stretching the spaces | rarely on screens: the stretched gaps make rivers of white |
<!DOCTYPE html>
<style>
h1 { text-align: center; }
.amount { text-align: right; }
</style>
<h1>Asha Stores</h1>
<p class="amount">Total: ₹830</p>
Checked in Chrome
- the heading is centred
- the amount lines up on the right
text-align lines up the text inside a block, not the block itself. To centre a whole box, lesson 3's margin: 0 auto is the tool. Mixing the two up is a very common slip.
Centring long text
Centred paragraphs make each line start in a different place, so the eye has to hunt for the start of every line. Centre short things, such as a heading or a button's words; keep paragraphs on the left.
text-decoration: lines on text
text-decoration draws lines on text: underline, line-through through the middle, overline above, or none. It can also take a style and a colour, like a border.
<!DOCTYPE html>
<style>
.old { text-decoration: line-through; }
.error { text-decoration: underline wavy red; }
</style>
<p>Toor dal <span class="old">₹200</span> ₹180</p>
<p>The PIN code <span class="error">41100</span> is too short.</p>
Checked in Chrome
- the old price is struck through
- a wavy red underline, like a spelling checker's
Remember the HTML step's rule: never by colour alone. The red wavy line goes with words that say what is wrong, as the sentence above does.
text-transform: changing the look of letters, not the letters
text-transform shows text in uppercase, lowercase or capitalize (a capital at the start of each word), without changing the text in the HTML.
<!DOCTYPE html>
<style>
.banner { text-transform: uppercase; letter-spacing: 0.1em; }
</style>
<p class="banner">diwali sale</p>
Checked in Chrome
- drawn in capitals
- but the text itself is still written in small letters
- innerText, which reads what is drawn, gives the capitals
Why does this matter? Because the words stay as they were written for search engines, for the page's own text, and for anyone who changes the style later. Write text in its normal case in the HTML, and leave the capitals to CSS, where a design change can undo them in one line.
Worked example 5
A shop's price list should show item names in capitals, old prices struck through, and prices lined up on the right of their column. The HTML is written in normal case. Write the three rules.
What we have: names in normal case, old prices, prices in a column
What we need: three rules that change only the look
The plan: text-transform for the names, text-decoration for old prices, text-align for the price column
<!DOCTYPE html>
<style>
.name { text-transform: uppercase; }
.old { text-decoration: line-through; }
.price { text-align: right; }
</style>
<p class="name">Toor dal</p>
<p class="price"><span class="old">₹200</span> ₹180</p>
Checked in Chrome
- drawn as TOOR DAL, still written Toor dal
- the old price struck through
- the price lined up on the right
.name { text-transform: uppercase; } .old { text-decoration: line-through; } .price { text-align: right; }
Check: each rule changed only the look: the name's own text is still Toor dal.
When you are stuck: aligning and decorating
text-align moves the text inside a box; margin: 0 auto moves the box.
A line through, under or over the text: text-decoration.
Capitals for looks: text-transform, and write the HTML in normal case.
Your turn
Which value of text-align suits a column of prices?Tap to reveal
- Numbers line up on one edge.
right.
How do you strike through an old price?Tap to reveal
- A line through the middle.
text-decoration: line-through.
With text-transform: uppercase, does the text in the HTML change?Tap to reveal
- Look at textContent in the check.
No: only how it is drawn changes.
Did you know?
Two letters i in Turkish
Capital letters are not the same in every language. Turkish has a dotted i and a dotless ı, each with its own capital: İ and I. So the CSS Text standard says that, when the content language is Turkish, the usual mapping between I and i is replaced by the dotted and dotless pairs. This is one reason text-transform pays attention to the page's language.
Why it fits here: This topic's text-transform: uppercase follows these rules, which change with the language of the page.
Source: W3C: CSS Text Level 3
Text that blinks
The first CSS standard, in 1996, offered text-decoration: blink, which makes text flash on and off. The value is still in today's CSS Text Decoration standard, but it is deprecated in favour of animations. That standard lets browsers leave the text still, and it notes that not blinking meets a checkpoint of the accessibility guidelines for browsers.
Why it fits here: It is the one value of this topic's text-decoration that browsers are allowed to ignore.
Sources: W3C: CSS level 1 (1996); W3C: CSS Text Decoration Level 3
In one line
The text-align property lines up the text inside a block (centre short things only), and text-decoration draws underlines and strike-throughs. The text-transform property changes how letters look, never the text itself.
Styling links, in all their states
At a glance
A link must look like a link, react when you point at it, and show when the keyboard reaches it. CSS gives a link a different look in each state: :link before it is visited, :visited after, :hover under the pointer, :focus-visible when the keyboard reaches it, and :active while it is pressed.
The rules go in the LoVe HAte order, or later rules hide earlier states. Keep an underline or another sign that is not only colour, a clear focus style, and the pointing hand only where a click really works.
Take a guess: If a:hover is written before a:link, does the hover colour still show on a link not yet visited?
A shop's door handle tells you three things without a word: it is a handle, you can push it, and it moves when you do. A link needs the same signals: it must look like a link, react when you point at it, and show when the keyboard has reached it. CSS gives a link a different look for each state it can be in.
| Selector | The link is | Read it as |
|---|---|---|
a:link | not yet visited | "a fresh link" |
a:visited | already visited from this browser | "been there" |
a:hover | under the mouse pointer | "pointed at" |
a:focus-visible | reached with the keyboard | "Tab is here" |
a:active | being pressed, for the moment of the click | "pressed now" |
The part after the colon is a pseudo-class: a state of the element, rather than a class written in the HTML. A selector with one counts as much as a class, 0, 1, 0, in lesson 1's specificity columns.
The order matters: LoVe HAte
All these rules are equally specific, so when two states are true at once, the one written later wins. A visited link can be hovered too; if :visited came after :hover, visited links would never change on hover. So the states go in a fixed order.
A memory aid
LoVe HAte: :link, :visited, :hover, :active. Put :focus-visible beside :hover, so keyboard users get the same signal as mouse users.
<!DOCTYPE html>
<style>
a:link { color: navy; }
a:visited { color: purple; }
a:hover, a:focus-visible { color: maroon; }
a:active { color: red; }
</style>
<p>See our <a href="/offers">Diwali offers</a>.</p>
Checked in Chrome
- the four states are written in the LoVe HAte order
- a fresh link matches :link, and is navy
Hover over the link to see it turn maroon. The check cannot hover a mouse, so it reads the stylesheet's own rules instead and confirms their order. Visited links are hard to test by script on purpose: browsers hide the visited state from scripts, so that a page cannot spy on where you have been.
Keep links recognisable
A link must be told from plain text without relying on colour alone, as the HTML step's accessibility lesson said. The underline is the simplest sign. If a design removes it, it must add another, such as bold weight, and keep the underline or a strong change on hover and focus.
<!DOCTYPE html>
<style>
a { color: navy; text-decoration: underline; text-underline-offset: 3px; }
a:hover, a:focus-visible { text-decoration-thickness: 2px; }
a:focus-visible { outline: 3px solid orange; }
</style>
<p>Read the <a href="/terms">terms of delivery</a>.</p>
Checked in Chrome
- the link keeps its underline, a sign that is not only colour
- and reached with the keyboard, it shows a 3px orange ring
text-underline-offset moves the underline a little below the letters, which many find neater, and text-decoration-thickness thickens it on hover and focus, a clear signal that is not colour.
The pointer: cursor
The cursor property sets the mouse pointer's shape over an element. Links already get pointer, the pointing hand, from the browser. Give it to other things that can be clicked, such as a summary or a custom control, and to nothing else: a hand over plain text promises a click that does nothing.
<!DOCTYPE html>
<style>
summary { cursor: pointer; }
</style>
<a href="/offers">Offers</a>
<details><summary>Delivery charges</summary><p>Free above ₹499.</p></details>
<p>Plain text</p>
Checked in Chrome
- a link has the pointing hand by default
- the summary, also clickable, is given one
- plain text keeps the ordinary pointer
Information only in a tooltip
The HTML title attribute shows a small tooltip when the mouse rests on a link. Phones have no mouse to rest, and keyboard users never see it, so anything a visitor needs must be in the link's own words, not only in its title.
Worked example 6
A shop's design removes the underline from links and makes them the same navy as headings. Readers cannot find the links. Restore a sign that is not colour alone, a hover change, and a keyboard focus style, in the right order.
What we have: links with no underline, coloured like headings
What we need: rules that make links recognisable and responsive, in the LVHA order
The plan: bring back the underline; on hover and focus thicken it and change colour; add a focus ring; write the states in order
<!DOCTYPE html>
<style>
a:link { color: navy; text-decoration: underline; }
a:visited { color: purple; }
a:hover, a:focus-visible { color: maroon; text-decoration-thickness: 2px; }
a:focus-visible { outline: 3px solid orange; }
a:active { color: red; }
</style>
<p>Our <a href="/offers">offers</a> change every Monday.</p>
Checked in Chrome
- the underline is back
- the states are in the LoVe HAte order
An underline on a:link, a thicker one and a new colour on hover and focus, an orange focus ring, and the states in LoVe HAte order.
Check: the underline is a sign that needs no colour, and the order lets every state show when it should.
When you are stuck: link styles
A hover style that never shows on visited links: :visited is written after :hover. Use the LoVe HAte order.
Links lost among text: bring back the underline, or another sign that is not colour.
Never remove the focus style without giving a clear one, such as an outline.
Your turn
What order do the link states go in?Tap to reveal
- LoVe HAte.
:link, :visited, :hover, :active, with :focus-visible beside :hover.
Why can a script not read whether a link was visited?Tap to reveal
- Think of the visitor's history.
Browsers hide it, so that a page cannot spy on where the visitor has been.
Where should cursor: pointer go?Tap to reveal
- It promises a click.
Only on things that can be clicked; links have it already.
Did you know?
When visited links leaked your history
For years, visited links leaked browsing history. A page could ask the browser how its links were styled and learn which sites a reader had been to; Mozilla wrote that some tests could check 210,000 addresses a minute. On 31 March 2010 Mozilla announced the fix: scripts are always told that links are unvisited, and visited links may change only a few properties. Browsers still limit these styles, for privacy.
Why it fits here: This topic's :visited is the state with the fewest styles allowed, and this is the reason.
Sources: Mozilla Hacks: privacy-related changes coming to CSS :visited; MDN: :visited
Where the blue and the purple come from
The blue of a fresh link and the purple of a visited one are written down in the HTML standard. Its suggested style sheet for browsers gives :link the colour #0000EE and :visited the colour #551A8B, and a link being pressed turns #FF0000, red. Rules of your own for these states replace them.
Why it fits here: These are the defaults that this topic's link states start from, before any style of yours.
Source: WHATWG: HTML Standard, Rendering
In one line
Links have states, :link, :visited, :hover, :focus-visible and :active, written in the LoVe HAte order. Keep an underline or another sign that is not colour, a clear focus style, and the pointing hand only where a click works.
Where it is used
Every readable page you have used made these choices, usually in one short block of rules on body and the headings.
| What you learned | Where it is used | A real example |
|---|---|---|
| a font stack | every site | a news site's body: a web font first, then Arial, then sans-serif |
| web fonts | brand typefaces | a bank's own typeface, sent with every page through @font-face |
| font-weight | headings, prices, buttons | a shopping site's prices in bold, its old prices regular |
| line-height 1.5 | every reading page | an online textbook whose lines never crowd on a phone |
| max-width in ch | articles and lessons | a blog's column held near 65 characters on a wide screen |
| text-decoration: line-through | sales | the struck-through old price on a sale tag |
| text-transform | labels and buttons | a menu written in normal case but shown in capitals |
| link states | every link | a search result's title that changes colour and thickens its underline under the pointer |
Mistakes and debugging
These are the typography slips that make pages hard to read, each checked in the browser.
1. A line height in px or %
<!DOCTYPE html>
<style>
article { font-size: 16px; line-height: 24px; }
article h2 { font-size: 32px; }
</style>
<article><h2>Monsoon arrives early</h2></article>
Checked in Chrome
- (the slip: the 32px heading inherits 24px of line height, less than its own letters, so its lines overlap)
The fix: line-height: 1.5, with no unit, so the heading gets 1.5 × 32px = 48px.
2. The link states out of order
<!DOCTYPE html>
<style>
a:hover { color: maroon; }
a:visited { color: purple; }
</style>
<a href="/offers">Offers</a>
Checked in Chrome
- (the slip: :visited comes after :hover, so a visited link never changes colour on hover)
The fix: the LoVe HAte order, :link, :visited, :hover, :active.
3. No fallback at the end of a font stack
font-family: "Ashoka"; alone leaves the browser's own default, often a serif, wherever the web font fails to load. The fix: always end with a generic family: font-family: "Ashoka", sans-serif;.
4. Links that look like text
No underline, and the same colour as the text around: links are found only by luck. The fix: an underline, or another sign that is not colour, plus a hover and focus change.
5. Long centred paragraphs
Every line starts in a new place, and the eye hunts for each one. The fix: left-aligned paragraphs; centre only short headings.
6. Bold for meaning
A warning made bold with CSS alone is not important to a screen reader. The fix: strong in the HTML; CSS only for looks.
The debugging corner: which font was really used?
Inspect a piece of text in DevTools and open the Computed tab. At its very bottom, Rendered Fonts names the font the browser actually drew with, and how many letters came from each. If it is not your first choice, the font is not installed or its web font file did not load: the Network tab, filtered to Font, shows whether the file arrived.
Quick card
The lesson on one card.
Every idea of the lesson
<!DOCTYPE html>
<style>
body { font-family: "Noto Sans", Arial, sans-serif; font-size: 1rem; line-height: 1.5; }
h1 { font-family: Georgia, serif; font-size: 2rem; font-weight: 700; text-align: center; }
article { max-width: 65ch; }
.label { text-transform: uppercase; letter-spacing: 0.1em; font-size: 0.75rem; }
.old { text-decoration: line-through; }
a:link { color: navy; text-decoration: underline; }
a:visited { color: purple; }
a:hover, a:focus-visible { color: maroon; text-decoration-thickness: 2px; }
a:active { color: red; }
</style>
<h1>Asha Stores</h1>
<article>
<p class="label">new arrivals</p>
<p>Toor dal <span class="old">₹200</span> ₹180. See all <a href="/offers">offers</a>.</p>
</article>
Checked in Chrome
- the stack and a 1.5 line height, inherited from body: 1.5 × 16px
- a 2rem heading, centred
- capitals drawn, small letters written
- the old price struck through
- the link states in LoVe HAte order
| Idea | In one line |
|---|---|
font-family | a stack of wishes, ending in serif, sans-serif or monospace |
@font-face | a web font sent with the page; font-display: swap; two families are enough |
font-size | in rem: 1rem text, 1.5rem to 2rem headings |
font-weight, font-style | 100 to 900 (400 normal, 700 bold); italic; looks only, meaning stays in the HTML |
line-height | about 1.5, a plain number, so children multiply it |
letter-spacing, max-width: 65ch | spacing for capitals and small print; lines near 65 characters |
text-align, text-decoration, text-transform | lines up text; lines on text; capitals in the look only |
| link states | :link, :visited, :hover (with :focus-visible), :active: LoVe HAte |
cursor: pointer | only on things that can be clicked |
The story: A notice people read across a square
You choose one family of letters, with a second and a third ready in case a glass does not have the first. The heading is heavy, the text is regular, and the lines get room to breathe between them, like Ilse's strips of lead.
The links take their own colour, change when a finger hovers over them, and keep their underline, so nobody mistakes them for plain words. The pointer turns into a hand over each one.
Ilse reads the notice from the far end of the shop. "That," she says, "I can read from here."
Reagents gathered: font-family, font-weight, line-height, text-align, hover, cursor.
Sources
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
- HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)