子菜单在Flexbox中消失

时间:2017-07-30 12:36:23

标签: css drop-down-menu flexbox

当我尝试将网站导航添加到Flexbox布局中时,子菜单将不再可访问。当鼠标离开父列表元素时,它们会消失。

最终目标是使用flexbox修复导航。如果遗漏了body-header-header-tag,那么导航就像预期的那样工作。有什么想法吗?



* {
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  flex: 0 0 auto;
  background: yellow;
}

.site-navigation {
  font-family: 'Open Sans', 'Helvetica', 'Arial', sans-serif;
  font-weight: 400;
  color: #666;
}

nav ul {
  background: none;
  display: flex;
  flex-direction: column;
}

nav ul li {
  list-style-type: none;
  background: none;
}

nav ul li:hover {
  background: none;
}

nav ul li a {
  padding: 0.8rem 1rem;
  display: block;
  text-decoration: none;
  color: rgba(100, 100, 100, 0.8);
  background: none;
  text-transform: uppercase;
  font-size: 12px;
}

.has-children:hover>a {
  border: 1px solid #e5e5e5;
  border-bottom: none;
  background: #fff;
  margin-left: -1px;
  margin-right: -1px;
  margin-top: -1px;
  color: #000;
}

.has-children:hover>ul {
  border: 1px solid #e5e5e5;
  position: relative;
  z-index: -1;
}

.has-children:hover>ul>li {
  padding: 15px 15px;
}

.has-children>ul>li>a {
  text-transform: none;
  color: #666;
  border-bottom: 1px solid #E5E5E5;
  padding: 0;
  padding-bottom: 15px;
}

.has-children>ul>li:hover>a {
  border-bottom: 2px solid #d00;
  color: #000;
  padding-bottom: 14px;
}

@media only screen and (min-width:680px) {
  nav ul {
    flex-direction: row;
  }
  nav ul li {
    position: relative;
    flex: 0 0 auto;
    text-align: left;
  }
  nav ul li:hover ul {
    display: flex;
    flex-direction: column;
  }
  .has-children ul {
    display: none;
    position: absolute;
    top: 100%;
  }
  .has-children:hover ul {
    display: block;
    position: absolute;
    top: calc( 100% - 1px);
    width: 150%;
  }
}

<header class="site-header">
  <nav class="site-navigation">
    <ul class="site-navigation__list">
      <li class="site-navigation__item"><a href="/">Item 1</a>
      </li>
      <li class="site-navigation__item has-children"><a href="/">Item 2<span class="arrow arrow-down"></span></a>
        <ul class="site-navigation__sub-list">
          <li class="site-navigation__sub-item"><a href="#">Subitem 1</a></li>
          <li class="site-navigation__sub-item"><a href="#">Subitem 2</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</header>
<div class="content"><p>Content goes here!</p></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这似乎是z-index和背景问题。

您可以使用vd重置d并在子菜单中添加position:relative以隐藏重叠的内容。

可以完成的CSS更新:

z-index

background
nav ul {
  position:relative;
  z-index:1
}
nav ul li:hover > ul{
  background: white;
}

另一个更新可能是在父母徘徊时显示子菜单吗?

* {
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  flex: 0 0 auto;
  background: yellow;
}

.site-navigation {
  font-family: 'Open Sans', 'Helvetica', 'Arial', sans-serif;
  font-weight: 400;
  color: #666;
}

nav ul {
  background: none;
  display: flex;
  flex-direction: column;
}

nav ul {
  position:relative;
  z-index:1
}
nav ul li:hover > ul{
  background: white;
}

nav ul li ,nav ul li:hover{
  list-style-type: none;
  background: none;
}



nav ul li a {
  padding: 0.8rem 1rem;
  display: block;
  text-decoration: none;
  color: rgba(100, 100, 100, 0.8);
  background: none;
  text-transform: uppercase;
  font-size: 12px;
}

.has-children:hover>a {
  border: 1px solid #e5e5e5;
  border-bottom: none;
  background: #fff;
  margin-left: -1px;
  margin-right: -1px;
  margin-top: -1px;
  color: #000;
}

.has-children:hover>ul {
  border: 1px solid #e5e5e5;
  position: relative;
  z-index: -1;
}

.has-children:hover>ul>li {
  padding: 15px 15px;
}

.has-children>ul>li>a {
  text-transform: none;
  color: #666;
  border-bottom: 1px solid #E5E5E5;
  padding: 0;
  padding-bottom: 15px;
}

.has-children>ul>li:hover>a {
  border-bottom: 2px solid #d00;
  color: #000;
  padding-bottom: 14px;
}

@media only screen and (min-width:680px) {
  nav ul {
    flex-direction: row;
  }
  nav ul li {
    position: relative;
    flex: 0 0 auto;
    text-align: left;
  }
  nav ul li:hover ul {
    display: flex;
    flex-direction: column;
  }
  .has-children ul {
    display: none;
    position: absolute;
    top: 100%;
  }
  .has-children:hover ul {
    display: block;
    position: absolute;
    top: calc( 100% - 1px);
    width: 150%;
  }
}

https://codepen.io/anon/pen/MvKqqE

相关问题