Text Properties - Alignment, Decoration & Effects

What You'll Learn

  • Control horizontal alignment with text-align for left, center, right, and justified text
  • Apply text decorations with text-decoration and modern control properties
  • Transform text case with text-transform for uppercase, lowercase, and capitalize
  • Indent paragraphs with text-indent for traditional book formatting
  • Adjust character spacing with letter-spacing and word-spacing
  • Control whitespace handling with white-space and truncation with text-overflow
  • Create text effects using text-shadow from simple drops to complex glows
  • Align inline elements vertically with vertical-align
  • Use modern text-wrap: balance and text-wrap: pretty for better typography

Introduction to Text Properties

Text properties control how text is aligned, decorated, spaced, and displayed. While font properties (lesson 1) control the typeface and size, text properties handle the finer details of presentation: alignment, decorative lines, case transformation, spacing adjustments, and special effects.

These properties are essential for creating polished, readable typography. From the basics like text alignment to modern features like balanced wrapping, mastering text properties gives you precise control over text appearance and behavior.

text-align - Horizontal Alignment

The text-align property controls horizontal alignment of text and inline content within its container.

/* Basic alignment */ text-align: left; /* Default for LTR languages */ text-align: center; /* Centered */ text-align: right; /* Right-aligned */ text-align: justify; /* Even edges, adjusts spacing */ /* Practical examples */ h1, h2, h3 { text-align: center; /* Center headings */ } .price { text-align: right; /* Right-align numbers */ } article p { text-align: justify; /* Justify body text */ }

text-decoration - Lines & Styles

The text-decoration property adds decorative lines to text: underlines, overlines, and strikethroughs.

/* Basic decorations */ text-decoration: none; /* Remove (for links) */ text-decoration: underline; /* Underline */ text-decoration: overline; /* Line above */ text-decoration: line-through; /* Strikethrough */ /* Shorthand: line style color */ text-decoration: underline wavy red; text-decoration: underline dotted blue; text-decoration: underline dashed green; text-decoration: underline double purple; text-decoration: underline overline #2196f3;

Modern Decoration Control

CSS provides fine-grained control over text decorations with dedicated properties for color, style, thickness, and positioning.

/* Individual control properties */ text-decoration: underline; text-decoration-color: #2196f3; /* Custom color */ text-decoration-style: wavy; /* solid|double|dotted|dashed|wavy */ text-decoration-thickness: 3px; /* Line thickness */ text-underline-offset: 5px; /* Distance from text */ /* Animated underline on hover */ a { text-decoration: underline; text-decoration-color: transparent; text-underline-offset: 4px; transition: text-decoration-color 0.3s; } a:hover { text-decoration-color: #2196f3; }

text-transform - Case Conversion

The text-transform property changes text capitalization without modifying the HTML content.

text-transform: uppercase; /* THIS BECOMES UPPERCASE */ text-transform: lowercase; /* this becomes lowercase */ text-transform: capitalize; /* This Capitalizes Each Word */ text-transform: none; /* Original case */ /* Common use cases */ button { text-transform: uppercase; /* BUTTON TEXT */ letter-spacing: 0.1em; /* Add spacing for caps */ } .subtitle { text-transform: lowercase; /* subtle subtitle */ } nav a { text-transform: capitalize; /* Menu Links */ }

text-indent - First-Line Indentation

The text-indent property indents the first line of text in a block element, commonly used in traditional book formatting.

/* Standard indent */ p { text-indent: 2em; /* Indent first line */ } /* Don't indent first paragraph after heading */ h1 + p, h2 + p { text-indent: 0; } /* Hanging indent (negative + padding) */ .bibliography { text-indent: -2em; padding-left: 2em; }

letter-spacing & word-spacing

The letter-spacing property (tracking) controls space between characters, while word-spacing adjusts space between words.

/* Letter spacing (tracking) */ letter-spacing: -0.05em; /* Tighter */ letter-spacing: normal; /* Default */ letter-spacing: 0.1em; /* Looser */ letter-spacing: 0.3em; /* Very wide */ /* Common use cases */ h1 { text-transform: uppercase; letter-spacing: 0.15em; /* Improve all-caps readability */ } .logo { letter-spacing: 0.2em; /* Spaced-out branding */ } /* Word spacing (rarely used) */ word-spacing: 0.5em; /* Increase space between words */

white-space - Whitespace Handling

The white-space property controls how whitespace (spaces, tabs, line breaks) is handled and whether text wraps.

