You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CSS实现部分条纹字母效果?该效果是否可行?

CSS实现部分条纹字母效果的可行方案

当然可以用纯CSS实现这种部分条纹的字母效果,无需依赖矢量编辑工具导出SVG,以下是几种实用方案:

方法一:背景裁剪+伪元素遮罩

利用background-clip: text让文字显示背景,再通过伪元素复制文字并裁剪指定区域,实现部分条纹+部分纯色的效果:

.stripe-text {
  font-size: 8rem;
  font-weight: bold;
  /* 底层文字显示斜条纹背景 */
  background: repeating-linear-gradient(45deg, #222, #222 10px, #fff 10px, #fff 20px);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  position: relative;
}

.stripe-text::after {
  content: attr(data-text);
  position: absolute;
  top: 0;
  left: 0;
  /* 上层伪元素显示纯色,覆盖指定区域 */
  background: #222;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  /* 裁剪出需要覆盖的区域,这里是上半部分 */
  clip-path: inset(0 0 50% 0);
}

使用时给元素加上data-text属性:

<h1 class="stripe-text" data-text="HELLO">HELLO</h1>

调整clip-path的inset参数可以改变条纹显示的区域,比如inset(20% 0 0 0)会让字母上20%显示纯色,剩余部分显示条纹。

方法二:CSS遮罩(mask)实现

通过伪元素叠加条纹背景,再用渐变遮罩控制条纹的显示范围:

.stripe-text-mask {
  font-size: 8rem;
  font-weight: bold;
  color: #222;
  position: relative;
}

.stripe-text-mask::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* 白色条纹背景(和文字颜色形成对比) */
  background: repeating-linear-gradient(45deg, #fff, #fff 10px, transparent 10px, transparent 20px);
  /* 遮罩:仅让下半部分显示条纹 */
  mask-image: linear-gradient(to bottom, transparent 50%, #000 50%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 50%, #000 50%);
  mask-mode: alpha;
  -webkit-mask-mode: alpha;
}

这种方法的优势是无需复制文字,直接通过遮罩控制条纹的可见区域。

方法三:多重背景定位划分区域

针对规则的上下/左右分块场景,直接用多重背景分别设置条纹和纯色,并定位到对应区域:

.stripe-text-bg {
  font-size: 8rem;
  font-weight: bold;
  background: 
    /* 纯色背景,覆盖上半部分 */
    linear-gradient(#222, #222) 0 0 / 100% 50% no-repeat,
    /* 条纹背景,显示下半部分 */
    repeating-linear-gradient(45deg, #222, #222 10px, #fff 10px, #fff 20px) 0 100% / 100% 50% no-repeat;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

这种写法更简洁,适合不需要复杂裁剪的对称区域效果。

如果需要不规则的条纹区域,可以自定义clip-path的路径(比如用多边形路径polygon()),或者使用SVG格式的遮罩代码(纯手写SVG,无需导出)来实现更精细的控制。

内容的提问来源于stack exchange,提问作者bluebit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 05:22:29