Fonts and Text: Family, Size, Weight and Spacing

A handful of text properties (font-family with web-safe fonts and fallbacks, font-size, font-weight, font-style, line-height, text-align, text-transform, letter-spacing, text-decoration, font-variant, text-indent and overflow-wrap) turn plain words into text people can read at a glance.

Novice · CSS · Updated

By

After this lesson you can

  • Choose a font with font-family and a list of fallbacks that ends in a generic family
  • Set text size, weight, style and line spacing with font-size, font-weight, font-style and a unitless line-height
  • Align and decorate text with text-align, text-transform, letter-spacing and text-decoration
  • Set small capitals and indents with font-variant: small-caps and text-indent, including a hanging indent
  • Let long words and links break with overflow-wrap (word-wrap), and tell it apart from word-break

Key takeaways

  • font-family takes a list of fonts, and the browser uses the first one it has. Web-safe fonts such as Verdana are on most computers; end the list with a generic family such as sans-serif.
  • Size text in rem. 1rem is the reader's normal text size, so your page follows their own browser setting.
  • font-weight sets how bold text is (400 normal, 700 bold), font-style slants it, and a unitless line-height such as 1.5 spaces lines by the text's own size.
  • text-align, text-transform, letter-spacing, text-decoration, font-variant and text-indent line text up, change its capitals, space it, mark it and indent it.
  • overflow-wrap: break-word (its older name is word-wrap) breaks a long word or link only when it can't fit; word-break: break-all breaks any word at the box's edge.

A good menu board can be read from the sidewalk: big, bold names, prices that stand out, and enough room between the lines that your eye doesn't slip. A bad one is a wall of tiny, cramped letters, and people walk on by. Web pages work the same way. Most of what's on any page is text, so you'll use the properties in this lesson on everything you build: a résumé page, a class report, a blog, the Sol Taco Truck menu.

font-family: a list, best choice first

Ordering at the truck window, you might say: "a horchata, please; if you're out, an agua fresca; if not, any cold drink." You get your first choice if they have it, and something close if they don't.

font-family works the same way. A browser can only use a font that's on the reader's device (or one the page sends along, which is the next lesson), so you give it a list, best choice first. That list is called a font stack, and the fonts after the first are its fallbacks:

body {
  font-family: "Trebuchet MS", Verdana, sans-serif;
}

h1 {
  font-family: Georgia, "Times New Roman", serif;
}

Put quotes around a font name that has spaces in it, such as "Trebuchet MS". The last item in each list is a generic family, a kind of font that every browser has some version of, so the list always ends with a match:

Generic family What it looks like Good for
serif small strokes ("feet") at the ends of letters a classic, bookish look
sans-serif no feet: clean, plain letters most body text on screens
monospace every letter the same width code, receipts, prices in columns
system-ui the device's own interface font pages that feel like an app
cursive handwriting-like letters a short, playful touch

Generic family names never take quotes: sans-serif, not "sans-serif". In quotes, the browser looks for a font actually named sans-serif.

Web-safe fonts: the ones most computers already have. Which fonts are worth naming first? A web-safe font is a font that comes installed on almost every Windows and Mac computer, so a stack that starts with one shows your first choice to most readers on a computer, with nothing to download. The usual list:

Web-safe font Its generic family
Arial sans-serif
Verdana sans-serif
Trebuchet MS sans-serif
Georgia serif
Times New Roman serif
Courier New monospace

That's why both stacks above start with web-safe fonts. "Almost every" isn't "every", though: many phones, Android phones especially, come with their own fonts instead of these. So a web-safe font still goes in a stack, and the stack still ends with a generic family, so a phone shows a font of the right kind. (A font that isn't web-safe, such as a brand's own font, can be sent along with the page: that's the next lesson.)

Try it. In your sol-taco-truck page (or any page), give the body the first stack above. Then misspell the first font, as "Trebuchet": nothing breaks, and the browser moves on to Verdana. To see which font it really used, select some text in the developer tools' Elements panel and scroll to the bottom of the Computed pane: Rendered Fonts names it. (In Firefox, it's the Fonts pane.)

font-size, and the rem unit

