CSS Text & Fonts

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

Font Properties

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

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

HTML
<style>
p {
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
}
</style>
<p>This is a paragraph example.</p>
▶ Try it Yourself

Text Properties

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)

Text Alignment

Example

HTML
<style>
.left { text-align: left; }
.center { text-align: center; }
.right { text-align: right; }
.justify { text-align: justify; }
</style>
<div class="left">Left aligned (default)</div>
<div class="center">Center aligned</div>
<div class="right">Right aligned</div>
<div class="justify">Justified text, auto-adjusts word spacing</div>
▶ Try it Yourself

Text Decoration

Example

HTML
<style>
.underline { text-decoration: underline; }
.overline { text-decoration: overline; }
.line-through { text-decoration: line-through; }
.none { text-decoration: none; }
</style>
<div class="underline">Underlined text</div>
<div class="overline">Overlined text</div>
<div class="line-through">Strikethrough text</div>
<div class="none">No decoration (removes default link underline)</div>
▶ Try it Yourself

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

HTML
<style>
h1 {
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
</style>
<h1>Heading with shadow</h1>
▶ Try it Yourself

Text Overflow

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

Example

HTML
<style>
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 200px;
  border: 1px solid #ccc;
  padding: 8px;
}
</style>
<div class="ellipsis">This is a long text that will be truncated with ellipsis</div>
▶ Try it Yourself

❓ 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.
100%

🙏 帮我们做得更好

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

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