CSS 强制将子级嵌套在父级后面,位置:固定

dun*_*140 2 html css z-index

我有一个固定的菜单栏,其中包含一个简单的<ul> <li>菜单系统。在li:hover我旁边有一个子菜单系统出现,这是相对于每个li的。不幸的是,这一点总是出现在所有父母的头顶上。

当我实际上希望它位于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;
}
Run Code Online (Sandbox Code Playgroud)

小提琴

eas*_*wee 5

如果您确实想保留该 html,则需要创建一个新的堆栈上下文。#sidebarhas position: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;

}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)