Seams in shadows between adjacent divs

2019-06-01 03:28发布

Adjacent elements typically appear as if they were fused, with no lines or seams between them). Example: http://jsfiddle.net/4m3L79w4/1/

However, this does no longer appear to be the case if shadows are in use.

Html:

<div class="top"></div>
<div class="bottom"></div>

Css:

DIV {
    background-color: #7faf7f;
    width: 400px;
    height: 50px;
    position: relative;
    box-shadow: 0px 5px 20px 5px #000000;
}
.top {
    z-index: 2;
}
.bottom {
    z-index: 2;
}

The "bottom" element projects shadow over the "top" one, even though both have the same z-index.

JSFiddle: https://jsfiddle.net/b4fvb7e0/1/

Is it possible to make the shadow between the two elements disappear, so that the look fused?

This is actually a simplified version of a header I am trying to create, which in some pages should appear fused to the next element. JSFiddle with the header standalone and with the following element: http://jsfiddle.net/c8oat7vo/3/

1条回答
SAY GOODBYE
2楼-- · 2019-06-01 04:09

Set a pseudo element on top and bottom, and in this one set the shadow.

Also, set z-index of the pseudo elements to -1

fixed z-index issue thank to Jose Gomez

DIV {
    background-color: #7faf7f;
    width: 400px;
    height: 50px;
    position: relative;
}
.stack {
    background-color: #7f7faf;
    width: 80%;
    height: 60%;
    left: 10%;
    top: 20%;
    box-shadow: 0px 5px 20px 5px #000000;
    z-index: 1;
}
.top {
    width: 350px;
    margin-left: auto;
    margin-right: auto;
    z-index: auto;
    position: relative;
}
.bottom {
    width: 100%;
    z-index: auto;
}

.top:after, .bottom:after {
    content: "";
    position: absolute;
    top: 0px; left: 0px; right: 0px; bottom: 0px;
    box-shadow: 0px 5px 20px 5px #000000;
    z-index: -1;
}
<div class="top">
    <div class="stack"></div>
</div>
<div class="bottom"></div>

查看更多
登录 后发表回答