css – 如何使用z-index对盒子阴影进行分层?

前端之家收集整理的这篇文章主要介绍了css – 如何使用z-index对盒子阴影进行分层?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
codepen

HTML

<div id="a">
  <div id="b">bbb</div>
  <div id="c">
    <ul>
      <li>a</li>
      <li class="current">b</li>
      <li>c</li>
      <li>d</li>
    </ul>
  </div>
</div>
<div id="d">dddd
</div>

CSS

#b {
  background: orange;
  Box-shadow: 0 0 10px black;
  z-index: 2;
  position: relative;
}

#c {
  background: red;
  z-index: 1;
  position: relative;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  z-index: 1;
  position: relative;
}
li {
  display: inline-block;
  padding: 2px 5px;
}

.current {
  background-color: orange;
  z-index: 3;
  position: relative;
}

#d {
  Box-shadow: 0 0 10px black;
  z-index: 2;
}

看看代码笔.我的层数或多或少都是我想要的,除了我希望“b”标签位于由上面的橙色div引起的盒子阴影之上.

要详细说明,橙色#b div应该在红色#c div上投射阴影,.current选项卡应该与橙色#b div齐平(没有阴影),#d不应该在#上投射阴影# c.

问题是.current上的z-index似乎不起作用.

解决方法

演示: http://codepen.io/anon/pen/vLgKC

有关z-index和堆栈上下文以及优先级的更多信息,请参阅我的答案here.

以上,结合了盒子阴影的插图

inset

If not specified (default),the shadow is assumed to be a drop shadow (as if the Box were raised above the content).
The presence of the inset keyword changes the shadow to one inside the frame (as if the content was depressed inside the Box).
Inset shadows are drawn inside the border (even transparent ones),above the
background,but below content.

而负面的传播

spread-radius

This is a fourth value. Positive values will cause the shadow to expand and grow bigger,negative values will cause the
shadow to shrink. If not specified,it will be 0 (the shadow will be the same size as the element).

(两者都是here)

会给你你想要的效果.

因此,您需要更改阴影应用于元素的位置.

所以最终的CSS:

#b {
  background: orange;
  z-index: 2;
  position: relative;
}

#c {
  background: red;
  -webkit-Box-shadow: inset 0 10px 10px -10px black;
  -moz-Box-shadow: inset 0 10px 10px -10px black;
  Box-shadow: inset 0 10px 10px -10px black;
  z-index: 1;
  position: relative;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  z-index: 1;
  position: relative;
}
li {
  display: inline-block;
  padding: 2px 5px;
}

.current {
  background-color: orange;
  z-index: 3;
  position: relative;
}

#d {
  Box-shadow: 0 0 10px black;
  z-index: 2;
}
原文链接:https://www.f2er.com/css/217426.html

猜你在找的CSS相关文章