Text size, units, and fonts
W3C CSS Snapshot 2026
Typography is how text is set so people can read it. CSS lets you choose the size, the space between lines, and the font the browser tries first.
On this page
Set font size with px, em, and rem
px is a CSS pixel. For font-size, em uses the parent’s text size as its reference. rem uses the root element’s text size. If the root size is 16px, 1.25rem is 20px. Percentages for font-size refer to the parent’s size.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Set font size with px, em, and rem</title>
<style>
html { font-size: 16px; }
.notices { font-size: 20px; }
.em { font-size: 1.25em; }
.rem { font-size: 1.25rem; }
</style>
</head>
<body>
<div class="notices"><p class="em">Sized in em</p><p class="rem">Sized in rem</p></div>
</body>
</html>
What to expect In the same parent, the em paragraph is 25px and the rem paragraph is 20px. Confirm in DevTools → Computed.
Size a box relative to its parent
For width in normal flow, a percentage uses the width of the containing block. For example, 75% of a 240px-wide content area is 180px. Change the parent’s width and the child’s width updates too.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Size a box relative to its parent</title>
<style>
.panel { width: 240px; outline: 1px solid teal; }
.panel p { width: 75%; background: #d6efea; }
</style>
</head>
<body>
<div class="panel"><p>Your order is ready.</p></div>
</body>
</html>
What to expect The colored paragraph is 180px wide inside the 240px panel.
Adjust line spacing with line-height
line-height sets the height of each text line. A number such as 1.5 multiplies the element’s font size. With a 20px font, 1.5 produces a 30px line height.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Adjust line spacing with line-height</title>
<style>
p { font-size: 20px; line-height: 1.5; width: 20ch; }
</style>
</head>
<body>
<p>The library is open every morning and lends books to local residents.</p>
</body>
</html>
What to expect The text wraps, and each line has room around it. Compare 1.5 with 1.1.
Choose fallback fonts
font-family is a list the browser tries in order. If the first font is unavailable or cannot display a character, the browser tries another. A generic name like sans-serif lets the browser choose a suitable font. @font-face lets you supply a font file your site owns or is licensed to use; font-display: swap allows fallback text while that file loads.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Choose fallback fonts</title>
<style>
.notice { font-family: "Noto Sans Myanmar", Arial, sans-serif; }
p { font-family: Georgia, serif; }
</style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>
What to expect The first paragraph uses the first suitable sans-serif font in its list. The second uses a serif font.