Tutorials CSS Text Styling
Ch 7 / 60
Chapter 7 Beginner 6 min read CSS

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.

What you'll learn
text-align — horizontal alignment
text-decoration — underlines, strikethroughs
text-transform — uppercase, lowercase, capitalize
text-shadow — drop shadows and glow effects
text-overflow, white-space, overflow-wrap
text-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.

left (default)
The quick brown fox jumps over the lazy dog near the riverbank.
center
The quick brown fox jumps over the lazy dog near the riverbank.
right
The quick brown fox jumps over the lazy dog near the riverbank.
justify
The quick brown fox jumps over the lazy dog near the riverbank and through the meadow.
CSS
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 */
}
Justify with care
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.

none
No decoration — link reset
underline
Classic underline
overline
Line above text
line-through
Strikethrough text
underline dotted #264de4
Dotted blue underline
underline wavy #ef4444
Wavy red underline
underline double
Double underline
underline dashed
Dashed underline
CSS
/* 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.

none K2infocom Vadodara — Original case
uppercase K2infocom Vadodara — all uppercase
lowercase K2infocom Vadodara — all lowercase
capitalize K2infocom vadodara — first letter of each word capitalised
CSS
.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 */
Use CSS, not HTML
Never type headings in ALL CAPS in your HTML. Use 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: 2px 2px 4px rgba(0,0,0,.5)
Classic Drop Shadow
text-shadow: 0 0 20px #4a8af4
Neon Glow Effect
Multiple shadows — 3D effect
3D Layered Text
text-shadow: 0 0 1px rgba(0,0,0,.3)
Subtle Legibility
Hard offset — retro style
Hard Retro Shadow
Embossed / letterpress
Embossed Text
CSS — text-shadow syntax
/* 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.

text-overflow: ellipsis
This is a very long text string that cannot fit in the container width

Requires: overflow: hidden + white-space: nowrap

text-overflow: clip
This is a very long text string that cannot fit in the container width

Cuts off text abruptly at border

overflow-wrap: break-word
Thisisaverylongwordwithoutnospacebreaksthatshouldwraptothenextline

Breaks long words at container edge

white-space: nowrap
This text will never wrap to the next line regardless of container width

Forces single line — hidden overflow

CSS — Truncation recipe (must-have trio)
/* 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.

CSS
/* 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 Effect
Fire & Sunset Gradient
Ocean to Forest
CSS — Gradient text technique
.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.

horizontal-tb
(default)
Horizontal Text
vertical-rl
Vertical Text ↓
vertical-lr
Vertical Text ↑
CSS
/* 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

text-align left · center · right · justify · start · end
text-decoration none · underline · overline · line-through · wavy · dotted · dashed · double
text-transform none · uppercase · lowercase · capitalize · full-width
text-shadow offset-x offset-y blur color, multiple shadows…
text-overflow clip · ellipsis — needs overflow: hidden + white-space: nowrap
text-indent 2em (first-line indent) · -2em (hanging indent)
white-space normal · nowrap · pre · pre-wrap · pre-line
overflow-wrap normal · break-word · anywhere
word-break normal · break-all · keep-all · break-word
writing-mode horizontal-tb · vertical-rl · vertical-lr
direction ltr · rtl — text direction for bidirectional text
hyphens none · manual · auto — hyphenate long words
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
One printable page covering every CSS3 property, selector type, unit, layout system and all text styling properties.
Download PDF

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.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js — all in one industry-ready curriculum. Build real projects with AI-powered tools and get placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js Tailwind CSS AI Tools

Quick Check — Chapter 7

Questions & Answers
The three properties that must all be present together are:

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.
Set 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.
The 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.
Three important reasons:

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.
CSS has no direct "gradient text color" property, but you can achieve it with three declarations:

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.
Multiple Choice Questions
Question 1 of 5
Which property aligns text horizontally within its container?
Question 2 of 5
Which CSS declaration removes the underline from a hyperlink?
Question 3 of 5
Which value of text-transform capitalises the first letter of every word?
Question 4 of 5
Which property prevents text from wrapping to a new line?
Question 5 of 5
What is the correct text-shadow syntax order?
Chapter Summary
  • text-align — aligns inline content horizontally: left, center, right, justify. Use start/end for RTL-aware layouts.
  • text-decoration shorthand controls line, style, colour and thickness. Sub-property text-underline-offset controls the gap between text and underline.
  • text-transform changes visual case without modifying HTML. Always use CSS for uppercase labels — never type CAPS in HTML (accessibility concern).
  • text-shadow syntax: 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-word breaks long words at container edges; word-break: break-all is more aggressive.
  • Gradient text: set a gradient background + background-clip: text + color: transparent.
  • writing-mode: vertical-rl or vertical-lr enables vertical text for Asian typography and creative side labels.
~6 min read
Beginner level
Chapter 7 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel