首页 > 解决方案 > 设置伪元素的 z-index

问题描述

目前我正在研究导航栏菜单。我希望能够在将CSS borders鼠标悬停在我的nav项目上时使用创建的箭头。目前这是可行的,但是创建的箭头在:after越过nav border. 即使设置如下所示的 z-index 样式,它仍然是隐藏的。

我只能输入一小段代码,否则它会太多,但希望你能明白我的意思。

#navbar {
  overflow: hidden;
  background-color: white;
  font-size: 11px;
  z-index: 2;
  border: 1px solid grey;
  height: 50px;
}

.navbar-links {
  margin-left:10em;
  height: 100%;
  padding: 0em 4em 0em 1em;
  min-width:348px;
  margin-top:auto;
  margin-bottom:auto;
}

#navbar .navbar-links a {
  display:block;
  float: left;
  color: grey;
  padding: 14px 20px 14px 20px;
  text-decoration: none;
  font-size: 14px;
  margin-top: auto;
  margin-bottom: auto;
  position:relative;
}

#navbar .navbar-links a:hover::after {
  left: calc(50% - 10px);
  content: '';
  position: absolute;
  bottom: -12px;
  width: 0;
  height: 0;
  text-align: center;
  border-width: 10px 10px 0;
  border-style: solid;
  border-color: grey transparent;
  margin: auto;
  display: block;
  left:0;
  right: 0;
  z-index:10;
}
  <div id="navbar">
        <div class="navbar-links">
          <a [routerLinkActive]="['navbar-links-active']" [routerLink]="['link1']" (mouseover)="displayInsightDropDown();" (mouseleave)="hideInsightDropDown();">LINK 1</a>
          <a [routerLinkActive]="['navbar-links-active']" [routerLink]="['link2']">LINK 2</a> 
          <a [routerLinkActive]="['navbar-links-active']" [routerLink]="['link3']">LINK 3</a> 
        </div>
  </div>

我只想能够在导航框之外显示这些箭头。任何建议,将不胜感激。

标签: angularhtmlcss

解决方案


它是隐藏的,因为它是#navbar您声明应该隐藏溢出的子元素。

您只需设置overflow: visible;您的#navbar.

#navbar {
  overflow: visible; /* ← This does the trick */
  background-color: white;
  font-size: 11px;
  z-index: 2;
  border: 1px solid grey;
  height: 50px;
}

.navbar-links {
  margin-left:10em;
  height: 100%;
  padding: 0em 4em 0em 1em;
  min-width:348px;
  margin-top:auto;
  margin-bottom:auto;
}

#navbar .navbar-links a {
  display:block;
  float: left;
  color: grey;
  padding: 14px 20px 14px 20px;
  text-decoration: none;
  font-size: 14px;
  margin-top: auto;
  margin-bottom: auto;
  position:relative;
}

#navbar .navbar-links a:hover::after {
  left: calc(50% - 10px);
  content: '';
  position: absolute;
  bottom: -16px;
  width: 0;
  height: 0;
  text-align: center;
  border-width: 10px 10px 0;
  border-style: solid;
  border-color: grey transparent;
  margin: auto;
  display: block;
  left:0;
  right: 0;
  z-index:10;
}
<div id="navbar">
        <div class="navbar-links">
          <a [routerLinkActive]="['navbar-links-active']" [routerLink]="['link1']" (mouseover)="displayInsightDropDown();" (mouseleave)="hideInsightDropDown();">LINK 1</a>
          <a [routerLinkActive]="['navbar-links-active']" [routerLink]="['link2']">LINK 2</a> 
          <a [routerLinkActive]="['navbar-links-active']" [routerLink]="['link3']">LINK 3</a> 
        </div>
  </div>


推荐阅读