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-familywith fallbacks,font-size,font-weighttext-align,text-transform,text-decorationline-heightandletter-spacingfor 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
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 โ
More in this module
Keep going in Web Developer Path
Adding CSS: three ways and three selectors
Colour the `h1` text, give `.intro` a `background-color`, and give `#special` both a `color` and a `background-color`.
Start โ8Backgrounds and gradients
Give `.hero` a `background-image` that uses `linear-gradient`, set its `background-size`, and give `.card` a `background-color`. Use the long-hand properties.
Start โ10Combinators, pseudo-classes and pseudo-elements
Write four rules with exactly these selectors: `.menu a:hover` (set a `color`), `.menu li:first-child` (set `font-weight`), `h2::before` (set `content`) and `input[type="text"]` (set a `border`).
Start โ11The box model
Style `.card`: give it a `width`, some `padding`, a `border`, a `margin` of `12px auto`, and `box-sizing: border-box`.
Start โ12Display and position
Make `.card` `position: relative`, pin `.badge` to its top-right corner with `position: absolute`, `top` and `right`, and hide `.hide-me` with `display: none`.
Start โPractise the idea
๐๏ธ Play Grammar Gym
Adaptive English grammar practice in nine skills, with a SmartScore that climbs as you improve.
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
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.
Pick a game
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay