gpt4 book ai didi

CSS clip-path 打破了伪::在元素堆栈顺序之前

转载 作者:行者123 更新时间:2023-11-28 14:37:26 25 4
gpt4 key购买 nike

我正在尝试在非矩形元素上创建磨砂玻璃效果,但没有成功。我遇到了一个奇怪的问题,我似乎无法解决问题......

磨砂玻璃效果很容易实现,方法是在文档主体上设置固定的背景图像,为元素添加部分透明的背景色,并创建一个具有相同固定背景图像的::before伪元素,并应用模糊滤镜。像这样:

  body {
background: url(bg-lanterns.jpg) 0 / cover fixed;
}

main {
position: relative;
margin: 1rem auto;
padding: 1rem;
height: 600px;
width: 800px;
background: rgba(255,255,255,0.7);
}

main::before {
content: '';
z-index: -1;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url(bg-lanterns.jpg) 0 / cover fixed;
filter: blur(10px);
}

frosted glass effect

使用 clip-path 创建非矩形元素也很容易像这样:

  main {
position: relative;
margin: 1rem auto;
padding: 1rem;
height: 600px;
width: 800px;
background: rgba(255,255,255,0.7);
clip-path: polygon(25% 0%, 100% 0%, 75% 100%, 0% 100%);
}

non-rectangular element

但是尝试组合这两种效果会破坏堆叠顺序并导致::before 元素出现在白色背景之上。

broken

我在 Chrome 和 Firefox 中得到了相同的结果,所以我想知道这是否是预期的行为,而我只是做错了什么......任何人都可以阐明这里发生的事情吗?

这是一个现场演示:

      body {
background: url(https://i.imgur.com/y1TH8fR.jpg) 0 / cover fixed;
}

main {
position: relative;
margin: 1rem auto;
padding: 1rem;
height: 600px;
width: 800px;
background: rgba(255,255,255,0.7);
clip-path: polygon(25% 0%, 100% 0%, 75% 100%, 0% 100%);
}

main::before {
content: '';
z-index: -1;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 1rem;
background: url(https://i.imgur.com/y1TH8fR.jpg) 0 / cover fixed;
filter: blur(10px);
}
<main></main>

最佳答案

根据clip-path的规范:

A computed value of other than none results in the creation of a stacking context [CSS21] the same way that CSS opacity [CSS3COLOR] does for values other than 1.

我设法通过将白色添加到::after 伪元素并剪裁两个伪元素而不是元素本身来达到预期效果。

      body {
background: url(https://i.imgur.com/y1TH8fR.jpg) 0 / cover fixed;
}

main {
position: relative;
margin: 1rem auto;
height: 600px;
width: 800px;
display: flex;
flex-flow: column nowrap;
align-content: center;
align-items: center;
justify-content: center;
}

main::before,
main::after {
content: '';
z-index: -1;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
clip-path: polygon(25% 0%, 100% 0%, 75% 100%, 0% 100%);
}

main::before {
background: url(https://i.imgur.com/y1TH8fR.jpg) 0 / cover fixed;
filter: blur(10px);
}

main::after {
background: rgba(255,255,255,0.7);
}
<main> <span> test </span> </main>

关于CSS clip-path 打破了伪::在元素堆栈顺序之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53306479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com