CSS Text
CSS gives you fine control over how text looks: its colour, alignment, decoration, capitalisation, spacing and more. This lesson covers the most useful text properties; fonts themselves (family, size, weight) are covered in the next lesson.
Colour and alignment
color sets the text colour. text-align sets horizontal alignment of inline content inside a block.
text-align value |
Effect |
|---|---|
left / right |
Align to that edge |
center |
Centre each line |
justify |
Stretch lines so both edges are straight |
start / end |
Follow the writing direction (left/right in English) |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
p { background: #f1f5ff; padding: 6px; margin: 6px 0; }
.l { text-align: left; color: #1565c0; }
.c { text-align: center; color: #2e7d32; }
.r { text-align: right; color: #c62828; }
.j { text-align: justify; color: #6a1b9a; max-width: 320px; }
</style>
</head>
<body>
<p class="l">Left aligned</p>
<p class="c">Centered</p>
<p class="r">Right aligned</p>
<p class="j">Justified text spreads the words on each line so that both the left edge and the right edge are perfectly straight, like a newspaper column.</p>
</body>
</html>Decoration and transformation
text-decoration draws lines on text. It's a shorthand for text-decoration-line, -color, -style and -thickness. text-transform changes capitalisation without editing the HTML.
| Property | Values |
|---|---|
text-decoration-line |
none, underline, overline, line-through |
text-decoration-style |
solid, double, dotted, dashed, wavy |
text-underline-offset |
distance of the underline from the text |
text-transform |
uppercase, lowercase, capitalize, none |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 20px; }
.u { text-decoration: underline wavy #e91e63 2px; text-underline-offset: 4px; }
.o { text-decoration: overline #009688 3px; }
.s { text-decoration: line-through; color: #888; }
.up { text-transform: uppercase; letter-spacing: 2px; }
.cap { text-transform: capitalize; }
</style>
</head>
<body>
<p class="u">Wavy pink underline</p>
<p class="o">Overline</p>
<p class="s">Old price: $40</p>
<p class="up">shouting quietly</p>
<p class="cap">every word gets a capital</p>
</body>
</html>Tip: Avoid underlining text that isn't a link — readers will try to click it.
Spacing
| Property | Controls |
|---|---|
line-height |
Height of each line. Unitless values like 1.5 scale with font size and are recommended. |
letter-spacing |
Space between characters |
word-spacing |
Space between words |
text-indent |
Indent of the first line |
white-space |
How spaces and line breaks are handled (normal, nowrap, pre, pre-wrap) |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Georgia, serif; max-width: 380px; }
.tight { line-height: 1; background: #fff0f0; }
.airy { line-height: 1.8; text-indent: 30px; background: #f0fff4; }
.wide { letter-spacing: 6px; word-spacing: 14px; }
.nowrap { white-space: nowrap; background: #fffbe6; overflow: hidden; text-overflow: ellipsis; }
</style>
</head>
<body>
<p class="tight">Line-height 1 makes lines touch each other and is hard to read over several lines of text.</p>
<p class="airy">Line-height 1.8 with a first-line indent feels calm and readable across multiple lines.</p>
<p class="wide">Wide spacing</p>
<p class="nowrap">This long line refuses to wrap and is cut off with an ellipsis at the end…</p>
</body>
</html>Text shadow and direction
text-shadow takes x-offset y-offset blur color and can be repeated with commas. direction: rtl together with unicode-bidi supports right-to-left languages, though the HTML dir attribute is usually preferred.
<!DOCTYPE html>
<html>
<head>
<style>
body { background: #222; font-family: system-ui, sans-serif; text-align: center; padding-top: 20px; }
h1 { color: white; font-size: 44px; text-shadow: 3px 3px 0 #ff4081; margin: 10px; }
h2 { color: #fff; font-size: 34px; text-shadow: 0 0 6px #00e5ff, 0 0 18px #00e5ff; margin: 10px; }
</style>
</head>
<body>
<h1>Offset shadow</h1>
<h2>Neon glow</h2>
</body>
</html>Test yourself
Which property makes text ALL CAPS without changing the HTML?
- font-variant: caps
- text-transform: uppercase
- text-style: upper
- text-case: upper
Why is line-height: 1.5 preferred over line-height: 24px?
- px is not allowed
- The unitless value scales with each element's font size
- It is faster
- It removes margins
Which value of text-align stretches lines so both edges are straight?
- center
- stretch
- justify
- full