萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 字體與文字屬性完整參考:Font、Text、Line-height、裝飾、轉換、間距

字體屬性

屬性值/範例說明繼承
font-family"PingFang TC", "Microsoft JhengHei", sans-serif字體堆疊,逗號分隔備選是
font-size16px, 1rem, clamp(1rem, 2vw, 1.5rem)字號,建議用相對單位是
font-weightnormal/bold/100-900粗細,Variable Font 支援任意值是
font-stylenormal, italic, oblique 10deg斜體/傾斜是
font-stretchcondensed, expanded, 100%字寬(需字體支援)是
font-variantsmall-caps, normal小型大寫字母是
fontitalic 600 1rem/1.5 "Font"簡寫:style variant weight stretch size/line-height family是
font-feature-settings"liga" 1, "kern" 1, "tnum" 1OpenType 特性(合字、等寬數字等)是
font-variation-settings"wght" 400, "wdth" 100Variable Font 軸控制是

文字排版屬性

屬性值/範例說明繼承
line-height1.5, 1.5rem, normal行高,無單位數值為乘數(推薦)是
text-alignleft, center, right, justify, start, end水平對齊,start/end 邏輯屬性支援 RTL是
text-align-lastauto, center, justify最後一行/強制換行行的對齊是
vertical-alignbaseline, middle, top, bottom, sub, super, text-top, text-bottom, 10px行內元素垂直對齊基準否
text-indent2em, 24px, -9999px首行縮排,負值常用隱藏文字是
letter-spacing0.05em, 1px, -0.02em字符間距(追蹤)是
word-spacing0.25em, 4px單詞間距是
white-spacenormal, nowrap, pre, pre-wrap, pre-line, break-spaces空白處理與換行規則是
word-breaknormal, break-all, keep-all, break-word單詞斷行規則是
overflow-wrap / word-wrapnormal, break-word, anywhere長詞溢出換行是
hyphensnone, manual, auto自動連字符斷詞(需 lang 屬性)是
writing-modehorizontal-tb, vertical-rl, vertical-lr, sideways-rl文字書寫方向是
text-orientationmixed, upright, sideways直排時字符朝向是

文字裝飾與轉換

屬性值說明
text-decorationunderline, overline, line-through, blink, none簡寫:line style color thickness
text-decoration-lineunderline 等線條位置
text-decoration-stylesolid, double, dotted, dashed, wavy線條樣式
text-decoration-colorred, currentColor線條顏色
text-decoration-thickness2px, 0.1em, auto線條粗細
text-decoration-skip-inkauto, none下劃線避開下延筆畫
text-transformuppercase, lowercase, capitalize, full-width, full-size-kana, none大小寫轉換
text-shadow2px 2px 4px rgba(0,0,0,0.3)陰影:x y blur color,可多層逗號分隔
text-overflowclip, ellipsis, "…"溢出省略標記(需配合 overflow:hidden, white-space:nowrap)

現代最佳實踐

  • 系統字體堆疊:font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; 零下載、原生渲染。
  • 流體排版:font-size: clamp(1rem, 2vw + 0.5rem, 2rem); 無媒體查詢自適應。
  • 行高無單位:line-height: 1.5; 子元素繼承乘數而非固定像素,避免行高崩壞。
  • 邏輯屬性:margin-inline-start, padding-block, border-inline-end 等,自適應 RTL/LTR。
  • 變數化設計 Token:--font-sans: ...; --text-base: 1rem; --leading-relaxed: 1.625;。

常見組合片段

/* 截斷單行文字 */
.truncate {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

/* 截斷多行文字 (WebKit/Blink) */
.line-clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 強制不換行、超出顯示省略 */
.nowrap-ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 保留空白與換行(如代碼、詩歌) */
.pre-wrap { white-space: pre-wrap; }

/* 文字選取樣式 */
::selection { background: var(--brand-primary); color: #fff; }

總結

CSS 文字屬性涵蓋字體選擇、度量、排版、裝飾、轉換、溢出處理全鏈路。現代開發應:優先系統字體、相對單位、無單位行高、邏輯屬性、CSS 變數管理設計 Token,配合 clamp() 實現流體排版,用 text-overflow/line-clamp 處理截斷,構建可維護、無障礙、跨語言友善的文字系統。

copyright © 萬盛學電腦網 all rights reserved