CSS Text: Complete Guide to Text Styling Properties

Master CSS text properties for beautiful, readable typography. From basic alignment to advanced text-wrap strategies, learn how to control every aspect of text rendering in modern web design.

Introduction to CSS Text Module

CSS Text properties form the foundation of readable, accessible, and visually appealing web typography. The CSS Text Module, currently at Level 3 with Level 4 in development, provides comprehensive control over how text is displayed, aligned, spaced, and wrapped on web pages. Understanding these properties is essential for creating professional web applications that communicate clearly across all devices and screen sizes.

Modern web development demands precise text control for everything from marketing copy to complex data displays. CSS Text properties work in conjunction with CSS Flexbox to create complete typographic systems. While fonts define the visual appearance of characters, text properties control how those characters are arranged, spaced, and wrapped within their containers. This separation of concerns allows developers to maintain clean, modular stylesheets that are easy to maintain and update.

The CSS Text Module covers several key areas: text transformation (changing case and appearance), text spacing (controlling gaps between characters and words), text alignment (positioning text within containers), text decoration (underlines and other visual annotations), and text wrapping (managing how text breaks across lines). Each area contains multiple properties that can be combined to achieve sophisticated typographic effects.

Text Alignment and Justification

Text alignment determines how text is positioned horizontally within its container. The text-align property is one of the most frequently used CSS Text properties, with four primary values that cover common layout requirements. The left value aligns text to the left edge of the container, which is the default for most left-to-right languages. The right value aligns text to the right edge, commonly used in languages that read right-to-left or for specific design effects. The center value centers text within the container, frequently used for headings, captions, and decorative text. The justify value stretches text to fill the full width of the container by adjusting word spacing, similar to newspaper columns.

The text-align-last property provides fine-grained control over how the last line of text is aligned when text-align: justify is applied. This property is particularly useful for achieving consistent typographic appearance in justified text blocks. For languages with mixed directionality, these controls ensure that justified text remains readable and aesthetically pleasing.

The text-justify property, introduced in CSS Text Module Level 3, offers granular control over how text justification works. The inter-word value distributes space between words, which is the most common justification method in English and similar languages. The inter-character value distributes space between characters, which works better for languages like Japanese that don't use word spaces.

text-alignment.css
1/* Text Alignment Basics */2.text-left { text-align: left; }3.text-center { text-align: center; }4.text-right { text-align: right; }5.text-justify { text-align: justify; }6 7/* Control the last line */8.justify-last-auto { text-align-last: auto; }9.justify-last-center { text-align-last: center; }10 11/* International text justification */12.text-justify-inter { text-justify: inter-word; }13.text-justify-character { text-justify: inter-character; }

Text Transformation

Text transformation properties allow you to change the case and appearance of text without modifying the underlying HTML content. The text-transform property is the primary tool for this purpose, offering five values that cover common typographic requirements. The none value prevents any transformation, useful for resetting inherited transformations. The uppercase value converts all characters to uppercase, commonly used for headings, acronyms, and emphasis text. The lowercase value converts all characters to lowercase, sometimes used for stylized text or to normalize user input visually. The capitalize value capitalizes the first letter of each word.

The full-width value transforms characters to their full-width equivalents, which is primarily used for CJK (Chinese, Japanese, Korean) text in contexts where full-width formatting is required. This property is particularly useful when mixing Latin characters with CJK text, as it ensures consistent character widths for better alignment. Combining text transformation with CSS colors creates visually cohesive typography systems.

Text transformation is particularly valuable for accessibility purposes. Using CSS for transformations rather than modifying the underlying text content ensures that screen readers and other assistive technologies receive the correct text to read aloud. When a screen reader encounters text transformed to uppercase, it typically adjusts its pronunciation accordingly.

