【发布时间】:2020-06-23 01:57:39
【问题描述】:
我有一个基于这个 codepen 的简单测试:https://codepen.io/davidlillo/pen/wZRagx
@import url('https://fonts.googleapis.com/css?family=Pirata+One|Rubik:900');
body {
justify-content: center;
align-items: center;
min-height: 25vh;
background-color: #141E30;
background: linear-gradient(to right, #24243e, #141E30, #0f0c29);
}
#backinblack h1 {
text-transform: Uppercase;
margin-bottom: .5em;
font-family: 'Rubik', sans-serif;
font-size: 6rem;
color: #E4E5E6;
}
#backinblack h1 {
position: relative;
background: linear-gradient(to right, #24243e, #141E30, #0f0c29);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
#backinblack h1:before,
#backinblack h1:after {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
}
#backinblack h1:before {
z-index: -1;
text-shadow: -0.001em -0.001em 1px rgba(255, 255, 255, .15)
}
#backinblack h1:after {
z-index: -2;
text-shadow: 10px 10px 10px rgba(0, 0, 0, .5), 20px 20px 20px rgba(0, 0, 0, .4), 30px 30px 30px rgba(0, 0, 0, .1);
mix-blend-mode: multiply;
}
<div id="backinblack">
<h1 data-text="test">test</h1>
</div>
当 CSS 像这样时,文本阴影可以正常工作:
body {
justify-content: center;
align-items: center;
min-height: 25vh;
background-color: #141E30;
background: linear-gradient(to right, #24243e, #141E30, #0f0c29);
}
但是,如果我将其更改为具有这样的 CSS:
#backinblack {
justify-content: center;
align-items: center;
min-height: 25vh;
background-color: #141E30;
background: linear-gradient(to right, #24243e, #141E30, #0f0c29);
}
然后像这样保留 HTML:
<div id="backinblack">
<h1 data-text="test">test</h1>
</div>
那么文字阴影就不会出现了:
@import url('https://fonts.googleapis.com/css?family=Pirata+One|Rubik:900');
#backinblack {
justify-content: center;
align-items: center;
min-height: 25vh;
background-color: #141E30;
background: linear-gradient(to right, #24243e, #141E30, #0f0c29);
}
#backinblack h1 {
text-transform: Uppercase;
margin-bottom: .5em;
font-family: 'Rubik', sans-serif;
font-size: 6rem;
color: #E4E5E6;
}
#backinblack h1 {
position: relative;
background: linear-gradient(to right, #24243e, #141E30, #0f0c29);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
#backinblack h1:before,
#backinblack h1:after {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
}
#backinblack h1:before {
z-index: -1;
text-shadow: -0.001em -0.001em 1px rgba(255, 255, 255, .15)
}
#backinblack h1:after {
z-index: -2;
text-shadow: 10px 10px 10px rgba(0, 0, 0, .5), 20px 20px 20px rgba(0, 0, 0, .4), 30px 30px 30px rgba(0, 0, 0, .1);
mix-blend-mode: multiply;
}
<div id="backinblack">
<h1 data-text="test">test</h1>
</div>
我想知道为什么会这样?
【问题讨论】:
-
你所需要的只是 h1 元素上的 z-index:0