Book a free demo

Web Developer Path ยท lesson 9 of 18

Fonts and text styling

Choose fonts and sizes, set the spacing between lines and letters, and make text easy to read.

What you will learn

  • font-family with fallbacks, font-size, font-weight
  • text-align, text-transform, text-decoration
  • line-height and letter-spacing for readability

Understand the idea

font-family lists fonts in order of preference: font-family: Georgia, "Times New Roman", serif;. If the first is missing on the visitor's computer the browser tries the next, and the last should be a generic family (serif, sans-serif, monospace). Put names with spaces in quotes.

font-size can be set in px (fixed), em (relative to the parent) or rem (relative to the page's base size, which is 16px by default). Rem is a good habit because the text can grow if a visitor increases their browser's text size. font-weight is normal, bold or a number from 100 to 900.

text-align sets left, center, right or justify. text-transform changes case (uppercase, lowercase, capitalize). text-decoration adds or removes underlines, so text-decoration: none removes the line from a link. For long paragraphs, line-height: 1.5 (one and a half times the text size) and a little letter-spacing make reading far easier.

Example

Heading, paragraph and link
h1 {
  font-family: Georgia, serif;
  font-size: 2.5rem;
  text-transform: uppercase;
  letter-spacing: 2px;
}

p {
  line-height: 1.6;
  text-align: justify;
}

a {
  text-decoration: none;
  font-weight: bold;
}

rem sizes follow the page's base size, so this heading is 2.5 times the normal text.

Your turn: code to type and practise

Style h1 with a font-family, a font-size, text-align: center and text-transform: uppercase. Style p with a line-height and letter-spacing, and remove the underline from a with text-decoration: none.

Stretch challenge

Change font-size from rem to px and think about which is better for a visitor who enlarges their text.

โ† Backgrounds and gradientsCombinators, pseudo-classes and pseudo-elements โ†’

CSS

More in this module

Keep going in Web Developer Path

Practise the idea

๐Ÿ‹๏ธ Play Grammar Gym

Adaptive English grammar practice in nine skills, with a SmartScore that climbs as you improve.

Play now

FAQ

Fonts and text styling: common questions

What is the difference between px, em and rem?

px is a fixed pixel size. em is relative to the parent element's font size, and rem is relative to the page's root font size, which makes rem predictable.

What is a good line-height for body text?

About 1.5 to 1.6 times the font size makes paragraphs comfortable to read.

How do I remove the underline from a link?

Use text-decoration: none on the a selector.

I am stuck. What can I do?

Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.

Does my work get saved?

Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.

Start a conversation

Enjoying coding? Meet a teacher.

Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try