我有两个部分叠在一起,如下所示,这可以在这个jsfiddle中看到。
我希望下拉列表显示在底部,但是,设置z-index
无效。此外,我知道我可以更新.card
也是position: absolute
它会工作,但这打破了我的整个布局,所以不是一个真正的选择。
有没有人知道如何在不将.card
类更改为position: absolute
的情况下使下拉列表显示在底部?
h3, p {
margin: 0;
}
.wrapper {
display: flex;
flex: 1;
flex-direction: column;
}
.row-one {
display: flex;
flex: 1;
flex-direction: column;
flex: 0 1 200px;
background: red;
overflow-y: auto;
}
.card {
position: relative;
/* uncomment line below to see how the dropdown should look */
/* position: absolute; */
background: orange;
width: 200px;
height: 100px;
}
.dropdown {
height: 300px;
width: 200px;
background: blue;
z-index: 2;
position: absolute;
top: 50px;
left: 0;
}
.row-two {
flex: 1;
display: flex;
background: #fff;
flex-direction: column;
background: pink;
height: 100%;
}
<div class="wrapper">
<div class="row-one">
<h3>Top Section</h3>
<div class='card'>
<p>I'm a card</p>
<div class="dropdown">
<h1>DROPDOWN</h1>
<p>item</p>
<p>item</p>
<p>item</p>
<p>item</p>
<p>item</p>
</div>
</div>
</div>
<div class="row-two">
<h3>Bottom Section</h3>
<p>...content</p>
<p>...content</p>
<p>...content</p>
</div>
</div>
由于card
有position: relative
,绝对定位的儿童内部受到约束(这也适用于z-index)。你可以将position: relative
放在wrapper
上。
h3, p {
margin: 0;
}
.wrapper {
display: flex;
flex: 1;
flex-direction: column;
/* add positon relative */
position:relative;
}
.row-one {
display: flex;
flex: 1;
flex-direction: column;
flex: 0 1 200px;
background: red;
overflow-y: auto;
}
.card {
/* remove position relative */
background: orange;
width: 200px;
height: 100px;
}
.dropdown {
height: 300px;
width: 200px;
background: blue;
z-index: 2;
position: absolute;
top: 50px;
left: 0;
}
.row-two {
flex: 1;
display: flex;
background: #fff;
flex-direction: column;
background: pink;
height: 100%;
}
<div class="wrapper">
<div class="row-one">
<h3>Top Section</h3>
<div class='card'>
<p>I'm a card</p>
<div class="dropdown">
<h1>DROPDOWN</h1>
<p>item</p>
<p>item</p>
<p>item</p>
<p>item</p>
<p>item</p>
</div>
</div>
</div>
<div class="row-two">
<h3>Bottom Section</h3>
<p>...content</p>
<p>...content</p>
<p>...content</p>
</div>
</div>
z-index
不适用于静态定位的元素。你至少需要在position: relative;
上使用.dropdown
来为z-index
工作。另外我认为你不需要overflow-y: auto;
上的.row-one
,它应该更像overflow-y: visible;
。这是我的看法。
链接到jsfiddle:https://jsfiddle.net/gh5ne2or/
h3, p {
margin: 0;
}
.wrapper {
display: flex;
flex: 1;
flex-direction: column;
}
.row-one {
display: flex;
flex: 1;
flex-direction: column;
flex: 0 1 200px;
background: red;
overflow-y: visible;
}
.card {
position: relative;
/* uncomment line below to see how the dropdown should look */
/* position: absolute; */
background: orange;
width: 200px;
height: 100px;
}
.dropdown {
height: 300px;
width: 200px;
background: blue;
z-index: 2;
position: relative;
top: 0;
left: 0;
}
.row-two {
flex: 1;
display: flex;
background: #fff;
flex-direction: column;
background: pink;
height: 100%;
}
<div class="wrapper">
<div class="row-one">
<h3>Top Section</h3>
<div class='card'>
<p>I'm a card</p>
<div class="dropdown">
<h1>DROPDOWN</h1>
<p>item</p>
<p>item</p>
<p>item</p>
<p>item</p>
<p>item</p>
</div>
</div>
</div>
<div class="row-two">
<h3>Bottom Section</h3>
<p>...content</p>
<p>...content</p>
<p>...content</p>
</div>
</div>