CSS Text Styling
Chapter 6 covered the font — how text is drawn. This chapter covers everything else: how text is aligned, decorated, transformed, shadowed and how it behaves when it runs out of space.
text-align — horizontal
alignmenttext-decoration — underlines,
strikethroughstext-transform — uppercase,
lowercase, capitalizetext-shadow — drop shadows and
glow effectstext-overflow, white-space, overflow-wraptext-indent, gradient text,
writing-mode
text-align — Horizontal Alignment
text-align controls how inline content (text,
images, inline elements) is positioned horizontally within its block container. It does not
align the block element itself — it aligns the content inside the block.
p { text-align: left; } /* default for LTR languages */
h1, h2 { text-align: center; } /* centred headings */
.caption { text-align: right; } /* right-aligned caption */
.article p{ text-align: justify; } /* newspaper-style justified */
/* start / end — logical properties (respects RTL languages) */
p { text-align: start; } /* left in LTR, right in RTL */
p { text-align: end; } /* right in LTR, left in RTL */
/* last line alignment for justified text */
.article p{
text-align: justify;
text-align-last: left; /* last line stays left */
}
text-align: justify spreads words to fill the full line width, which can
create awkward gaps called "rivers of white space" — especially on narrow columns or with long words. Always
test justified text on mobile widths.
text-decoration — Underlines & Strikethroughs
text-decoration is a shorthand for four
sub-properties that control lines drawn on text. You can control the type, colour,
style and thickness of each line independently.
/* Shorthand: line | style | color | thickness */
a { text-decoration: none; } /* remove link underline */
a:hover { text-decoration: underline; } /* restore on hover */
del { text-decoration: line-through; } /* strikethrough */
.price { text-decoration: line-through #ef4444; } /* red strikethrough */
.abbr { text-decoration: underline dotted; } /* dotted tooltip hint */
.error { text-decoration: underline wavy #ef4444; } /* spellcheck style */
/* Sub-properties for finer control */
.fancy-link {
text-decoration-line: underline;
text-decoration-style: solid;
text-decoration-color: #264de4;
text-decoration-thickness: 2px;
text-underline-offset: 4px; /* space between text and line */
}
text-transform — Case Control
text-transform changes the visual case of text
without modifying the underlying HTML. The HTML source stays unchanged — only the rendered appearance
changes.
.nav-link { text-transform: uppercase; } /* nav items in caps */
.badge { text-transform: uppercase; } /* ALL CAPS badges */
.tag { text-transform: lowercase; } /* lowercase tags */
.card-title{ text-transform: capitalize; } /* Title Case */
.btn { text-transform: uppercase;
letter-spacing: 0.08em; } /* BUTTON TEXT + tracking */
text-transform: uppercase in CSS instead. That way, screen readers read the
text normally, and you can change the visual style later without editing HTML.
text-shadow — Shadows & Glow Effects
text-shadow accepts one or more shadow definitions
separated by commas. Each shadow uses the format offset-x offset-y blur-radius color.
/* text-shadow: offset-x offset-y blur color */
h1 { text-shadow: 2px 2px 4px rgba(0,0,0,.5); }
/* Glow — zero offset, large blur */
.glow { text-shadow: 0 0 20px #4a8af4, 0 0 40px #4a8af4; }
/* Multiple shadows — comma separated */
.shadow-3d {
text-shadow:
1px 1px 0 #264de4,
2px 2px 0 rgba(38,77,228,.6),
3px 3px 0 rgba(38,77,228,.3);
}
/* Subtle legibility boost on photos */
.hero-text { text-shadow: 0 1px 3px rgba(0,0,0,.5); }
/* Remove all shadows */
.no-shadow { text-shadow: none; }
text-overflow, white-space & overflow-wrap
These three properties work together to control what happens when text is too long for its container — whether to clip it, truncate with an ellipsis, or wrap to a new line.
Requires: overflow: hidden + white-space: nowrap
Cuts off text abruptly at border
Breaks long words at container edge
Forces single line — hidden overflow
/* THE TRUNCATION TRIO — all three required */
.truncate {
overflow: hidden; /* ① hide overflow */
white-space: nowrap; /* ② prevent wrapping */
text-overflow: ellipsis; /* ③ show '...' */
}
/* Multi-line truncation (CSS clamp with -webkit) */
.clamp-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3; /* show 3 lines then '...' */
overflow: hidden;
}
/* overflow-wrap — break long URLs or words */
.break-word { overflow-wrap: break-word; }
.break-all { word-break: break-all; } /* more aggressive */
The white-space property
controls how whitespace and line breaks in the HTML source are handled:
| Value | Wraps | Preserves spaces | Preserves line breaks |
|---|---|---|---|
| normal | ✓ | ✗ | ✗ |
| nowrap | ✗ | ✗ | ✗ |
| pre | ✗ | ✓ | ✓ |
| pre-wrap | ✓ | ✓ | ✓ |
| pre-line | ✓ | ✗ | ✓ |
| break-spaces | ✓ | ✓ | ✓ |
text-indent
text-indent sets the indentation of the first line
of a block of text. Positive values indent inward; negative values create a hanging indent
(used for bullet lists and bibliographies).
This paragraph has text-indent: 2em. The first line is indented by two em units, creating a
traditional book-paragraph style. The remaining lines stay at the normal left margin.
/* Indent first line — classic book paragraph */
.article p { text-indent: 2em; }
/* Hanging indent — first line protrudes to the left */
.bibliography li {
padding-left: 2em;
text-indent: -2em; /* negative = hanging indent */
}
/* Hide text accessibly (image replacement technique) */
.logo-text {
text-indent: -9999px;
overflow: hidden;
}
Gradient Text
CSS doesn't have a direct gradient text property, but you can achieve it by combining
background-clip: text with a gradient background. This is one of the
most popular modern CSS effects.
.gradient-text {
background: linear-gradient(90deg, #264de4, #a855f7);
/* Clip background to the text shape */
-webkit-background-clip: text; /* required for Safari/Chrome */
background-clip: text;
/* Make the text fill transparent so background shows through */
-webkit-text-fill-color: transparent;
color: transparent;
}
/* Animated gradient text */
@keyframes gradientShift {
0% { background-position: 0%; }
100% { background-position: 200%; }
}
.animated-gradient {
background: linear-gradient(90deg, #264de4, #a855f7, #264de4);
background-size: 200%;
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
animation: gradientShift 3s linear infinite;
}
writing-mode & Vertical Text
writing-mode changes the direction text flows —
enabling vertical text for Asian typography, rotated labels, or creative design effects.
(default)
/* Default horizontal flow (left-to-right, top-to-bottom) */
p { writing-mode: horizontal-tb; }
/* Vertical text — right column first (CJK text direction) */
.vertical-label {
writing-mode: vertical-rl;
text-orientation: mixed; /* upright CJK, rotated Latin */
}
/* Rotate sidebar label — useful for kanban columns */
.sidebar-label {
writing-mode: vertical-lr;
transform: rotate(180deg); /* flip to read bottom-to-top */
}
/* Upright characters (Japanese/Chinese) */
.japanese {
writing-mode: vertical-rl;
text-orientation: upright;
}
All CSS Text Properties — Quick Reference
Try It Yourself
This editor demonstrates all the text properties covered in this chapter working
together on a real article card layout. Try changing text-align,
uncommenting the gradient text or adjusting the shadow.
Quick Check — Chapter 7
1.
overflow: hidden — prevents text from visually
spilling outside the container.2.
white-space: nowrap — prevents text from
wrapping to a second line (without this the text just wraps instead of overflowing).3.
text-overflow: ellipsis — renders the … character at the point where the text is cut.Omitting any one of the three causes the recipe to fail silently.
text-decoration: none on the a selector to remove the underline by default, then restore it on the
hover state:a { text-decoration: none; }a:hover { text-decoration: underline; }You can also use the sub-properties for more control:
text-decoration-color, text-decoration-style (solid/dashed/wavy/dotted/double), text-decoration-thickness and text-underline-offset to customise the underline appearance.
text-shadow syntax is: offset-x offset-y blur-radius color. Multiple shadows are separated by
commas.For a glow effect, use zero offsets and a large blur radius:
text-shadow: 0 0 20px #4a8af4, 0 0 40px #4a8af4;The duplicate shadow at a larger radius makes the glow more intense. For a stronger neon glow, add three layers at progressively larger blur radii. The colour of the text itself should match or complement the shadow colour.
Accessibility — screen readers read ALL-CAPS text letter by letter ("C-S-S" instead of "CSS"), creating a poor experience for visually impaired users.
text-transform: uppercase changes only the visual appearance; the
underlying HTML text is read normally by screen readers.Maintainability — if you later want to change the heading to title case or sentence case, you only change one CSS property instead of editing every heading in your HTML.
Semantic correctness — the HTML represents the content; the CSS represents the presentation. Mixing them by typing in CAPS violates the separation of concerns principle.
1.
background: linear-gradient(90deg, #264de4, #a855f7); — set a gradient
background on the element.2.
-webkit-background-clip: text; background-clip: text; — clip the
background to the text shape only (both prefixed and standard versions needed).3.
-webkit-text-fill-color: transparent; color: transparent; — make the
text fill transparent so the gradient background shows through.The gradient is then visible through the transparent text shape. This works in all modern browsers.
text-transform capitalises the
first letter of every word?text-shadow syntax order?
text-align— aligns inline content horizontally:left,center,right,justify. Usestart/endfor RTL-aware layouts.text-decorationshorthand controls line, style, colour and thickness. Sub-propertytext-underline-offsetcontrols the gap between text and underline.text-transformchanges visual case without modifying HTML. Always use CSS for uppercase labels — never type CAPS in HTML (accessibility concern).text-shadowsyntax:offset-x offset-y blur color. Stack multiple comma-separated shadows for glow, 3D and layered effects.- Truncation trio:
overflow: hidden+white-space: nowrap+text-overflow: ellipsis— all three required for the…effect. overflow-wrap: break-wordbreaks long words at container edges;word-break: break-allis more aggressive.- Gradient text: set a gradient background +
background-clip: text+color: transparent. writing-mode: vertical-rlorvertical-lrenables vertical text for Asian typography and creative side labels.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.