「CSS 字體底線樣式」相關筆記
總結
記錄一下近期是如何使用 text-decoration 與 text-underline-offset 來調整底線樣式外觀
DEMO STRING
with a little tail
with a little tail
/* color ticket --> https://nipponcolors.com/#kitsune */
text-decoration: underline line-through #9b6e23;
text-underline-offset: 0.25em;
筆記
text-decoration
- syntax 如下:
<'text-decoration-line'> || <'text-decoration-style'> || <'text-decoration-color'> || <'text-decoration-thickness'> text-decoration-line設定裝飾線段的位置- 允許複數值,
text-decoration-line: overline underline line-through;這樣的樣式設定是合法的
- 允許複數值,
text-decoration-style設定裝飾線段的樣式text-decoration-color設定裝飾線段的顏色,注意顏色目前僅支援單一色彩,無法處理linear gradient;若底線需要漸變色,可考慮使用pseudo-element或background-image來處理text-decoration-thickness設定裝飾線段的粗細,需注意目前主流瀏覽器尚未全面支援使用百分比來設定粗細
text-underline-offset
- 沒有包含在
text-decoration的 syntax 內,如果要設定底線的偏移數值,需獨立宣告 - While an element can have multiple
text-decorationlines,text-underline-offsetonly impacts underlining, and not other possible line decoration options such asoverlineorline-through. 注意text-underline-offset僅能設定 underlining 的偏移數值,無法調整overline或line-through的位置- 參考上方 DEMO STRING 可發現
line-through並未偏移字體中心,僅有underline往下方偏移
- 參考上方 DEMO STRING 可發現
- It is recommended to use
emunits so the offset scales with the font size. 推薦使用em作為單位,根據字體大小調整偏移的幅度(而非使用px等單位寫死)