white-space: normal; /* Collapse whitespace, wrap lines */ white-space: nowrap; /* Collapse whitespace, no wrapping */ white-space: pre; /* Preserve whitespace, no wrapping */ white-space: pre-line; /* Collapse spaces, preserve breaks, wrap */ white-space: pre-wrap; /* Preserve whitespace, wrap lines */ /* Common use cases */ .code-block { white-space: pre; /* Like <pre> tag */ } .single-line { white-space: nowrap; /* Never wrap */ overflow: hidden; text-overflow: ellipsis; } .poetry { white-space: pre-line; /* Preserve line breaks only */ }

text-overflow - Truncating Text

The text-overflow property controls how overflowing text is displayed when it exceeds its container. Requires white-space: nowrap and overflow: hidden.

/* Single-line truncation with ellipsis */ .truncate { white-space: nowrap; /* Don't wrap */ overflow: hidden; /* Hide overflow */ text-overflow: ellipsis; /* Show ... */ } /* Clip (cut off abruptly) */ .clip { white-space: nowrap; overflow: hidden; text-overflow: clip; /* No ellipsis */ } /* Multi-line truncation (3 lines) */ .truncate-3 { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }

text-shadow - Shadow Effects

The text-shadow property adds shadow effects to text. Syntax: x-offset y-offset blur-radius color. Multiple shadows separated by commas.

/* Simple drop shadow */ text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); /* Glow effect (no offset, multiple blurs) */ text-shadow: 0 0 10px #ffd700, 0 0 20px #ffd700, 0 0 30px #ffd700; /* 3D effect (layered shadows) */ text-shadow: 1px 1px 0 #93c5fd, 2px 2px 0 #60a5fa, 3px 3px 0 #3b82f6, 4px 4px 0 #2563eb, 5px 5px 0 #1d4ed8, 6px 6px 10px rgba(0, 0, 0, 0.4); /* Embossed effect (light + dark) */ text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.3);

vertical-align - Inline Alignment

The vertical-align property controls vertical alignment of inline and inline-block elements relative to their line box.

vertical-align: baseline; /* Default, align baseline */ vertical-align: super; /* Superscript position */ vertical-align: sub; /* Subscript position */ vertical-align: top; /* Align to line-box top */ vertical-align: middle; /* Middle of line-box */ vertical-align: bottom; /* Align to line-box bottom */ vertical-align: text-top; /* Align to font top */ /* Numeric values */ vertical-align: 5px; /* Raise 5px */ vertical-align: -5px; /* Lower 5px */ vertical-align: 0.5em; /* Relative to font size */ /* Common use cases */ .icon { vertical-align: middle; /* Align icon with text */ } .footnote { vertical-align: super; font-size: 0.75em; }

text-wrap: balance & pretty (Modern!)

The text-wrap property provides modern control over text wrapping behavior. Two values dramatically improve typography with minimal effort.

/* Balance - for headings (prevents orphans) */ h1, h2, h3, h4, h5, h6 { text-wrap: balance; /* Balances text across lines, max 4 lines */ } /* Pretty - for paragraphs (better breaks) */ p { text-wrap: pretty; /* Prevents orphans, improves line breaks */ } /* Other values */ text-wrap: wrap; /* Default, normal wrapping */ text-wrap: nowrap; /* Never wrap */ text-wrap: stable; /* Minimize reflow on edits */

Key Takeaways

  • text-align: Controls horizontal alignment (left, center, right, justify)
  • text-decoration: Adds underlines, overlines, strikethroughs with customizable styles
  • Modern decorations: Use text-decoration-color, text-decoration-thickness, and text-underline-offset for fine control
  • text-transform: Changes case (uppercase, lowercase, capitalize) without modifying HTML
  • text-indent: Indents first line; common in traditional book formatting
  • letter-spacing: Adjust character spacing; increase for all-caps text (0.05em-0.15em)
  • word-spacing: Adjust word spacing; use sparingly
  • white-space: Controls whitespace handling (normal, nowrap, pre, pre-line, pre-wrap)
  • text-overflow: Truncate with ellipsis; requires nowrap and overflow: hidden
  • text-shadow: Multiple shadows for drops, glows, 3D, and embossed effects
  • vertical-align: Only works with inline/inline-block elements, not blocks
  • text-wrap: balance: Use on headings to prevent orphans (Chrome 114+)
  • text-wrap: pretty: Use on paragraphs for better line breaks (Chrome 117+)
  • Justified text works best with wider columns and hyphens: auto
  • Multi-line truncation uses -webkit-line-clamp (widely supported)
  • Accessibility: avoid excessive spacing that reduces readability