font-size sets how big the letters are. The unit that suits text best is rem, read "rem". 1rem is the reader's normal text size, which is 16 pixels in most browsers unless the reader has changed it:

body {
  font-size: 1.125rem;
}

h1 {
  font-size: 2.5rem;
}

The body text is 1.125 times the normal size, which is 18 pixels for most readers, and the heading 2.5 times, 40 pixels. Here's why rem beats pixels for text: someone who has set their browser to bigger text gets bigger text on your page too, because everything is measured from their normal size. With font-size: 16px, the size is fixed, whatever the reader asked for. (Units: px, rem, em, % and Viewport Units, in the next chapter, covers every unit.)

font-weight and font-style

font-weight sets how bold text is, with a number: 100 is thin, 900 is very heavy, and most fonts come in steps of 100 between them. Two have names: 400 is normal, and 700 is bold. font-style: italic slants the letters, and font-style: normal straightens them again.

.price {
  font-weight: 700;
}

.tagline {
  font-style: italic;
}

Every price turns bold, and the tagline under the truck's name leans to the right. A font comes with only some weights. Ask for one it doesn't have, and the browser uses the nearest one it has, or draws a fake bold or italic, which never looks quite as good as the real thing. The next lesson shows how to load the weights you need.

line-height: room between the lines

Notebook paper comes wide-ruled and college-ruled: the same words, but on wide-ruled paper the lines sit farther apart. line-height is how far apart your lines of text sit. Give it a number with no unit, and it's a multiple of the text's own size:

body {
  font-size: 1.125rem;
  line-height: 1.5;
}

h1 {
  line-height: 1.1;
}

For 18-pixel body text, 1.5 gives lines 27 pixels apart, a comfortable rhythm: body text reads well at about 1.4 to 1.6. Big headings look better tighter, around 1.1 to 1.2.

Why no unit? A number scales with each element's own size. Write line-height: 24px on the body instead, and every element inside it gets 24-pixel lines, including a 40-pixel heading, whose lines would then crash into each other if it wrapped onto two. With 1.5, the heading gets lines 1.5 times its size.

Try it. Paste a long paragraph into your page (two or three menu descriptions in a row will do). Set its line-height to 1, then 1.5, then 2.5, saving after each. Which reads best, and which makes the lines feel like they're floating apart?

text-align, text-transform and letter-spacing

The truck's section headings are painted in capital letters, spaced out a little and centered. Three properties do that:

h2 {
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
  • text-align lines text up: left (the usual for English), right, center, or justify, which stretches each line to both edges.
  • text-transform: uppercase shows text in capitals. The HTML still says Tacos: only the look changes, so if the design changes later, you change one line of CSS instead of retyping every heading. There's also lowercase, capitalize (a capital at the start of each word) and none.
  • letter-spacing adds room between letters. 0.1em is a tenth of the text's own size (an em is the size of the element's own font), so the spacing grows with the text. Capitals look better with a little extra room; lowercase body text is best left alone.

text-decoration: underlines and cross-outs

text-decoration draws a line under, over or through text. The value you'll use most is line-through, for a price that has dropped:

.old-price {
  text-decoration: line-through;
}

.menu-link {
  text-decoration: none;
}
<p>Chicken tinga burrito <span class="old-price">$10.00</span> <span class="price">$8.50</span></p>

The old price is crossed out beside the new one, the way a sale sign shows it. underline adds an underline, and none takes one away, which is how sites remove the underline from links in a menu bar.

Take care with that last one. In a paragraph of text, the underline is how readers spot a link, and some people can't tell the link's color from the text around it. Keep links in your paragraphs underlined, and remove the underline only where it's clear from the design that something is a link, such as a menu bar of buttons.

Small capitals and indents: font-variant and text-indent

The truck's "Our story" page reads like a page from a book, and books have two habits the browser doesn't copy on its own. Their headings are often set in small capitals: capital letters only as tall as the lowercase ones, quieter than shouting in full capitals. And their paragraphs often start with the first line pushed in a little, an indent, so the eye can see where a new paragraph begins.

.story h2 {
  font-variant: small-caps;
}

