要使用 HTML 和 CSS 实现“文字镂空效果”(即文字区域透明,背景可见,文字外是有颜色的),常用的方法是借助 background-clip: text
或 mix-blend-mode
,但 真正镂空 需要配合 mask
或 filter
,也可以用 SVG
来实现。
✅ 方法一:使用 background-clip
(模拟镂空视觉,背景填充文字)
虽然这不是真正的“镂空”,但视觉效果很像:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style>
.hollow-text {
font-size: 80px;
font-weight: bold;
background: url('https://picsum.photos/id/1015/800/400') no-repeat center;
background-size: cover;
-webkit-background-clip: text;
color: transparent;
}
body {
background-color: #000;
}
</style>
</head>
<body>
<div class="hollow-text">HELLO</div>
</body>
</html>
说明:这是一种文字背景填充的技术,不是真正“挖空”。
✅ 方法二:真正的“文字镂空”效果(mask 遮罩)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style>
.hollow-container {
position: relative;
width: 100vw;
height: 100vh;
background: url('https://picsum.photos/id/1005/800/600') center/cover no-repeat;
}
.hollow-text {
font-size: 100px;
font-weight: bold;
color: black;
text-align: center;
line-height: 100vh;
mix-blend-mode: destination-out;
position: absolute;
width: 100%;
top: 0;
}
</style>
</head>
<body>
<div class="hollow-container">
<div class="hollow-text">HELLO</div>
</div>
</body>
</html>
说明:通过
mix-blend-mode: destination-out
,文字真正“挖掉”背景内容,形成镂空。
✅ 方法三:SVG 实现真正文字镂空(兼容性更好)
<svg width="800" height="300">
<defs>
<mask id="text-mask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="white"/>
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle"
font-size="80" font-family="Arial" fill="black">
HELLO
</text>
</mask>
</defs>
<image href="https://picsum.photos/id/1016/800/300" width="800" height="300" mask="url(#text-mask)" />
</svg>
效果:文字区域被挖掉,露出背景内容,适用于高级视觉展示。
✅ 总结
方法 | 是否真正镂空 | 浏览器支持 | 说明 |
---|---|---|---|
background-clip: text | 否(填充文字) | 所有现代浏览器 | 简单好用 |
mix-blend-mode: destination-out | 是 | 较好 | 真镂空但复杂,需注意层级 |
SVG mask | 是 | 非常好 | 精准控制,推荐用于复杂设计 |
如果你想要用于某种具体的场景(如按钮、海报、LOGO文字),我可以给你量身定做一个代码例子。需要吗?
发表回复