CSS   发布时间:2022-04-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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.

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

而负面的传播

(两者都是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;
}

大佬总结

以上是大佬教程为你收集整理的css – 如何使用z-index对盒子阴影进行分层?全部内容,希望文章能够帮你解决css – 如何使用z-index对盒子阴影进行分层?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。