css - CSS background-clip:来自父元素背景的文本
问题描述
我试图让background-clip: text
一个子 div 从父 div 获取它的背景。基本上它应该看起来像文本被剪掉了。
当背景在同一个元素上时很容易做到,但我似乎无法弄清楚背景何时在不同的 div 上。
.background {
width: 100%;
height: 700px;
background: url('http://bgfons.com/uploads/gold/gold_texture464.jpg');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
}
.background > div {
flex: 1;
font-size: 5em;
font-weight: bold;
text-align: center;
}
.foreground1 {
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}
<div class="background">
<div class="foreground1">This is some text</div>
<div class="foreground2">This is some text</div>
</div>
我在 Plunkr 中有以下内容(文本应该从背景图像中获取颜色):
https://next.plnkr.co/edit/4lGVXnfvex9Q6mmY?open=lib%2Fscript.js
这可能吗?如果可以,怎么做?任何帮助表示赞赏。
解决方案
您必须应用background-clip
到要剪辑的背景:
.background {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
background: url('http://bgfons.com/uploads/gold/gold_texture464.jpg');
background-size: cover;
background-clip: text;
-webkit-background-clip: text;
}
.background>div {
flex: 1;
font-size: 3em;
font-weight: bold;
text-align: center;
}
.foreground1 {
color: transparent;
}
<div class="background">
<div class="foreground1">This is some text</div>
<div class="foreground2">This is some text</div>
</div>
推荐阅读
- scikit-learn - sklearn TtfidfVectorizer 停用词_
- python - matplotlib 相对于特定绘图的自动缩放
- java - javapackager 无法在 macOS 上生成 dmg
- css - Opera 插件,更改 url 文本颜色
- django - 在 Django 中聚合分组字段时返回对象
- java - Maven Failsafe 因 java.lang.NoClassDefFoundError 而失败
- javascript - 动态表单操作 URL
- python - 为什么使用 importlib 而不是 exec import
- firebase - 使用 auth.uid 的基于查询的规则不起作用
- javascript - 使传单地图顶部的 SVG 不响应平移事件