3 回答
TA贡献1828条经验 获得超3个赞
有什么方法可以用Flexbox实现这一点吗?
基本上没有。FLEX等高特性基于容器的高度,而不是任何特定的同级。
所以sibling-1和sibling-2总是一样高。
但是,柔性盒没有内置的机制来限制项目的高度到一个兄弟姐妹的高度。
考虑JavaScript或CSS定位属性。
下面是一个使用绝对定位的示例:
.flex {
display: flex;
width: 200px;
position: relative;
}
.flex>div {
flex: 0 0 50%;
border: 1px solid black;
box-sizing: border-box;
}
.sibling-2 {
position: absolute;
left: 50%;
top: 0;
bottom: 0;
right: 0;
overflow: auto;
}
<div class="flex">
<div class="sibling-1">text<br>text<br>text<br>text<br>text<br>text<br></div>
<div class="sibling-2">text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br></div>
</div>
TA贡献1829条经验 获得超7个赞
是的,这是可能的。让兄弟姐妹单独设置最大高度,并设置其他人的flex-basis: 0和flex-grow: 1,根据规范,它们会扩展到兄弟姐妹的高度。没有绝对定位。没有设置任何元素的高度。
main {
display: flex;
}
section {
display: flex;
flex-direction: column;
width: 7em;
border: thin solid black;
margin: 1em;
}
:not(.limiter)>div {
flex-basis: 0px;
flex-grow: 1;
overflow-y: auto;
}
<main>
<section>
<div>I'm longer and will scroll my overflow. in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text
in flow text in flow text in flow text in flow text in flow text in flow text in</div>
</section>
<section class="limiter">
<div>Every parent's siblings match my height. in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text</div>
</section>
<section>
<div>I'm shorter but still match the height. in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text</div>
</section>
</main>
TA贡献1812条经验 获得超5个赞
<div class="sibling-1 flex sibling"></div><div class="sibling-2 flex sibling"> <div class="absolute flex scroller-wrap"> <div class="relative vertical-scroller"> your content here </div> </div></div>
.relative{ position:relative;}.absolute{ position:absolute;}.flex{ display:flex;}.sibling-2{ flex:1; }.scroller-wrap{ height:100%;}
- 3 回答
- 0 关注
- 568 浏览
相关问题推荐
添加回答
举报