.story p {
  text-indent: 1.5em;
}
  • font-variant: small-caps draws the lowercase letters as small capitals. Our Story shows as a capital O and S at full height, and "ur" and "tory" as small capitals. Like text-transform, it changes only the look: the HTML keeps its own letters. font-variant: normal turns it off.
  • text-indent pushes the first line of a block of text in from the left. 1.5em is one and a half times the text's own size, so with 16-pixel text the first line starts 24 pixels in. Only the first line moves; the rest of the paragraph starts at the edge.

A negative text-indent pulls the first line out instead, to the left of the rest. That's a hanging indent: the first line hangs out, and every other line is pushed in. A list of sources at the end of a report is set this way, and so is a menu where each dish's name should stand out at the left:

.dish {
  padding-left: 1.5em;
  text-indent: -1.5em;
}

The padding pushes the whole paragraph in by 1.5em, then the negative indent pulls the first line back out by the same amount, so the dish's name starts at the edge and its description lines up underneath, a little in. Without the padding, the first line would hang off the left edge of its box.

Long words that won't fit: overflow-wrap and word-break

The truck's story ends with a link to its catering form, written out in full: soltacotruck.example/catering/request-a-quote. On a phone, the story's column is narrow, and that address is one long "word" with no spaces in it. A browser breaks lines only at spaces (and a few other places, such as after a hyphen), so the address can't wrap. It sticks out past the side of its box, and on a narrow screen it can push the whole page sideways.

overflow-wrap: break-word lets the browser break a word that is too long to fit on a line of its own, wherever it has to. Words that fit are left alone:

.story a {
  overflow-wrap: break-word;
}

Now the address wraps onto a second line inside the box. You'll also see this property under its older name, word-wrap: word-wrap: break-word is the same thing, and browsers still read it.

overflow-wrap has three values:

  • normal, the starting value: a long word sticks out.
  • break-word: a long word breaks only when it can't fit on a line by itself.
  • anywhere: breaks the same way. The difference shows up when the browser works out how narrow a box is allowed to get, which matters in the flexbox and grid layouts of Module 3: with anywhere, a box with a long word in it can shrink.

A different property, word-break, sounds as though it does the same job, and it doesn't. word-break: break-all breaks any word at the edge of the box, even a short one that would fit on the next line. The text fills every line right to the edge, like a receipt, and ordinary words get chopped in the middle: "bur-" on one line and "rito" on the next.

Declaration A long address Ordinary words
overflow-wrap: normal; Sticks out of the box Wrap at spaces
overflow-wrap: break-word; Breaks to fit Wrap at spaces, never split
word-break: break-all; Breaks to fit Split at the box's edge, mid-word

For text people read, choose overflow-wrap: break-word. Save word-break: break-all for long strings nobody reads as words, such as an order code in a narrow table cell.

Do it: make the menu readable from the sidewalk

People in line squint at Sol Taco Truck's menu: the browser's plain text is small and cramped, and nobody can see that the chicken tinga burrito is on sale. The owner asks for:

  • Verdana for the whole menu, and if a reader's device doesn't have it, any sans-serif font in its place.
  • Text a little bigger than the browser's: 1.125rem.
  • Room between the lines: one and a half times the text's size, written as a plain number so it grows with the text.
  • Prices in bold, and the old price crossed out, the way a sale sign shows it.

Practiced live on the lesson page: write the CSS right there and check it.

Do it: set the truck's story like a page from a book

Sol Taco Truck's "Our story" box is narrow, so it fits beside the menu on a phone. The owner wants it to read like a page from a book, and right now its catering link sticks out of the box:

  • The heading, Our Story, in small capitals.
  • Each paragraph's first line pushed in by one and a half times the text's size: 1.5em, which is 24 pixels here.
  • The long catering link breaks onto the next line when it can't fit, so it stays inside the box.
  • The story's ordinary words are never split in the middle: only the link, when it has to.

Write each rule in the box under its selector.

Practiced live on the lesson page: write the CSS right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Text is most of every page you'll build. A font stack with a fallback, sizes in rem, a unitless line-height and a few finishing properties make the difference between a page people read and a page they leave.

Key terms

Cheat sheets

Related