要实现字体镂空描边效果,主要有以下几种CSS方法:
text-stroke
属性.text-outline {
color: transparent;
-webkit-text-stroke: 2px #000; /* 适用于WebKit浏览器 */
text-stroke: 2px #000; /* 标准属性 */
}
text-shadow
模拟描边.text-shadow-outline {
color: transparent;
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
}
.text-pseudo-outline {
position: relative;
color: transparent;
}
.text-pseudo-outline::before {
content: attr(data-text);
position: absolute;
-webkit-text-stroke: 2px #000;
text-stroke: 2px #000;
}
<svg width="100%" height="100">
<text x="50%" y="50%"
text-anchor="middle"
dominant-baseline="middle"
stroke="#000"
stroke-width="2"
fill="transparent"
font-size="48">
镂空文字
</text>
</svg>
text-stroke
属性在非WebKit浏览器中支持度有限text-shadow
方法会影响性能,特别是对大量文本选择哪种方法取决于你的项目需求和浏览器兼容性要求。