【发布时间】:2019-05-07 10:26:29
【问题描述】:
我有一个侧栏和一个主要内容,每个都有自己的滚动条。 我试图实现一个结果,即主要内容的红色背景始终可见,无论是否滚动,也无论弹性项目的高度是否高于其父项。 这可能吗?
我目前的问题是,当我尝试滚动时,主 div 的背景消失了。背景与主体高度完全相同,这不是目标。
如果我将主要内容的align-items 从默认的stretch 更改为flex-start,则背景会正确扩展为长(高)弹性项目,但不适用于短弹性项目。
如果我让它伸展,我会遇到另一个问题,正如您在下面的 sn-p 中看到的那样:背景一直到底部(正确)直到滚动,在这种情况下背景会丢失。
我可以以某种方式始终拥有我的红色/橙色背景吗?
* { box-sizing:border-box; }
html, body {
width:100%;
height:100%;
padding:0;
margin:0;
}
.flex-item {
background-color:red;
border:solid 1px white;
}
.box {
width:300px;
height:300px;
border:solid 2px black;
margin:4px;
}
.orange {
background:orange;
}
<div style="display:flex; height:100%;">
<div style="flex:0 0 300px; background-color:green; height:100%; overflow:auto;">
We are two divs, I am the sidebar and the one next to me is the main content. Each of us has its own scrollbar. But my red brother has an issue with his background if you scroll...<br><br>
How could we solve that?
<br><br><br>
Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
</div>
<div style="display:flex; overflow:auto; height:100%;">
<div class="flex-item">
<div class="box">i am a box. The background behind me should be red.</div>
<div class="box">i am a box. The background behind me should be red.</div>
<div class="box">i am a box. The background behind me should be red.</div>
<div class="box">i am a box. The background behind me should be red.</div>
<div class="box">i am a box. The background behind me should be red.</div>
</div>
<div class="flex-item orange">
<div class="box">i am a box. The background behind me should be orange.</div>
<div class="box">i am a box. The background behind me should be orange.</div>
<div class="box">i am a box. The background behind me should be orange.</div>
<div class="box">i am a box. The background behind me should be orange.</div>
<div class="box">i am a box. The background behind me should be orange.</div>
</div>
<div class="flex-item">
<div class="box">i am a box. The background behind me should be red.</div>
<div class="box">i am a box. The background behind me should be red.</div>
</div>
</div>
</div>
【问题讨论】: