CSS: CSS Text & Fonts

Last updated: 2026-10-13

The main content of web pages is text. CSS provides rich properties to control text font, size, color, spacing, and alignment.

Properties in this section: font-family font-size font-weight font-style line-height font color text-align left right text-decoration text-indent letter-spacing word-spacing text-transform text-shadow text-overflow overflow white-space width max-width

1. Font Properties

(1) Font Properties Quick Reference

Property Common Values Purpose
font-family "Microsoft YaHei", Arial, sans-serif Set font (multiple fallbacks)
font-size 16px, 1.2em, 1rem, 100% Font size
font-weight normal(400), bold(700), 100~900 Font weight
font-style normal, italic, oblique Font style (italic/normal)
line-height 1.5(unitless), 24px, 150% Line height (recommended 1.5-1.8)
font italic bold 16px/1.8 sans-serif Shorthand for multiple properties

(2) Font Stack Best Practice

✅ Font stack order: font-family should go from most specific to most generic, e.g., font-family: "PingFang SC", "Microsoft YaHei", sans-serif;. Browsers try loading from left to right until finding an available font.

▶ Example

▶ Try it Yourself

2. Text Properties

(1) Text Properties Quick Reference

Property Common Values Purpose
color #333, red, rgb(0,0,0) Text color
text-align left, center, right, justify Horizontal alignment
text-decoration none, underline, overline, line-through Text decoration
text-indent 2em, 20px First line indent
letter-spacing 2px, -1px Character spacing
word-spacing 5px Word spacing
text-transform uppercase, lowercase, capitalize Text transformation
text-shadow 2px 2px 4px rgba(0,0,0,0.5) Text shadow
text-overflow clip, ellipsis Overflow handling (needs overflow: hidden)

(2) Text Alignment

▶ Example

▶ Try it Yourself

(3) Text Decoration

▶ Example

▶ Try it Yourself

(4) Text Shadow

⚠️ Readability reminder: While text shadows look nice, overuse reduces readability. Dark background with dark shadow is unreadable — ensure sufficient contrast between text and background.

▶ Example

▶ Try it Yourself

(5) Text Overflow

Combined with overflow and white-space for handling text overflow:

▶ Example

▶ Try it Yourself

Properties in this section: font-family font-size font-weight font-style line-height font color text-align left right text-decoration text-indent letter-spacing word-spacing text-transform text-shadow text-overflow overflow white-space width max-width

❓ FAQ

Q How to set font stack for different languages?
A For Chinese: font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;. Mac uses PingFang SC, Windows uses Microsoft YaHei, fallback to Noto Sans or system sans-serif.
Q Does loading Google Fonts affect speed?
A Yes. Google Fonts requires additional network requests. Only load needed weights (don't load all 300/400/500/700 at once), and add display=swap parameter so browsers use fallback font first, then replace when custom font loads.
Q Why doesn't text-overflow: ellipsis work sometimes?
A Ellipsis needs three conditions simultaneously: white-space: nowrap (no line breaks), overflow: hidden (hide overflow), and width or max-width (finite width). Missing any one prevents it from working.

📖 Summary

📝 Exercises

  1. Create an article reading page with title, subtitle, and body paragraphs. Use different fonts, sizes, and colors to distinguish hierarchy.
  2. Demonstrate at least 4 text-decoration effects (underline, overline, strikethrough, wavy).
  3. Implement "single-line text overflow with ellipsis" — show long text in a fixed-width container with ... for overflow.
  4. Use text-shadow to add a glow effect to a heading.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