CSSLesson 5 of 10Jump to practice
Text and fonts
Control font family, size, weight, line spacing and alignment.
Choosing a font
font-family takes a list. The browser uses the first font it has installed and falls back to the next one.
body {
font-family: "Segoe UI", Roboto, Arial, sans-serif;
}
Always finish with a generic family, so there’s a sensible fallback:
| Generic family | Looks like |
|---|---|
sans-serif |
Clean letters with no feet, like Arial |
serif |
Letters with small feet, like Times |
monospace |
Every letter the same width, like code |
Font names with spaces need quotes: "Segoe UI".
Size, weight and spacing
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Georgia, serif;
font-size: 18px;
line-height: 1.6;
color: #333;
max-width: 34em;
}
h1 {
font-family: Arial, sans-serif;
font-size: 2rem;
font-weight: 700;
letter-spacing: -0.5px;
text-align: center;
}
.note {
font-style: italic;
color: #777;
}
a {
color: teal;
text-decoration: none;
}
</style>
</head>
<body>
<h1>Fall in Vermont</h1>
<p>In October the maple trees turn red, orange and gold.
The back roads fill with people stopping to take photos.</p>
<p class="note">Photos by our readers. <a href="#">Send yours</a>.</p>
</body>
</html>Result
| Property | What it does | Common values |
|---|---|---|
font-size |
How big the letters are | 16px, 1.25rem, 2em |
font-weight |
How thick | 400 (normal), 700 (bold), or normal, bold |
font-style |
Slanted or not | normal, italic |
line-height |
Space between lines | 1.5 (no unit means 1.5 × the font size) |
text-align |
Line up the text | left, center, right, justify |
text-decoration |
Lines on text | none, underline, line-through |
px, em or rem?
pxis a fixed size.remis relative to the root font size (16px by default). If a visitor sets a larger default font for easier reading,remsizes grow with it.emis relative to the element’s own font size, which makes it handy for spacing that should scale with the text.
A good habit: use rem for font sizes.
Practice
Check what you just learned. Fill in the missing code, then answer the quiz.
Exercise 1Solved
Center the heading.
h1 { text-align: ; }CSS uses the American spelling.
Exercise 2Solved
Make the text bold using the right property.
.title { : bold; }Exercise 3Solved
Remove the underline from links.
a { text-decoration: ; }
Finish the practice to complete this lesson.
Sign in to keep your progress on any device.
