兩個完全重疊的文字層,分別設定不同顏色,利用 clip: rect() 裁切上半部與下半部,形成上下雙色效果。
<style>
.textBottom {
color: #0000cc;
position: absolute;
left: 3em; top: 1em;
font: 26px "Century Gothic", Arial, Helvetica, sans-serif;
clip: rect(18px auto auto auto); /* 保留下半部:top=18px 以下 */
}
.textTop {
color: #ff0000;
position: absolute;
left: 3em; top: 1em;
font: 26px "Century Gothic", Arial, Helvetica, sans-serif;
clip: rect(0 auto 18px 0); /* 保留上半部:bottom=18px 以上 */
}
.container {
width: 28em; height: 5em;
margin: 1em auto;
position: relative; /* 為絕對定位子元素建立參考系 */
}
</style>
<div class="container">
<a href="#" class="textTop">Cascading Style Sheet</a>
<a href="#" class="textBottom">Cascading Style Sheet</a>
</div>
position:relative 容器 + position:absolute 文字層 = 精準重疊。clip: rect(top, right, bottom, left) 只保留矩形區域,其餘隱藏。/* CSS3 background-clip:text + gradient */
.double-color {
background: linear-gradient(red 50%, blue 50%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
單層標籤、可選取、無障礙友善,現代瀏覽器首選。