gpt4 book ai didi

html - CSS:类似于将元素的框阴影放置在与元素不同的 z-index 处的效果

转载 作者:行者123 更新时间:2023-11-28 03:51:50 27 4
gpt4 key购买 nike

我有一个包含图像的容器和一个面板,当您将鼠标悬停在该图像上时会出现该面板。我试图让面板上的框阴影出现在图像后面,而面板的其余部分与图像重叠。

What I have vs. What I'd like to have

HTML:

<div class="container">
<img class="icon" src="http://placehold.it/350x350" />
<div class="sum-container left">
<img src="http://placehold.it/350x150" />
</div>
</div>

CSS:

.container .sum-container {
position: absolute;
top: 0;
bottom: 0;
border: solid 5px blue;
background-color: white;
width: 250px;
height: 200px;
max-height: 100%;
opacity: 0;
overflow: hidden;
z-index: 5;
pointer-events: none;
transition-property: opacity;
transition-duration: .250s;
}

.container .sum-container.left {
right: 100%;
margin-right: -5px;
border-right: none;
padding-right: 0px;
box-shadow: 5px 5px 0px #888888;
}

.container .icon:hover + .sum-container {
z-index: 6;
opacity: 1;
}

.container {
position: absolute;
left: 300px;
top: 20px;
}

.container img {
width: 100%;
}

.icon {
margin-left: auto;
margin-right: auto;
display: block;
width: 100%;
height: auto;
max-width: 480px;
background-color: blue;
box-shadow: 5px 5px 0px #888888;
text-align: center;
font-size: 26px;
color: white;
text-decoration: none;
padding: 5px;
cursor: pointer;
border: none;
outline: none;
user-drag: none;
}

我添加了一个 JSFiddle

此外,这里还是新的。如果有人可以建议更好的标题,请告诉我。我知道您实际上不能为一个元素设置多个 z-index,但我正在寻找具有类似效果的解决方案。

最佳答案

如果我理解最终目标,您可以使阴影成为具有负 z-index 的伪元素,并从 .sum 中删除 z-index -container.sum-container 将在 .icon 之上,它的伪元素将在它们之下。

.container .sum-container {
position: absolute;
top: 0;
bottom: 0;
border: solid 5px blue;
background-color: white;
width: 250px;
height: 200px;
max-height: 100%;
opacity: 1;
pointer-events: none;
transition-property: opacity;
transition-duration: .250s;
}

.sum-container:after {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
content: '';
background: #888;
transform: translate(0,10px);
z-index: -1;
}

.container .sum-container.left {
right: 100%;
margin-right: -5px;
border-right: none;
padding-right: 0px;
}

.container .icon:hover + .sum-container {
opacity: 1;
}

.container {
position: absolute;
left: 300px;
top: 20px;
}

.container img {
width: 100%;
}

.icon {
margin-left: auto;
margin-right: auto;
display: block;
width: 100%;
height: auto;
max-width: 480px;
background-color: blue;
box-shadow: 5px 5px 0px #888888;
text-align: center;
font-size: 26px;
color: white;
text-decoration: none;
padding: 5px;
cursor: pointer;
border: none;
outline: none;
user-drag: none;
}
<div class="container">
<img class="icon" src="http://placehold.it/350x350" />
<div class="sum-container left">
<img src="http://placehold.it/350x150" />
</div>
</div>

关于html - CSS:类似于将元素的框阴影放置在与元素不同的 z-index 处的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689074/

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