text-transformation.css
1/* Text Transformation Values */2.uppercase { text-transform: uppercase; }3.lowercase { text-transform: lowercase; }4.capitalize { text-transform: capitalize; }5.full-width { text-transform: full-width; }6.none { text-transform: none; }7 8/* Common Use Cases */9/* Uppercase buttons and CTAs */10.btn-cta {11 text-transform: uppercase;12 letter-spacing: 0.05em;13}14 15/* Capitalize proper nouns display */16.user-name {17 text-transform: capitalize;18}

Text Spacing and Layout

Controlling the spacing between characters, words, and lines is essential for creating readable and visually balanced typography. The letter-spacing property adjusts the space between characters, accepting positive values to increase spacing and negative values to decrease it. The normal keyword allows the browser to use the default spacing for the font, which varies based on the font design. When specifying explicit values, absolute lengths like em units are recommended as they scale proportionally with font size, maintaining the intended visual relationship.

The word-spacing property similarly controls the space between words, with the normal keyword using the font's default word spacing, typically equal to the width of the space character. Positive values increase word separation, while negative values decrease it. This property is particularly useful for adjusting the density of text blocks or creating visual emphasis through spacing variations.

The line-height property, often called leading in traditional typography, controls the space between lines of text within a block element. A line-height of 1.5 means the line height is 1.5 times the font size, providing comfortable reading spacing for most body text. Headings often use smaller multipliers to maintain visual hierarchy, while decorative text might use larger values for artistic effect.

The text-indent property creates an indent for the first line of text, useful for traditional paragraph formatting, hanging punctuation, or decorative indentation effects. Positive values create a rightward indent, while negative values create a hanging first line where the first character extends into the left margin.

text-spacing-basics.css
1/* Letter Spacing - Character Separation */2.tracking-tight { letter-spacing: -0.02em; }3.tracking-normal { letter-spacing: 0; }4.tracking-wide { letter-spacing: 0.02em; }5.tracking-xwide { letter-spacing: 0.1em; }6 7/* Word Spacing - Word Separation */8.word-spacing-normal { word-spacing: normal; }9.word-spacing-wide { word-spacing: 0.25em; }10 11/* Line Height - Vertical Rhythm */12.leading-tight { line-height: 1.25; }13.leading-normal { line-height: 1.5; }14.leading-relaxed { line-height: 1.75; }15.leading-loose { line-height: 2; }16 17/* Text Indent - First Line */18.indent-sm { text-indent: 1rem; }19.indent-negative { text-indent: -1rem; }
text-spacing-advanced.css
1/* Professional Typography Example */2.prose {3 /* Optimal line height for body text */4 line-height: 1.6;5 6 /* Readable character width */7 letter-spacing: 0.01em;8 9 /* Comfortable word spacing */10 word-spacing: 0.02em;11}12 13.heading-tight {14 /* Tighter tracking for headings */15 letter-spacing: -0.02em;16 line-height: 1.2;17}18 19.blockquote {20 /* Hanging indent for quotes */21 padding-left: 1.5rem;22 text-indent: -1.5rem;23}

Text Decoration Properties

Text decoration properties control the lines, styles, and colors applied to text. The text-decoration shorthand combines text-decoration-line, text-decoration-style, text-decoration-color, and text-decoration-thickness into a single declaration. The text-decoration-line property specifies the type of decoration: underline draws a line below the text, overline draws a line above, and line-through creates a strikethrough effect. Multiple values can be combined to create layered decorations.

The text-decoration-style property defines the visual style of the decoration line. The solid value creates a single solid line, which is the default. The double value creates two parallel lines. The dotted value creates a dotted line, useful for editorial markup or secondary emphasis. The dashed value creates a dashed line, commonly used for indicating deletions. The wavy value creates a wave pattern, typically used to indicate spelling errors or emphasis.

The text-decoration-thickness property, added in CSS Text Level 4, controls the thickness of the decoration line. The auto value allows the browser to choose an appropriate thickness based on the font, while from-font uses the decoration thickness specified in the font metrics. This fine-grained control allows designers to create text decorations that better match the visual weight of the font.

