CSS Text Effects
Beyond colour and size, CSS can control how text behaves when it doesn't fit, how words break, which direction text runs, and even fill letters with gradients. This lesson collects these text effects.
Handling text that doesn't fit
| Property | Values | Purpose |
|---|---|---|
text-overflow |
clip, ellipsis |
How clipped single-line text ends (needs overflow: hidden and white-space: nowrap) |
overflow-wrap |
normal, break-word, anywhere |
Allow long words or URLs to break so they don't overflow |
word-break |
normal, break-all, keep-all |
Breaking rules between characters |
hyphens |
none, manual, auto |
Add hyphens at line breaks (needs a lang attribute) |
-webkit-line-clamp |
number | Limit text to N lines with an ellipsis |
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 12px; }
.box { width: 150px; padding: 6px; border: 2px solid #7e57c2; background: #f3e5f5; font-size: 14px; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wrap { overflow-wrap: anywhere; }
.hyph { hyphens: auto; text-align: justify; }
b { color: #4a148c; }
</style>
</head>
<body>
<div class="box ellipsis"><b>ellipsis:</b> this sentence is far too long for one line</div>
<div class="box"><b>default:</b> https://example.com/a/very/long/unbroken/address</div>
<div class="box wrap"><b>anywhere:</b> https://example.com/a/very/long/unbroken/address</div>
<div class="box hyph"><b>hyphens:</b> Internationalization requires extraordinarily comprehensive documentation.</div>
</body>
</html>Tip: Put
overflow-wrap: break-wordon user-generated content such as comments and chat messages. Someone will always paste a 200-character URL.
Writing modes and orientation
writing-mode turns lines vertical. It's used for East Asian scripts and for space-saving side labels. text-orientation decides whether letters stay upright or lie sideways.
writing-mode |
Lines run |
|---|---|
horizontal-tb |
Horizontally, top to bottom (default) |
vertical-rl |
Vertically, right to left |
vertical-lr |
Vertically, left to right |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 20px; align-items: flex-start; padding: 10px; }
.v { writing-mode: vertical-rl; background: #e0f2f1; padding: 10px; height: 200px; border-left: 4px solid #00897b; }
.up { writing-mode: vertical-rl; text-orientation: upright; background: #fff8e1; padding: 10px; letter-spacing: 4px; font-weight: bold; }
.side { writing-mode: vertical-rl; transform: rotate(180deg); background: #263238; color: white; padding: 12px 6px; font-size: 13px; letter-spacing: 3px; text-transform: uppercase; }
</style>
</head>
<body>
<div class="v">Vertical text flows top to bottom, lines stack right to left.</div>
<div class="up">UPRIGHT</div>
<div class="side">Side label</div>
</body>
</html>Gradient and outlined text
background-clip: text clips a background to the shape of the letters. Make the text colour transparent and the gradient shows through. -webkit-text-stroke draws an outline around each glyph. Despite the prefix, every modern browser supports it.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; text-align: center; margin: 10px; }
.grad { font-size: 54px; font-weight: 900; margin: 0;
background: linear-gradient(90deg, #ff6a00, #ee0979, #8e2de2);
-webkit-background-clip: text; background-clip: text; color: transparent; }
.stroke { font-size: 54px; font-weight: 900; margin: 0; color: transparent; -webkit-text-stroke: 2px #1565c0; }
.both { font-size: 40px; font-weight: 900; margin: 0; color: #ffeb3b; -webkit-text-stroke: 1.5px #212121; text-shadow: 4px 4px 0 #212121; }
</style>
</head>
<body>
<p class="grad">Gradient</p>
<p class="stroke">Outline</p>
<p class="both">Comic Pop!</p>
</body>
</html>Decorative typography helpers
| Property | Example | Effect |
|---|---|---|
::first-letter |
p::first-letter { font-size: 3em; float: left; } |
Drop cap |
font-variant-caps |
small-caps |
Lowercase drawn as small capitals |
text-decoration |
underline wavy red |
Decorative lines |
text-wrap |
balance |
Evens out the line lengths of headings |
mix-blend-mode |
difference |
Text that inverts against the background |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Georgia, serif; max-width: 400px; margin: 10px; }
.story::first-letter { float: left; font-size: 3.4em; line-height: .8; margin: 4px 8px 0 0; color: #c62828; font-weight: bold; }
h2 { text-wrap: balance; font-family: system-ui, sans-serif; font-size: 20px; border-left: 4px solid #c62828; padding-left: 8px; }
.blend { position: relative; height: 60px; background: linear-gradient(90deg, black 50%, white 50%); }
.blend span { position: absolute; inset: 0; display: grid; place-items: center; color: white; mix-blend-mode: difference; font: 900 30px system-ui, sans-serif; }
</style>
</head>
<body>
<h2>A balanced heading wraps into lines of similar length</h2>
<p class="story">Once upon a time, a stylesheet discovered it could make giant decorative first letters, just like old printed books.</p>
<div class="blend"><span>DIFFERENCE</span></div>
</body>
</html>Test yourself
What does text-overflow: ellipsis need in order to work on a single line?
- Nothing else
- white-space: nowrap and overflow: hidden
- display: flex
- hyphens: auto
How do you fill text with a gradient?
- color: linear-gradient(...)
- background: gradient plus background-clip: text and a transparent colour
- text-fill: gradient
- font-gradient: ...
Which property makes lines of text run vertically?
- text-direction
- writing-mode
- text-orientation
- rotate