CSS 具有绝对位置的 z-index
CSS z-index with position absolute
我有简单的代码
问题是红色 div。它是动画的,但它的 z-index 比它的父级大,所以在动画时它遮盖了主要 div。当我给 animated div z-index: -1 但是 :hover 不能正常工作(animated div 消失)时,我可以解决这个问题。有人可以帮忙吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.main {
width: 100px;
height: 100px;
background: green;
position: relative;
}
.main .bar {
height: inherit;
width: 300px;
position: absolute;
background: red;
left: -300px;
}
.main:hover .bar {
left: 100px;
transition: left .3s;
}
</style>
</head>
<body>
<div class="main"><div class="bar"></div></div>
</body>
</html>
解决方案是将 .main
的内容包装在一个 z-index
大于 .bar
的内容器中。
.main {
width: 100px;
height: 100px;
position: relative;
}
.main > .bar {
height: inherit;
width: 300px;
position: absolute;
background: red;
left: -300px;
}
.main > .content {
height: 100%;
background: green;
position: relative;
z-index: 1;
}
.main:hover .bar {
left: 100px;
transition: left .3s;
}
<div class="main">
<div class="bar"></div>
<div class="content"></div>
</div>
我有简单的代码
问题是红色 div。它是动画的,但它的 z-index 比它的父级大,所以在动画时它遮盖了主要 div。当我给 animated div z-index: -1 但是 :hover 不能正常工作(animated div 消失)时,我可以解决这个问题。有人可以帮忙吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.main {
width: 100px;
height: 100px;
background: green;
position: relative;
}
.main .bar {
height: inherit;
width: 300px;
position: absolute;
background: red;
left: -300px;
}
.main:hover .bar {
left: 100px;
transition: left .3s;
}
</style>
</head>
<body>
<div class="main"><div class="bar"></div></div>
</body>
</html>
解决方案是将 .main
的内容包装在一个 z-index
大于 .bar
的内容器中。
.main {
width: 100px;
height: 100px;
position: relative;
}
.main > .bar {
height: inherit;
width: 300px;
position: absolute;
background: red;
left: -300px;
}
.main > .content {
height: 100%;
background: green;
position: relative;
z-index: 1;
}
.main:hover .bar {
left: 100px;
transition: left .3s;
}
<div class="main">
<div class="bar"></div>
<div class="content"></div>
</div>