CSS能否实现在文字上重叠一个透明背景的图片,这个图片除去文字区域外隐藏?

不好意思哈,没说明白,不是想说让文字有渐变效果,一张颜色很复杂的透明背景图片,投影到文字上..

问题如题:
大概意思就是我在一个标签内有几个很大的文字,我想在这几个很大的文字上面盖一层背景透明的渐变色图片,我又不想这个这个图片在文字以外的区域显示。

现在的代码:

结构:

<p>测试文字<span></span></p>

样式:

p {
  position: relative;
  font-size: 130px;
  font-family: '华文行楷';
  color: '#290b0c';
}

span {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: 300px;
  height: 140px;
  background: red;
  opacity: 0.4;
}

现在的效果:

图片描述

我想要实现的效果:

图片描述

除了文字上面的红色以外,其他地方的红色全都隐藏。

最后:
如果真的没法就算了,有办法的话,麻烦大家了。

阅读 8.1k
6 个回答

试试这个能不能稍微靠近你那个需求,希望能对你有所帮助:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>tab</title>
    <style>
        .text-gradient {
            display: inline-block;
            color: green;
            font-size: 10em;
            font-family: "微软雅黑";
            background-image: -webkit-gradient(linear, 0 0, 0 bottom, from(rgba(0, 128, 0, 1)), to(rgba(51, 51, 51, 1)));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        };
    </style>
</head>
<body>
    <h2 class="text-gradient">文字渐变颜色</h2>
</body>
</html>

效果

clipboard.png

图片描述

<div class="text">Welcome!</div>
.text{ 
    font-size: 120px;
    background: url('https://ss1.bdstatic.com/lvoZeXSm1A5BphGlnYG/skin/836.jpg?2') no-repeat center center / cover;
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
}

好像没法实现吧。
你可以直接把字做成 iconfont 或者 svg 呀。
比如:

<svg height="30" width="200">
  <text x="0" y="15" fill="red">在平坦的路上曲折前行</text>
</svg>

效果:

文字转个曲,然后SVG输出,再写到clip-path里应该可以吧。

https://codepen.io/WhiteYin/p...
背景用图片,设置

-webkit-background-clip: text;
-webkit-text-fill-color: transparent;

我觉得luozz的回答应该满足你的要求了吧……

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题