CSS: CSS Text & Fonts

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

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 do I change font size in CSS?
A Directly: font-size: 16px;. Better: rem (relative to the root) — set html { font-size: 16px; } then .title { font-size: 1.5rem; }, so scaling the whole page means changing one line. Headings at 2rem and body at 1rem keep the hierarchy obvious.
Q How do I add fonts to CSS?
A With a font stack: font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;. The browser walks left to right and uses the first installed font, so always end with a generic fallback (sans-serif / serif / monospace). Names with spaces need quotes. Don't declare a single font name — if the user's system lacks it, you drop to the browser default.
Q My font isn't showing up / custom font won't load — why?
A Two scenarios. Custom font (@font-face) not loading: check whether src: url(...) 404s in the Network panel, whether the format is right (woff2 first), and whether the font-family name matches the @font-face declaration exactly. Regular font not applying: usually a misspelled name, missing quotes ("Microsoft YaHei"), or a higher-specificity rule winning. The Computed panel in DevTools shows which font actually renders.
Q How do I make font size responsive?
A Three moves: a rem base — change html { font-size } inside a media query and all text scales; clamp(1rem, 2.5vw, 1.5rem) lets the size flow with viewport width between floor and ceiling in one line; big headings can use vw, but pair it with min/max so text stays legible on small screens.
Q How do I fix cramped line spacing?
A line-height: 1.6; (no unit) — spacing becomes 1.6× the font size. Body text reads best at 1.5–1.8, and Chinese content shouldn't go below 1.5. Hard-coding line-height in px bites you when font sizes change; unitless values travel with the text. Tight UI like button labels can drop to 1.2.
Q How do I center text in CSS?
A Horizontal centering for a line of text: text-align: center; on the parent. To center the block element itself: margin: 0 auto; with a set width. For both horizontal and vertical centering inside a container, flexbox is the one-liner: display: flex; align-items: center; justify-content: center;.
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.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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