Z-index不适用于flex元素?

F. *_*val 9 css z-index overlap css3 flexbox

我正在尝试有两列,一个是可以扩展并与另一列重叠的菜单.但是我使用了一个flex元素来包装这些列,我的菜单扩展到了另一个元素后面,即使有更大的元素z-index.

渲染是这样的:

.main {
  font-family: 'Open Sans', Helvetica, sans-serif;
  display: flex;
  background-color: #99baef;
}

nav {
  height: 100%;
  width: 8em;
  background-color: black;
  z-index: 1;
}

.navbox {
  box-sizing: border-box;
  background-color: black;
  color: white;
  height: 4em;
  width: 100%;
  text-align: center;
  line-height: 4em;
  border-top: 1px dotted #99baef;
  transition: all 1s;
}

.navbox:hover {
  width: 130%;
  border-top: none;
  background-color: #4a77c4;
  color: black;
}

.container {
  padding: 1em;
}

a {text-decoration: inherit;}
Run Code Online (Sandbox Code Playgroud)
<div class="main">
  <div class="maincolumn">
    <nav>
      <a href="">
        <div class="navbox">
          Nav 1
        </div>
      </a>
      <a href="">
        <div class="navbox">
          Nav 2
        </div>
      </a>
    </nav>
  </div>
  <div class="maincolumn">
    <div class="container">
      <h2>Title</h2>
      <p>This is a text.</p>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

看到?扩展时,我希望我的菜单与页面的其余部分重叠.我怎样才能做到这一点?

Vad*_*kov 9

Flex项目只是flex-container的直接子项.

Flex项目遵循z-index顺序,但您z-index不是应用于flex项目,而是应用于它们的后代.

来自w3c flexbox规范:

Flex的项目漆完全一样,内嵌块CSS21,除了order修饰的文档顺序代替原文档顺序使用,而z-index其它值比auto创建堆叠环境,即使positionstatic.

因此,为了使其工作,您应该z-index对您的第一个弹性项目应用更多.演示:

.main {
  font-family: 'Open Sans', Helvetica, sans-serif;
  display: flex;
  background-color: #99baef;
}

.maincolumn:first-child {
  z-index: 1;
}

nav {
  height: 100%;
  width: 8em;
  background-color: black;
}

.navbox {
  box-sizing: border-box;
  background-color: black;
  color: white;
  height: 4em;
  width: 100%;
  text-align: center;
  line-height: 4em;
  border-top: 1px dotted #99baef;
  transition: all 1s;
}

.navbox:hover {
  width: 130%;
  border-top: none;
  background-color: #4a77c4;
  color: black;
}

.container {
  padding: 1em;
}

a {text-decoration: inherit;}
Run Code Online (Sandbox Code Playgroud)
<div class="main">
  <div class="maincolumn">
    <nav>
      <a href="">
        <div class="navbox">
          Nav 1
        </div>
      </a>
      <a href="">
        <div class="navbox">
          Nav 2
        </div>
      </a>
    </nav>
  </div>
  <div class="maincolumn">
    <div class="container">
      <h2>Title</h2>
      <p>This is a text.</p>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Ori*_*ori 6

z-index属性仅影响位置值不是静态值(默认值)或弹性项目(display: flex或的直接子项)的元素display: inline-flex

有两种选择可以使z-index您的工作顺利进行:

  1. 将设置z-index为1st .maincolumn,这是一个flexbox项:

    .maincolumn:first-child {
      z-index: 1;
    }
    
    Run Code Online (Sandbox Code Playgroud)

.maincolumn:first-child {
  z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)
.main {
  font-family: 'Open Sans', Helvetica, sans-serif;
  display: flex;
  background-color: #99baef;
}

.maincolumn:first-child {
  z-index: 1;
}

nav {
  height: 100%;
  width: 8em;
  background-color: black;
}

.navbox {
  box-sizing: border-box;
  background-color: black;
  color: white;
  height: 4em;
  width: 100%;
  text-align: center;
  line-height: 4em;
  border-top: 1px dotted #99baef;
  transition: all 1s;
}

.navbox:hover {
  width: 130%;
  border-top: none;
  background-color: #4a77c4;
  color: black;
}

.container {
  padding: 1em;
}

a {text-decoration: inherit;}
Run Code Online (Sandbox Code Playgroud)

要么

  1. 设置position: relativenav

    nav {
       position: relative;
       height: 100%;
       width: 8em;
       background-color: black;
       z-index: 1;
     }
    
    Run Code Online (Sandbox Code Playgroud)

<div class="main">
  <div class="maincolumn">
    <nav>
      <a href="">
        <div class="navbox">
          Nav 1
        </div>
      </a>
      <a href="">
        <div class="navbox">
          Nav 2
        </div>
      </a>
    </nav>
  </div>
  <div class="maincolumn">
    <div class="container">
      <h2>Titre</h2>
      <p>This is a text.</p>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
nav {
   position: relative;
   height: 100%;
   width: 8em;
   background-color: black;
   z-index: 1;
 }
Run Code Online (Sandbox Code Playgroud)