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 Decoration
Add visual effects like underlines, overlines, or strike-through.
Text Transform
Change the capitalization of text content.
Text Spacing
Control spacing between letters, words, and lines.
Text Color
Set the color of text using various color formats.
Text Shadow
Add shadow effects to make text stand out.
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.
White Space Control
normalCollapse whitespacenowrapNo line breaksprePreserve all whitespacepre-wrapPreserve + wrapText Direction
ltrLeft to rightrtlRight to leftautoBrowser determinesTypography 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
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.
🎯 Quick Check
Which property adds trailing dots when text overflows its container?
CSS Text Styling: summary
Key points
- Write
line-heightwithout a unit (1.5) so it scales with each element's font size. text-wrap: balanceevens out line lengths in headings;text-wrap: prettyavoids single-word last lines in paragraphs.- Truncating to several lines works with
-webkit-line-clampplusdisplay: -webkit-box. text-decoration-thicknessandtext-underline-offsetgive 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.