In CSS Lesson 3 you changed the color and look of words. Now let's choose the letters themselves: their shape, size and weight.
flag
What you'll learn
How to pick a font, with backups in case it is missing.
How to make text bigger, smaller, bolder or thinner.
How to space lines so long text is easy to read.
waving_handLet's choose some fonts
A font is a set of letters drawn in one style. Some look classic, some look modern, some look like a typewriter.
With four font properties, you choose the font, how big it is, how bold it is, and how much room each line gets.
restaurant_menu
Think of a café menu. The café's name is printed in a fancy, classic font, and it's the biggest text on the card.
Section names like "Drinks" are bold, so your eyes find them fast. The small note under the drinks has extra space between the lines, so it's easy to read.
Front Page Caféfont-family: Georgia, serif;font-size: 30px;
Every drink is made fresh when you order. Ask for less sugar or a different milk, and we will gladly make it your way.
font-size: 13px;line-height: 1.8;
Two fonts are enough for most pages: a special one for the name or headings, and a plain one for everything else.
searchA closer look
Fonts need backups
The browser can only show fonts that are on the visitor's computer or phone. That's why font-family takes a list, split by commas: Georgia, 'Times New Roman', serif.
The browser tries each name in order and uses the first one it has. The last name is a generic family, a type of font that every device has.
Put quotes around names with spaces, like 'Times New Roman'.
Three generic families
serif fonts have small "feet" on the letters, like in printed books. sans-serif fonts have no feet (sans means "without") and look clean on screens. monospace fonts give every letter the same width, which is why code editors use them.
Text size
font-size sets how big the letters are. Most browsers start body text at 16px, and headings are bigger by default.
For story text, 16px to 18px is easy to read. Pixels are a good start. CSS Lesson 6 shows other units like em and rem.
Font weight
font-weight sets how thick the letters are: normal (the same as 400) or bold (the same as 700).
Numbers from 100 to 900 give more steps, but a font only shows the steps it was made with.
Line height
line-height sets how tall each line of text is, so it controls the space between lines. A plain number like 1.6 means "1.6 times the text size", and it adjusts by itself if the text size changes.
Long paragraphs are easier to read with a line height between 1.5 and 1.8.
constructionLet's build it
Let's choose fonts for a Front Page Bytes story: the site name, the headline, the writer's name and the story text. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
1Step 1: Pick a font for the whole page
Your code
body { background-color: #F0F7FF; font-family: Arial, Helvetica, sans-serif;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
font-family on body changes the font of all the text, because fonts are passed down like text color. The browser uses Arial, then Helvetica if Arial is missing, then any sans-serif font.
2Step 2: Give the site name its own font
Your code
body { background-color: #F0F7FF; font-family: Arial, Helvetica, sans-serif;}h1 { font-family: Georgia, 'Times New Roman', serif;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
The h1 rule switches the site name to Georgia, a classic newspaper-style font with small feet on the letters. Everything else stays Arial.
3Step 3: Change text sizes
Your code
body { background-color: #F0F7FF; font-family: Arial, Helvetica, sans-serif;}h1 { font-family: Georgia, 'Times New Roman', serif; font-size: 40px;}p { font-size: 18px;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
font-size: 40px; makes the site name bigger, and font-size: 18px; makes every paragraph a little bigger than the usual 16px, so the story is easier to read.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
The byline is a paragraph too, so it was 18px. This rule makes it smaller at 14px and bold with font-weight: 700;.
The .byline rule points at a class, and a class rule wins over a plain tag rule like p, so its size is used.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
line-height: 1.6; makes each line of the story 1.6 times as tall as its text. Compare it with step 4: the story looks calmer and is easier to follow.
sticky_note_2Quick notes
font-familyWhich font to use
body { font-family: Arial, Helvetica, sans-serif; }
How to use
Georgia, serifA font name first, then a backup. The browser uses the first one it has.
serifLetters with small feet, like in books and newspapers.
sans-serifLetters without feet. Clean and easy to read on screens.
monospaceEvery letter is the same width, like code.
'Times New Roman'Put quotes around font names that have spaces.
Examples
A clean font for the whole page.
body { font-family: Arial, Helvetica, sans-serif;}
A classic newspaper look for the site name.
h1 { font-family: Georgia, 'Times New Roman', serif;}
Code shown in a typewriter-style font.
code { font-family: 'Courier New', monospace;}
font-sizeHow big the text is
p { font-size: 18px; }
How to use
16pxThe usual size of body text.
24pxBigger text, good for headings.
1.5emOne and a half times the size of the parent's text (more in CSS Lesson 6).
Add a unit like px. A number on its own is skipped.
Examples
A big site name.
h1 { font-size: 40px;}
Easy-to-read story text.
p { font-size: 18px;}
Small print, like a photo caption.
.caption { font-size: 13px;}
font-weightHow bold the text is
.byline { font-weight: 700; }
How to use
400 or normalRegular text.
700 or boldBold text. Headings are bold by default.
100 to 900From very thin to very heavy. Only some fonts have every step.
Examples
A bold name under a story.
.byline { font-weight: 700;}
A heading that is not bold.
h2 { font-weight: normal;}
line-heightSpace for each line
p { line-height: 1.6; }
How to use
1.6Lines are 1.6 times as tall as the text. A number with no unit is the easiest choice.
24pxAn exact line height.
normalThe browser's usual spacing, about 1.2.
Values below 1 make lines overlap.
Examples
Roomy lines for long stories.
p { line-height: 1.6;}
Tight lines for a big two-line title.
h1 { line-height: 1.1;}
reportCommon mistakes
These are mistakes many beginners make. Each mini browser shows what really happens.
error
A misspelled font with no backup
The mistake
body { background-color: #F0F7FF; font-family: "Curier New";}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
No device has a font called "Curier New", and there is no backup in the list. The browser falls back to its own default font, so the page doesn't get the typewriter look.
tips_and_updatesTip: Check the spelling, and end the list with a generic family: font-family: 'Courier New', monospace;
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
18 on its own could mean 18 pixels, 18 points or something else, so the browser skips the line and the story keeps its usual size.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <h1>Front Page Bytes</h1> <h2>New Phones Arrive This Week</h2> <p class="byline">By Ana Reyes</p> <p>Three new phones go on sale on Friday. Each one has a bigger battery, a sharper camera and faster charging. Stores in Manila and Cebu open early, and prices start at ₱9,999.</p> </body></html>
What you see
description(no title)
With line-height: 0.7;, each line is shorter than the letters themselves, so the lines of the story run into each other.
tips_and_updatesTip: Use a line height of 1.5 or more for paragraphs.
quizTest your knowledge
Edit the CSS on the left. Open the HTML tab to see the class names you can use. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Change the font of the whole page with font-family on body.not done yet
Change the size of the .note text.not done yet
Make the .title heading normal weight, not bold.not done yet
Give the .note text more space between lines.not done yet