簡體   English   中英

除非必須換行,否則如何使元素填充剩余空間

[英]How do I make an element fill remaining space unless it must wrap

我有兩個div 我試圖讓其中一個填充剩余的空間,直到它必須包裹。

 #resize { resize: both; display: inline-block; background: #0f0; width: 100px; height: 100px; overflow: hidden; } #div1 { display: inline-block; background: #f00; } #div2 { display: inline-block; background: #00f; }
 <div id="resize"> <div id="div1">div1</div> <div id="div2">div2 (remaining space)</div> </div>

我可以實現填充:

 #resize { resize: both; display: inline-block; background: #0f0; width: 100px; height: 100px; overflow: hidden; /*fill*/ display: flex } #div1 { display: inline-block; background: #f00; } #div2 { display: inline-block; background: #00f; /*fill*/ flex-grow: 1 }
 <div id="resize"> <div id="div1">div1</div> <div id="div2">div2 (remaining space)</div> </div>
但是當它們溢出時我不能讓div垂直換行

您是否允許使用彈性框,如果是,那么您可以在 CSS 下面使用以獲得所需的結果

#resize {
 display: flex;
 flex-wrap: wrap;
 align-items: baseline;
 ...removed other CSS for readability
}

#div2 {
 flex: 1;
 min-width: 151px; //fixed min-height to wrap at certain width
 ...removed other CSS for readability
}

 #resize { resize: both; display: flex; flex-wrap: wrap; align-items: baseline; background: #0f0; width: 100px; height: 100px; overflow: hidden; } #div1 { display: inline-block; background: #f00; } #div2 { flex: 1; min-width: 151px; display: inline-block; background: #00f; }
 <div id="resize"> <div id="div1">div1</div> <div id="div2">div2 (remaining space)</div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM