CSS文字镂空效果

一、 text-stroke属性

原理 :将文字颜色设为透明,给文字添加描边,只保留描边部分,形成镂空效果。

.text {
	color: transparent;
	-webkit-text-stroke: 2px #3498db; /* 描边宽度和颜色 */
	text-stroke: 2px #3498db;
}        

备注: 支持渐变描边:text-stroke: 2px linear-gradient(…)

进阶: 动态渐变背景(文字区域会透出背景渐变)
<div class="box">
	<h1class="title">夏日特惠</h1>
</div>
.box{
	background: linear-gradient(45deg, #ff9a9e, #fad0c4);
	padding: 3rem;
}

.title {
	font-size: 5rem;
	color: transparent;
	-webkit-text-stroke: 3px #fff;
	text-stroke: 3px #fff;
}

二、兼容方案:伪元素叠加法

原理:使用两层文字叠加,底层为描边文字,顶层为小一号的实心文字,通过错位形成镂空边缘。

.text {
	position: relative;
	font-size: 4rem;
	font-weight: bold;
	color: #2980b9; /* 描边色 */
}

/* 创建镂空层 */
.text ::after {
	content: attr(data-text); /* 复用元素文字 */
	position: absolute;
	top: 3px; /* 微调位置 */
	left: 3px;
	color: white; /* 背景色 */
	font-size: 0.92em; /* 小于原文字 */
}

优点:
1、兼容所有浏览器(包括IE9+)
2、可通过调整top/left值和字体大小控制镂空厚度
3、支持复杂背景(图片、视频背景均可)

三、高级方案:background-clip文本裁剪

原理:利用background-clip: text将背景裁剪到文字区域,结合text-fill-color实现镂空。

.text{
	color: transparent; /* 或使用text-fill-color */
	-webkit-text-fill-color: transparent;
	background: url(../images/back.jpg) center/cover; /* 任意背景 */
	-webkit-background-clip: text;
	background-clip: text;
}
扩展:动态视频背景
.video-box{
  position: relative; /* 作为视频和文字的定位容器 */
}
.video-box video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: -1; /* 视频置于文字下方 */
}
.video-box h1 {
  font-size: 6rem; /* 大字体增强效果 */
  background: white; 
  mix-blend-mode: screen; /* 关键:与背景视频混合 */
  -webkit-background-clip: text;
  background-clip: text; /* 背景仅显示在文字区域 */
  color: transparent; /* 文字本身透明 */
}

background-clip: text 让白色背景仅出现在文字形状内。
mix-blend-mode: screen 使文字区域的白色背景与下方视频混合,显示视频内容。
文字本身设置为透明,最终呈现 “视频透过文字显示” 的镂空效果。

CSS 中为文字添加镂空效果并加上白色描边,可以通过 `-webkit-text-stroke` 属性实现。这一属性允许开发者为文字添加描边,并且结合 `color: transparent` 可以将文字的填充颜色设置为透明,从而形成“镂空”的视觉效果。 以下是完整的实现方法: ### 1. HTML 结构 ```html <div class="hollow-text">镂空文字 + 白色描边</div> ``` ### 2. CSS 样式 ```css .hollow-text { font-size: 60px; font-weight: bold; text-align: center; -webkit-text-stroke: 2px white; /* 设置白色描边,宽度为2px */ color: transparent; /* 填充颜色为透明,实现镂空 */ background-color: #333; /* 背景颜色增强对比度 */ padding: 20px 0; } ``` ### 3. 效果说明 - **`-webkit-text-stroke: 2px white;`**:该属性为文字添加了宽度为 `2px` 的白色描边[^3]。 - **`color: transparent;`**:将文字本身的填充颜色设为透明,从而只显示描边部分,形成镂空效果[^1]。 - **`font-size` 和 `font-weight`**:用于控制字体大小和粗细,确保镂空文字清晰可见。 - **`background-color`**:为了突出白色描边,建议使用深色背景来增强对比度。 --- ### 示例代码整合 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>镂空文字 + 白色描边</title> <style> .hollow-text { font-size: 60px; font-weight: bold; text-align: center; -webkit-text-stroke: 2px white; color: transparent; background-color: #333; padding: 20px 0; } </style> </head> <body> <div class="hollow-text">镂空文字 + 白色描边</div> </body> </html> ``` --- ### 注意事项 - **浏览器兼容性**:`-webkit-text-stroke` 是一个非标准属性,主要在基于 WebKit 的浏览器(如 Chrome、Safari)中支持良好,在 Firefox 或 IE 中可能无法正常显示[^1]。 - **渐进增强**:如果需要更广泛的兼容性,可以考虑使用 SVG 来实现类似效果,或者使用图片替代。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值