如何让 box-shadow 出现在 div 上方?

新手上路,请多包涵

是我要完成的示例。

上层的 --- box-shadow div 不会出现在它下面的 div 。据我了解,我需要设置 z-index 所以它会出现在顶部并且只适用于 position: relative; 的元素,但它仍然没有出现。

我究竟做错了什么?

 #top {
    width: 100%;
    height: 100px;
    box-shadow: 3px 3px 3px #333;
    background-color: blue;
}

#middle {
    width: 100%;
    height: 200px;
    z-index: 0;
    position: relative;
    background-color: orange;
}

#innerMiddle {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    background-color: green;
}
 <div id="top">
</div>

<div id="middle">
    <div id="innerMiddle">
    </div>
</div>

原文由 Vecta 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

It’s #top and its box-shadow that you want to appear on top, so give that position: relative instead of giving position: relative to #middle 。不需要 z-index: 0

http://jsfiddle.net/thirtydot/QqME6/1/

 #top {
    width: 100%;
    height: 100px;
    box-shadow: 3px 3px 3px #333;
    background-color: blue;

    position: relative;
}

#middle {
    width: 100%;
    height: 200px;
    background-color: orange;
}

#innerMiddle {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    background-color: green;
}
 <div id="top">
</div>

<div id="middle">
    <div id="innerMiddle">
    </div>
</div>

原文由 thirtydot 发布,翻译遵循 CC BY-SA 4.0 许可协议

将您的 CSS 更改为:

 #top {
    width: 100%;
    height: 100px;
    box-shadow: 3px 3px 3px #333;
    background-color: blue;
    position:relative;
    z-index:1;
}

#middle {
    width: 100%;
    height: 200px;
    z-index: 0;
    position: relative;
    background-color: orange;
}
 <div id="top">
</div>

<div id="middle">
    <div id="innerMiddle">
    </div>
</div>

原文由 Sagar Patil 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题