CSS 强制将 child 嵌套在 parent 和 position:fixed 后面

CSS Force nested child behind parent with position:fixed

我有一个包含简单 <ul> <li> 菜单系统的固定菜单栏。在 li:hover 之后,我在旁边出现了一个 sub-menu 系统,这是相对于每个里的。不幸的是,这一旁白总是出现在所有 parents 之上。

当我真正希望它位于div#sidebar 后面时。这可能吗?我在 z-index(包括 -1)方面运气不佳,如有任何帮助,我们将不胜感激!

<div id="sidebar">
    <nav class="secondary">
            <h2>Featured</h2>
            <ul>
                    <li>
                <a href="#">
                    <h3>Title</h3>
                </a>
                        <aside class="article-card">
                            <h4>TITLE</h4>
                            <h5>TEXT</h5>
                        </aside>
                    </li>
            </ul>
    </nav>
</div>


ul {
    list-style: none;
    display: inline-block;
    width: 59.6%;
    margin-right: 9.1%;
    float: right;
    margin-bottom: 40px;
}

li {
    display: block;
    margin-bottom: 10px;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -ms-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
}

#sidebar {
    background: #253e40;
    color: #8b8c91;
    width: 215px;
    height: 100%;
    position: fixed;
    top: 0;
    bottom: 0;
    right: 215px;
    margin-right: -215px; /* "#sidebar" width */
    z-index: 3;
}

#sidebar.active { margin-right: 0; }

#sidebar header {
    font-weight: bold;
    padding: 30px 20px 50px 20px;
    border-bottom: 1px solid #8b8c91;
    color: #8b8c91;
}

#sidebar footer {
    background: none;
    bottom: 40px;
    padding: 0 20px;
    position: absolute;
}

/* Nav */
#sidebar nav {
    width: 100%;
    margin: 20px 0 50px 0;
    display: inline-block;
}

#sidebar ul {
    width: 100%;
    margin: 0;
}

#sidebar li {
    margin-bottom: 0;
    padding: 2px 20px;
}

#sidebar li:before {
    content: none;
    padding: 0;
}

.current-menu-item {
    font-weight: bold;
    color: #fff;
}

#sidebar a:hover {
    color: #fff;
}

#sidebar nav.secondary h2 {
    font-weight: bold;
    color: #fff;
    padding: 0 20px 15px 20px;
    border-bottom: 1px solid #8b8c91;
}

#sidebar nav.secondary li {
    padding: 15px 20px;
    border-bottom: 1px solid #8b8c91;
}

#sidebar nav.secondary li:hover {
    background: #252f37;
    color: #fff;
}

/* Article Card Popout */
.article-card {
    position: absolute;
    background-color: #44484f;
    display: inline-block;
    width: 200px;
    height: auto;
    right: 15px;
    border-left: 5px solid #be572b;
}

#sidebar nav.secondary li:hover .article-card {
    right: 215px;
}

.article-card h4 {
    font-weight: bold;
    padding: 10px;
}

.article-card h5 {
    color: #fff;
    padding: 10px;
}

/* Transition animations */
#sidebar,
.article-card {
    -webkit-transition: all 0.7s ease-in-out;
    -moz-transition: all 0.7s ease-in-out;
    -ms-transition: all 0.7s ease-in-out;
    -o-transition: all 0.7s ease-in-out;
    transition: all 0.7s ease-in-out;
}

Fiddle

如果你真的想保留那个 html,你需要创建一个新的堆叠上下文。 #sidebarposition:fixed - 侧边栏内的元素使用新的堆叠上下文处理,该上下文现在从 #sidebar 开始,而不是在 body 级别。 侧边栏的子项不能位于 #sidebar 下面。

要解决此问题,请在侧边栏内添加另一个容器,其中包含所有 background 样式,并且与滑出组件位于相同的堆叠上下文中。

ul {
    list-style: none;
    display: inline-block;
    width: 59.6%;
    margin-right: 9.1%;
    float: right;
    margin-bottom: 40px;
}

li {
 display: block;
    margin-bottom: 10px;
 -webkit-transition: all 1s ease-in-out;
 -moz-transition: all 1s ease-in-out;
 -ms-transition: all 1s ease-in-out;
 -o-transition: all 1s ease-in-out;
 transition: all 1s ease-in-out;
}

#sidebar {
 background: #253e40;
 color: #8b8c91;
    width: 215px;
 height: 100%;
    position: fixed;
    top: 0;
    bottom: 0;
 right: 215px;
 margin-right: -215px; /* "#sidebar" width */
 z-index: 3;
}

#sidebar.active { margin-right: 0; }

.sidebar-content {
    height: 100%;
  background: #253e40;  
}

#sidebar header {
 font-weight: bold;
 padding: 30px 20px 50px 20px;
 border-bottom: 1px solid #8b8c91;
 color: #8b8c91;
}

#sidebar footer {
 background: none;
 bottom: 40px;
 padding: 0 20px;
 position: absolute;
}

/* Nav */
#sidebar nav {
 width: 100%;
 margin: 20px 0 50px 0;
 display: inline-block;
}

#sidebar ul {
 width: 100%;
 margin: 0;
}

#sidebar li {
 margin-bottom: 0;
 padding: 2px 20px;
}

#sidebar li:before {
 content: none;
 padding: 0;
}

.current-menu-item {
 font-weight: bold;
 color: #fff;
}

#sidebar a:hover {
 color: #fff;
}

#sidebar nav.secondary h2 {
 font-weight: bold;
 color: #fff;
 padding: 0 20px 15px 20px;
 border-bottom: 1px solid #8b8c91;
}

#sidebar nav.secondary li {
 padding: 15px 20px;
 border-bottom: 1px solid #8b8c91;
}

#sidebar nav.secondary li:hover {
 background: #252f37;
 color: #fff;
}

/* Article Card Popout */
.article-card {
    position: absolute;
    z-index: -1; // z index put's it below .sidebar-content
    background-color: #44484f;
    display: inline-block;
 width: 200px;
    height: auto;
    right: 15px;
    border-left: 5px solid #be572b;
}

#sidebar nav.secondary li:hover .article-card {
 right: 215px;
}

.article-card h4 {
 font-weight: bold;
 padding: 10px;
}

.article-card h5 {
 color: #fff;
 padding: 10px;
}

/* Transition animations */
#sidebar,
.article-card {
    -webkit-transition: all 0.7s ease-in-out;
    -moz-transition: all 0.7s ease-in-out;
    -ms-transition: all 0.7s ease-in-out;
    -o-transition: all 0.7s ease-in-out;
    transition: all 0.7s ease-in-out;

}
<div id="sidebar">
    <div class="sidebar-content">
    <nav class="secondary">
         <h2>Featured</h2>
   <ul>
    <a href="#">
                    <li>Title
                        <aside class="article-card">
                         <h4>TITLE</h4>
                            <h5>TEXT</h5>
                        </aside>
                    </li>
                </a>
   </ul>
    </nav>
    </div>
</div>