line-height: normal | | | | inherit
初始值: normal (約 1.2) | 繼承: 是 | 適用: 所有元素
推薦使用無單位數值 (如 1.4),表示與 font-size 的倍數關係,繼承時按比例計算,避免固定像素值導致的行高異常。
無 ::nth-line 選擇器,利用背景漸變模擬:
.text {
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-image: linear-gradient(
to bottom,
#9588DD, #9588DD 22px, /* 第1行 */
#DD88C8, #DD88C8 44px, /* 第2行 */
#D3DD88, #D3DD88 66px, /* 第3行 */
#88B0DD, #88B0DD /* 第4行 */
);
line-height: 22px; /* 必須固定值配合色標位置 */
}
SCSS 變量化:
$lh: 1.4em;
@supports (-webkit-background-clip: text) {
p {
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-image: linear-gradient(
to bottom,
#9588DD, #9588DD #{$lh},
#DD88C8 #{$lh}, #DD88C8 #{$lh*2},
#D3DD88 #{$lh*2}, #D3DD88 #{$lh*3},
#88B0DD #{$lh*3}, #88B0DD
);
}
}
配合 calc() 和 background-position: bottom center:
.text {
background-image: linear-gradient(
to bottom,
rgba(white,0.8), rgba(white,0.8) calc(100% - 66px),
rgba(white,0.6) calc(100% - 66px), rgba(white,0.6) calc(100% - 44px),
rgba(white,0.4) calc(100% - 44px), rgba(white,0.4) calc(100% - 22px),
rgba(white,0.2) calc(100% - 22px), rgba(white,0.2)
);
background-position: bottom center;
}
.parent {
padding: $lh*2;
background: #082838;
background-image: repeating-linear-gradient(
to bottom,
rgba(white,0) 0,
rgba(white,0) #{$lh/1em*16px - 1},
rgba(white,0.1) #{$lh/1em*16px - 1},
rgba(white,0.1) #{$lh/1em*16px}
);
}
原理:repeating-linear-gradient 每 line-height 重複,留 1px 顯色形成細線。padding 同步基於 line-height 保證對齊。
.text {
background-image: url(icon.svg);
background-size: $lh $lh; /* 寬高同 line-height */
background-repeat: repeat-y;
padding-left: $lh*2; /* 留出圖標空間 */
}
background-clip: text:WebKit/Blink 內核支援,Firefox 需 layout.css.background-clip-text.enabled@supports 封裝增強樣式