text-decoration.css
1/* Text Decoration Line */2.underline { text-decoration-line: underline; }3.overline { text-decoration-line: overline; }4.line-through { text-decoration-line: line-through; }5 6/* Text Decoration Style */7.solid { text-decoration-style: solid; }8.double-line { text-decoration-style: double; }9.dotted { text-decoration-style: dotted; }10.dashed { text-decoration-style: dashed; }11.wavy { text-decoration-style: wavy; }12 13/* Modern Shorthand */14.modern-link {15 text-decoration: underline wavy #3b82f6 2px;16}17 18/* Skip ink for better readability */19.skip-ink {20 text-decoration-skip-ink: auto;21 text-underline-offset: 2px;22}

Text Overflow and Wrapping

Managing how text behaves when it exceeds its container's boundaries is crucial for creating robust layouts. The overflow-wrap property (formerly known as word-wrap) determines whether the browser can break long words to prevent overflow. The normal value breaks words only at allowed break points, such as spaces or hyphens. The break-word value allows breaking long words at arbitrary points when necessary to prevent overflow.

The word-break property provides more aggressive control over word breaking for different scripts and languages. The break-all value breaks words at any character boundary, useful for languages without word spaces or when preventing overflow is more important than preserving word integrity. The keep-all value prevents breaking CJK text at word boundaries.

The white-space property controls how whitespace within an element is handled. The normal value collapses sequences of whitespace and wraps text normally. The nowrap value collapses whitespace but prevents all line breaks. The pre value preserves all whitespace exactly as written in the source. The pre-wrap value preserves whitespace while still allowing text wrapping.

The text-wrap property, introduced in CSS Text Level 4, provides additional control over text wrapping strategies. The balance value attempts to balance the length of wrapped lines, particularly useful for headings. The pretty value optimizes for readability by using strategies that minimize line lengths and avoid awkward breaks. These properties work hand-in-hand with responsive web design techniques to ensure text remains readable across all device sizes.

text-overflow-basics.css
1/* Overflow Wrap */2.break-word { overflow-wrap: break-word; }3.break-all { word-break: break-all; }4.keep-all { word-break: keep-all; }5 6/* White Space Control */7.whitespace-nowrap { white-space: nowrap; }8.whitespace-pre { white-space: pre; }9.whitespace-pre-wrap { white-space: pre-wrap; }10 11/* Text Overflow Ellipsis */12.truncate {13 overflow: hidden;14 white-space: nowrap;15 text-overflow: ellipsis;16}
text-wrap-level4.css
1/* CSS Text Level 4 - Modern Wrapping */2 3/* Balance line lengths for headings */4.balance-text {5 text-wrap: balance;6}7 8/* Optimize for readability */9.pretty-text {10 text-wrap: pretty;11}12 13/* Hyphenation */14.hyphenated {15 hyphens: auto;16 hyphenate-limit-chars: 6 2 2;17 hyphenate-limit-lines: 3;18}19 20/* Drop cap styling */21.drop-cap::first-letter {22 initial-letter: 3 2;23 font-weight: bold;24}

Hyphenation

Automatic hyphenation improves the appearance of justified text and helps prevent awkward gaps in narrow columns. The hyphens property controls whether hyphenation is applied to text. The none value prevents all hyphenation. The manual value hyphenates only at explicit hyphenation points marked in the content, such as soft hyphens. The auto value enables automatic hyphenation based on language-specific hyphenation rules, which the browser determines from the lang attribute.

For effective automatic hyphenation, proper language declaration is essential. The HTML lang attribute on the document or individual elements tells the browser which hyphenation dictionary to use. Different languages have different hyphenation conventions, and the quality of hyphenation dictionaries varies by browser and language.

CSS Text Level 4 introduces additional hyphenation control properties. The hyphenate-limit-chars property specifies the minimum number of characters required before and after a hyphenation break. The hyphenate-limit-lines property specifies the maximum number of consecutive lines that can end with a hyphen. These properties allow fine-tuning of hyphenation behavior for specific design requirements.

Best Practices and Performance

When working with CSS Text properties, several best practices ensure maintainable, accessible, and performant typography. First, use relative units for text properties to ensure proper scaling across different screen sizes and user preferences. The em unit is ideal for letter-spacing and text-indent as it maintains proportional relationships with font size. The rem unit works well for line-height when you want sizing relative to the root font size.

Accessibility considerations should guide text styling decisions. Ensure sufficient contrast between text and background colors, and avoid relying solely on color to convey meaning in text decoration. Maintain readable line lengths, typically between 45 and 75 characters, by using appropriate container widths. Provide adequate line-height for body text, generally 1.5 or higher, to improve readability for users with dyslexia or visual impairments.

Performance optimization for text rendering involves minimizing layout thrashing and avoiding expensive operations. Most text properties are inexpensive to compute and apply, but changing text-align on large blocks of text can trigger layout recalculations. When animating or transitioning text properties, prefer transforms and opacity changes which can be GPU-accelerated.

Maintaining consistency across a project is easier when text styling follows a systematic approach. Define text style variables for common configurations using CSS custom properties. Use utility classes for common text patterns and document your typographic system to ensure all team members understand the intended usage and hierarchy.

text-performance.css
1/* Performance Considerations */2 3/* Good: Simple text properties are fast */4.performance-friendly {5 text-align: left;6 line-height: 1.5;7 letter-spacing: normal;8}9 10/* Use sparingly: balance has performance cost */11.balance-heading {12 text-wrap: balance;13 /* Only use on headings, not paragraphs */14}15 16/* Good: GPU-friendly text animations */17.animate-text {18 will-change: transform;19}20 21/* Accessibility: Ensure color contrast */22.accessible-link {23 text-decoration: underline;24 text-decoration-color: rgba(59, 130, 246, 0.5);25 /* Must meet WCAG 2.1 AA contrast ratio */26}

Key Best Practices Checklist

Typography Hierarchy

  • Use consistent line-height and letter-spacing across similar elements

Readability

  • Aim for 50-75 characters per line
  • Use text-wrap: pretty for long content

Accessibility

  • Maintain 4.5:1 contrast ratio
  • Don't rely on color alone for meaning

Responsive Design

  • Test text wrapping at multiple viewport sizes
  • Increase line-height and font-size for mobile

Performance

  • Use text-wrap: balance sparingly
  • Enable hyphens: auto for better justified text

Modern CSS Text Level 4 Features

CSS Text Level 4 introduces several powerful features that extend the capabilities of text styling on the web. The text-wrap property with balance and pretty values provides intelligent wrapping that considers typographic quality. The hanging-punctuation property, when supported, allows punctuation marks to extend beyond the text container's boundaries, creating more visually balanced margins.

The initial-letter property simplifies the creation of drop caps and raised caps, a common typographic treatment for article introductions and chapter beginnings. Browser support for initial-letter is growing, making this a viable option for progressive enhancement.

As you build modern web applications, incorporating these CSS Text properties into your workflow will help you create more polished, readable, and accessible user interfaces. Start with the fundamentals--text-align, line-height, and letter-spacing--and progressively adopt newer features like text-wrap: balance and hyphenation as browser support improves.

Need Help Implementing Modern CSS?

Our web development team specializes in building performant, accessible websites using the latest CSS techniques. From typography systems to responsive layouts, we can help you achieve professional results.

Related Resources

Sources

  1. MDN Web Docs - CSS Text Module - Official CSS Text Module Level 3/4 documentation with complete property reference
  2. MDN Web Docs - Wrapping and breaking text - Guide on text overflow management and breaking
  3. CSS-Tricks - overflow-wrap - Practical syntax and browser support information
  4. MDN Web Docs - CSS text styling fundamentals - Foundational guide covering text formatting and typography
  5. CSS Text Module Level 4 Specification - W3C specification for upcoming CSS text features