[英]position:relative breaking background-clip: text
I'm using background-clip: text
on an element with some child <p>
elements.我在带有一些子
<p>
元素的元素上使用background-clip: text
。 I wanted to add some :after
elements to the <p>
tags and position then absolutely in order to cover up the text and do some animations, but when I set position: relative
on the <p>
tags, they disappear.我想添加一些
:after
元素到<p>
标签和位置然后绝对是为了覆盖文本并做一些动画,但是当我在<p>
标签上设置position: relative
时,它们消失了。 I'm guessing it has something to do with the combination of background-clip
and color: transparent
.我猜这与
background-clip
和color: transparent
的组合有关。 It's like it removes the ability to use background-clip
.这就像它删除了使用
background-clip
的能力。 Is there a way to get this working?有没有办法让这个工作?
* { margin: 0; padding: 0; box-sizing: border-box; } html,body { display: flex; width: 100%; height: 100%; justify-content: center; align-items: center; } body { flex-direction: column; } .container { background: linear-gradient(to bottom, white, black); background-clip: text; -webkit-background-clip: text; text-align: center; } .container p { font-size: 5em; line-height: 1; font-weight: 900; text-transform: uppercase; color: transparent; -webkit-text-fill-color: transparent; } .container.relative p { position: relative; } /* .container.relative p:after { content: ""; width: 100%; height: 100%; position: absolute; top: 0; left: 0; background: black; } */
<p>The text below is relative, and appears invisible</p> <div class="container relative"> <p>Some Relative</p> <p>Text</p> </div> <p>The text below is <strong>not</strong> relative and is visible</p> <div class="container"> <p>Some</p> <p>Text</p> </div>
https://jsfiddle.net/Rissk13/pa1v6gdr/1/ https://jsfiddle.net/Rissk13/pa1v6gdr/1/
Here is an idea with more gradient where you don't need position:relative
.这是一个带有更多渐变的想法,您不需要
position:relative
。
Hover to see the effect:悬停查看效果:
* { margin: 0; padding: 0; box-sizing: border-box; } html,body { display: flex; width: 100%; height: 100%; justify-content: center; align-items: center; } body { flex-direction: column; } .container { background: linear-gradient(to bottom, white, black); background-clip: text; -webkit-background-clip: text; text-align: center; } .container p { font-size: 5em; line-height: 1; font-weight: 900; text-transform: uppercase; color: transparent; -webkit-text-fill-color: transparent; } .container p { background:linear-gradient(#000,#000); background-repeat:no-repeat; background-size:0% 100%; background-position:left; transition:1s; } .container p:hover { background-size:100% 100%; }
<p>The text below is <strong>not</strong> relative and is visible</p> <div class="container"> <p>Some</p> <p>Text</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.