首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将阴影和z索引应用于绝对定位元素

将阴影和z索引应用于绝对定位元素
EN

Stack Overflow用户
提问于 2017-05-15 10:30:42
回答 4查看 842关注 0票数 0

我试图在导航栏中实现一个凸起,导航条应该有一个方框-阴影.然而,在底部也需要它.不知何故,我不能把撞在导航栏后面,这是一个已知的问题,还是我遗漏了什么?非常感谢。

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-05-15 11:48:40

不好意思,我刚刚意识到了最简单的实现方法,那就是在这个伪元素的上面加上另一个伪元素,这样就可以消除(或掩盖)凸起的阴影。

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2017-05-15 10:39:51

我不知道这是否是预期的结果,但我成功地通过将

代码语言:javascript
复制
  <div class="c-logo">

在标头元素的外部。

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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">

票数 2
EN

Stack Overflow用户

发布于 2017-05-15 10:44:19

您已经为box-shadow circle指定了伪选择器:after上的blur-radius,您可以将该特定元素上的blur-radius缩减如下,

方框-阴影:偏移-x,偏移-y,模糊-半径-颜色

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43977193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档