萬盛學電腦網

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

CSS 文字下劃線完整指南:text-decoration、border-bottom、偽元素三大方案

給文字加下劃線看似簡單,實則有 text-decoration、border-bottom、偽元素三大技術路線,各有優劣。本文系統對比三種方案的能力邊界、瀏覽器支援、無障礙表現,助你按需選擇。

方案對比總覽

特性text-decorationborder-bottom偽元素 ::after
線型solid, double, dotted, dashed, wavysolid, double, dotted, dashed, groove, ridge, inset, outset任意(漸變、圖片、SVG、動畫)
顏色text-decoration-color(可與文字色分離)border-color(獨立控制)完全自由
距離控制text-underline-offsetpadding-bottombottom / transform
粗細控制text-decoration-thicknessborder-width完全自由
換行行為每行獨立底線(原生)每行獨立底線需 box-decoration-break: clone 或 display:inline-block
瀏覽器支援現代瀏覽器全支援(IE 不支援新屬性)全瀏覽器(IE6+)全瀏覽器(IE9+ 偽元素)
無障礙/語意最佳(原生語意)良好需 ARIA 輔助
動畫/過渡支援色/粗/距離動畫支援色/粗/樣式動畫最豐富(寬度展開、漸變移動等)

方案一:text-decoration(語意正確、現代優先)

基礎用法

.underline-basic {
  text-decoration: underline;
  /* 現代增強屬性 */
  text-decoration-color: #667eea;      /* 線條顏色 */
  text-decoration-style: wavy;         /* solid | double | dotted | dashed | wavy */
  text-decoration-thickness: 2px;      /* 線條粗細 */
  text-underline-offset: 4px;          /* 與文字距離 */
  /* 簡寫 */
  text-decoration: underline 2px wavy #667eea;
  text-underline-offset: 4px;
}

狀態變化

.link-modern {
  text-decoration: underline;
  text-decoration-color: #ccc;
  text-underline-offset: 3px;
  transition: text-decoration-color 0.2s, text-decoration-style 0.2s;
}
.link-modern:hover {
  text-decoration-color: #667eea;
  text-decoration-style: solid;
}

⚠️ IE/舊版 Edge 不支援 text-decoration-color/style/thickness/offset,需降級或用 PostCSS 插件自動轉換。

方案二:border-bottom(相容性最廣、控制力強)

基礎用法

.underline-border {
  text-decoration: none;           /* 移除原生 */
  border-bottom: 1px solid #333;   /* 線條樣式 */
  padding-bottom: 2px;             /* 距離控制 */
  color: #333;                     /* 文字顏色 */
}
/* 文字紅、底線藍 */
.underline-color-split {
  border-bottom: 2px solid #00f;
  color: #c00;
}

虛線/點線/雙線

.dashed { border-bottom: 1px dashed #666; }
.dotted { border-bottom: 1px dotted #666; }
.double { border-bottom: 3px double #666; }

方案三:偽元素 ::after(極致控制、動畫豐富)

基礎底線

.underline-pseudo {
  text-decoration: none;
  position: relative;
  padding-bottom: 4px;
}
.underline-pseudo::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 2px;
  background: #667eea;
}

進階:滑入動畫

.underline-slide::after {
  transform: scaleX(0);
  transform-origin: right;
  transition: transform 0.3s cubic-bezier(0.25, 1, 0.5, 1);
}
.underline-slide:hover::after {
  transform: scaleX(1);
  transform-origin: left;
}

進階:漸變底線

.underline-gradient::after {
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb);
  background-size: 200% 100%;
  animation: gradient-flow 3s linear infinite;
}
@keyframes gradient-flow {
  to { background-position: -200% 0; }
}

換行場景處理

/* 方案:box-decoration-break: clone (現代) */
.underline-break {
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone; /* border-bottom、偽元素在換行處各自繪製 */
}

/* 方案:inline-block 強制不換行(不推薦長文) */
.underline-nowrap { display: inline-block; }

選擇決策樹

  1. 需支援 IE / 舊版瀏覽器? → border-bottom
  2. 僅需虛線/波浪線/顏色/距離,現代瀏覽器? → text-decoration-* 系列
  3. 需漸變底線、滑入動畫、寬度動態變化、非矩形底線? → 偽元素 ::after
  4. 需語意完美、螢幕閱讀器識別、列印樣式自動生效? → text-decoration

總結:現代專案建議**預設使用 text-decoration 系列**,語意正確、代碼最少、無障礙最佳;相容性受限時退回 border-bottom;極致視覺效果用偽元素。三種方案可組合:基礎語意用 text-decoration,Hover 增強用偽元素動畫。

copyright © 萬盛學電腦網 all rights reserved