CSS Basics

CSS Text

CSS Text properties control the styling and formatting of textual content, including text-align, text-decoration, text-transform, line-height, letter-spacing, and word-spacing for typography control.

How do you style text in CSS?

Text appearance is controlled by properties like color, text-align, line-height, letter-spacing, text-decoration and text-transform. For readable body text, use a line-height around 1.5 and keep lines about 60–75 characters long, for example max-width: 65ch.

body {
  line-height: 1.6;
  color: #1f2937;
}
p   { max-width: 65ch; }
h1  { text-align: center; letter-spacing: -0.02em; }
.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

Essential Text Properties

Text Alignment

Control how text is positioned within its container.

text-align: left | center | right | justify;

Text Decoration

Add visual effects like underlines, overlines, or strike-through.

text-decoration: underline | overline | line-through | none;

Text Transform

Change the capitalization of text content.

text-transform: uppercase | lowercase | capitalize;

Text Spacing

Control spacing between letters, words, and lines.

letter-spacing: 2px; word-spacing: 5px; line-height: 1.6;

Text Color

Set the color of text using various color formats.

color: #3b82f6; color: rgb(59, 130, 246); color: hsl(217, 91%, 60%);

Text Shadow

Add shadow effects to make text stand out.

text-shadow: 2px 2px 4px rgba(0,0,0,0.3);

Interactive Text Styling Demo

Explore comprehensive text styling with interactive examples. Click, hover, and interact with text elements to see CSS properties in action!

Code Editor

<h2 class="balanced">A heading that wraps onto two balanced lines</h2>
<p class="body">Readable body text uses a comfortable line height and a limited line length of about 65 characters, so the eye can find the next line easily.</p>
<p class="upper">uppercase with letter spacing</p>
<p class="truncate">A single line that is far too long for its box gets an ellipsis at the end</p>

Preview

Advanced Text Features

Text Overflow Control

Handle text that exceeds its container boundaries with ellipsis or clipping.

.text-ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .text-clip { white-space: nowrap; overflow: hidden; text-overflow: clip; }

White Space Control

normalCollapse whitespace
nowrapNo line breaks
prePreserve all whitespace
pre-wrapPreserve + wrap

Text Direction

ltrLeft to right
rtlRight to left
autoBrowser determines

Typography Best Practices

  • •Use relative units (em, rem) for scalable typography
  • •Maintain proper contrast ratios for accessibility
  • •Use consistent line-height (1.4-1.6) for readability
  • •Limit line length to 45-75 characters for optimal reading

Your Learning Path

Master text styling fundamentals

What You've Learned

  • ▶Text alignment and positioning
  • ▶Text decoration styles and effects
  • ▶Text transformation and capitalization
  • ▶Text spacing and typography controls
  • ▶Advanced text features and overflow handling

What's Next

  • ▶CSS Fonts - Font families and web fonts
  • ▶CSS Icons - Icon fonts and SVG icons
  • ▶CSS Links - Link styling and states
  • ▶CSS Lists - List styling and customization

Pro Typography Tip

Create a consistent typography scale for your website using relative units (rem) and establish a clear hierarchy with font sizes, weights, and spacing.

CSS Text — step by step

1 / 4
1

font-size, line-height, letter-spacing

Control text size with px/rem/em, readability with line-height (1.5 is standard), and character spacing with letter-spacing.

1 of 4
🔍line-height Unitless is Best
Prefer unitless line-height values like 1.5 over pixel values — they scale proportionally with font-size, preventing cramped text when users change their browser font settings.

🎯 Quick Check

Which property adds trailing dots when text overflows its container?

CSS Text Styling: summary

Key points

  • Write line-height without a unit (1.5) so it scales with each element's font size.
  • text-wrap: balance evens out line lengths in headings; text-wrap: pretty avoids single-word last lines in paragraphs.
  • Truncating to several lines works with -webkit-line-clamp plus display: -webkit-box.
  • text-decoration-thickness and text-underline-offset give you nicer link underlines.

Frequently asked questions

›How do I add an ellipsis (...) to overflowing text?

For one line, use white-space: nowrap; overflow: hidden; text-overflow: ellipsis; and give the element a width.

›What is a good line-height for body text?

Between 1.4 and 1.7 for body text. Headings usually look better tighter, around 1.1 to 1.3.