預設 text-decoration: underline 無法控制線型(虛線/點線)、顏色、與文字距離。巧用 border-bottom 即可實現完全可控的連結下劃線效果,包含懸停變實線、顏色變化、動畫過渡等豐富互動。
text-decoration: noneborder-bottom 模擬下劃線:可控寬度、樣式、顏色、距離(padding-bottom)<style>
.link-dashed {
text-decoration: none; /* 關鍵:移除原生 */
border-bottom: 1px dashed #ccc; /* 虛線、顏色、寬度 */
color: #c00; /* 文字顏色 */
padding-bottom: 2px; /* 控制線與文字距離 */
transition: border-color 0.2s, border-style 0.2s, color 0.2s;
}
.link-dashed:hover {
border-bottom: 1px solid #c00; /* 懸停變實線、變色 */
color: #666;
}
</style>
<a href="https://example.com" class="link-dashed">範例連結:虛線下劃線,懸停變實線紅色</a>
.link-style-a {
text-decoration: none;
border-bottom: 1px dashed #00f; /* 藍色虛線 */
color: #090; /* 綠色文字 */
}
.link-style-a:hover {
border-bottom-style: solid; /* 僅改樣式 */
color: #000;
}
.link-anim {
text-decoration: none;
border-bottom: 2px dashed #667eea;
color: #333;
padding-bottom: 3px;
transition: border-color 0.3s, border-style 0.3s, color 0.3s;
}
.link-anim:hover {
border-bottom: 2px solid #764ba2;
color: #764ba2;
}
.link-slide {
text-decoration: none;
color: #333;
position: relative;
padding-bottom: 4px;
}
.link-slide::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 2px;
background: linear-gradient(90deg, #667eea, #764ba2);
transform: scaleX(0);
transform-origin: right;
transition: transform 0.3s ease;
}
.link-slide:hover::after {
transform: scaleX(1);
transform-origin: left;
}
border-bottom 在換行處會中斷,每行各自有底線。若需單一連續底線,需配合 display: inline-block 或 box-decoration-break: clone(支援度有限)。:focus 樣式,鍵盤用戶可見焦點:.link-dashed:focus { outline: 2px solid #03c; outline-offset: 2px; }border 可能被系統色覆蓋,測試時請開啟高對比度驗證。@media print { a::after { content: " (" attr(href) ")"; } } 補充 URL,底線在列印時通常保留。CSS Text Decoration Module Level 3/4 新增屬性,現代瀏覽器已支援:
.modern-underline {
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;
}
/* 懸停變實線 */
.modern-underline:hover {
text-decoration-style: solid;
text-decoration-color: #764ba2;
}
✅ 優勢:自動處理換行、支援 wavy 波浪線、語意正確、無需偽元素。
⚠️ 支援度:Chrome 57+、Firefox 36+、Safari 12.1+、Edge 79+。IE 不支援,需降級方案。
選擇建議:
text-decoration,進階互動用偽元素增強