将shadow和z-index应用于绝对定位元素

时间:2017-05-15 10:30:42

标签: html css css3 sass

我试图在导航栏中实现一个凹凸,导航栏应该有一个盒子阴影...但是凹凸也需要它在底部......不知怎的,我不能把凹凸放在后面导航栏,是一个已知的问题还是我错过了什么?非常感谢。



body {
  background: beige;
}

.c-header {
  background: white;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.8);
  height: 60px;
  position: fixed;
  width: 100%;
  z-index: 9999;
}

.c-site-nav {
  align-content: center;
  display: flex;
  justify-content: space-between;
}
.c-site-nav ul {
  margin-left: 0;
}
.c-site-nav__item {
  display: flex;
  justify-content: space-around;
  list-style: none;
  width: 40%;
}
.c-site-nav__item a {
  font-size: 20px;
  color: grey;
  display: inline-block;
  text-decoration: none;
}

.c-logo {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  padding: 0;
  position: absolute;
  top: 15px;
  width: 45px;
  z-index: 1;
}
.c-logo::after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.8);
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  background: white;
  border-radius: 50%;
  content: '';
  height: 60px;
  position: absolute;
  width: 60px;
  z-index: -100;
}

<header class="c-header">
  <div class="c-logo">
  </div>
  <nav class="c-site-nav">
    <ul class="c-site-nav__item">
        <li><a href="/">asd</a></li>
        <li><a href="/ueber_uns">asd</a></li>
        <li><a href="/">asd</a></li>
    </ul>
    <ul class="c-site-nav__item">
        <li><a href="/">asd</a></li>
        <li><a href="/">asd</a></li>
        <li><a href="tel:+49234234234">34234234</a></li>
    </ul>
  </nav>
</header>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

我不知道这是否是预期的结果,但我设法通过把

完成了
  <div class="c-logo">

在标题元素之外。

&#13;
&#13;
body {
  background: beige;
}

.c-header {
  background: white;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.8);
  height: 60px;
  position: fixed;
  width: 100%;
  z-index: 9999;
}

.c-site-nav {
  align-content: center;
  display: flex;
  justify-content: space-between;
}
.c-site-nav ul {
  margin-left: 0;
}
.c-site-nav__item {
  display: flex;
  justify-content: space-around;
  list-style: none;
  width: 40%;
}
.c-site-nav__item a {
  font-size: 20px;
  color: grey;
  display: inline-block;
  text-decoration: none;
}

.c-logo {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  padding: 0;
  position: absolute;
  top: 15px;
  width: 45px;
  z-index: 1;
}
.c-logo::after {
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.8);
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  background: white;
  border-radius: 50%;
  content: '';
  height: 60px;
  position: absolute;
  width: 60px;
  z-index: -100;
}
&#13;
<header class="c-header">
  </div>
  <nav class="c-site-nav">
    <ul class="c-site-nav__item">
        <li><a href="/">asd</a></li>
        <li><a href="/ueber_uns">asd</a></li>
        <li><a href="/">asd</a></li>
    </ul>
    <ul class="c-site-nav__item">
        <li><a href="/">asd</a></li>
        <li><a href="/">asd</a></li>
        <li><a href="tel:+49234234234">34234234</a></li>
    </ul>
  </nav>
</header>
  <div class="c-logo">
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您已为中心box-shadow的伪选择器:after分配了circle,您可以减少该特定元素的blur-radius,如下所示,

  

box-shadow:offset-x | offset-y | blur-radius |颜色

&#13;
&#13;
body {
  background: beige;
}

.c-header {
  background: white;
  box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.8);
  height: 60px;
  position: fixed;
  width: 100%;
  z-index: 9999;
}

.c-site-nav {
  align-content: center;
  display: flex;
  justify-content: space-between;
}
.c-site-nav ul {
  margin-left: 0;
}
.c-site-nav__item {
  display: flex;
  justify-content: space-around;
  list-style: none;
  width: 40%;
}
.c-site-nav__item a {
  font-size: 20px;
  color: grey;
  display: inline-block;
  text-decoration: none;
}

.c-logo {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  padding: 0;
  position: absolute;
  top: 15px;
  width: 45px;
  z-index: 1;
}
.c-logo::after {
  box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.8);
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  background: white;
  border-radius: 50%;
  content: '';
  height: 60px;
  position: absolute;
  width: 60px;
  z-index: -100;
}
&#13;
<header class="c-header">
  <div class="c-logo">
  </div>
  <nav class="c-site-nav">
    <ul class="c-site-nav__item">
        <li><a href="/">asd</a></li>
        <li><a href="/ueber_uns">asd</a></li>
        <li><a href="/">asd</a></li>
    </ul>
    <ul class="c-site-nav__item">
        <li><a href="/">asd</a></li>
        <li><a href="/">asd</a></li>
        <li><a href="tel:+49234234234">34234234</a></li>
    </ul>
  </nav>
</header>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

更改'C-logo'的样式。只将Z-index从1改为-1希望这对你有帮助

`.c-logo {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  padding: 0;
  position: absolute;
  top: 15px;
  width: 45px;
  z-index: -1;
}`

答案 3 :(得分:0)

很抱歉这个混乱,我刚刚意识到实现它的最简单方法,就是在伪顶部放置另一个伪元素,创建凹凸以消除(或掩盖)凹凸的阴影

&#13;
&#13;
body {
  background: beige;
  margin: 0;
}

.c-header {
  box-shadow: 0 1.5px 4px rgba(0, 0, 0, 0.24), 0 1.5px 6px rgba(0, 0, 0, 0.12);
  background: white;
  height: 45px;
  width: 100%;
  z-index: 9999;
}

.c-site-nav {
  align-content: center;
  display: flex;
  justify-content: space-between;
}
.c-site-nav ul {
  margin-left: 0;
}
.c-site-nav__item {
  display: flex;
  justify-content: space-around;
  list-style: none;
  width: 40%;
}
.c-site-nav__item a {
  font-size: 20px;
  color: grey;
  display: inline-block;
  text-decoration: none;
}

.c-logo {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  padding: 0;
  position: absolute;
  top: 10px;
  width: 45px;
  z-index: 1;
}
.c-logo::after {
  box-shadow: 0 1.5px 4px rgba(0, 0, 0, 0.24), 0 1.5px 6px rgba(0, 0, 0, 0.12);
  background: white;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  content: '';
  height: 45px;
  position: absolute;
  width: 60px;
  z-index: -1;
}

.c-logo::before {
  background: white;
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  content: '';
  top: -10px;
  height: 45px;
  position: absolute;
  width: 80px;
  z-index: 1;
}
&#13;
<header class="c-header">
  <div class="c-logo">
</div>
  <nav class="c-site-nav">
    <ul class="c-site-nav__item">
      <li><a href="/">item</a></li>
      <li><a href="/">item</a></li>
      <li><a href="/">item</a></li>
    </ul>
    <ul class="c-site-nav__item">
      <li><a href="/">item</a></li>
      <li><a href="/">item</a></li>
      <li><a href="/">item</a></li>
    </ul>
  </nav>
</header>
&#13;
&#13;
&#13;