CSS Force嵌套在父级后面的子位置:fixed

时间:2015-09-15 14:08:45

标签: 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;
}

Fiddle

1 个答案:

答案 0 :(得分:3)

如果你真的想保留那个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>