2016-06-10 140 views
38

我在柔版盒中並排放置2個div。右邊的一個應該總是相同的寬度,我想要左邊的一個只抓住剩餘的空間。但除非我專門設定寬度,否則不會。使柔版盒中剩餘的*水平*空間填充

所以目前,它被設置爲96%,這看起來不錯,直到你真的壓扁屏幕 - 然後右手div有點餓了它所需要的空間。

我想我可以離開它,因爲它是,但感覺錯了 - 就像必須有一個方式說:

正確的始終是相同的;您在左側 - 你得到剩下的

.ar-course-nav { 
 
    cursor: pointer; 
 
    padding: 8px 12px 8px 12px; 
 
    border-radius: 8px; 
 
} 
 
.ar-course-nav:hover { 
 
    background-color: rgba(0, 0, 0, 0.1); 
 
}
<br/> 
 
<br/> 
 
<div class="ar-course-nav" style="display:flex; justify-content:space-between;"> 
 
    <div style="width:96%;"> 
 
    <div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;"> 
 
     <strong title="Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!"> 
 
       Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer! 
 
      </strong> 
 
    </div> 
 
    <div style="width:100%; display:flex; justify-content:space-between;"> 
 
     <div style="color:#555555; margin-right:8px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;" title="A really really really really really really really really really really really long department name"> 
 
     A really really really really really really really really really really really long department name 
 
     </div> 
 
     <div style="color:#555555; text-align:right; white-space:nowrap;"> 
 
     Created: 21 September 2016 
 
     </div> 
 
    </div> 
 
    </div> 
 
    <div style="margin-left:8px;"> 
 
    <strong>&gt;</strong> 
 
    </div> 
 
</div>

回答

58

使用flex-grow屬性,以使柔性項目佔用的可用空間主軸線一切。

此屬性將盡可能擴大該項目,調整動態環境的長度,例如重新調整大小或添加/刪除其他項目。

一個常見示例是flex-grow: 1或使用速記屬性flex: 1

因此,而不是width: 96%在您的div,使用flex: 1


您寫道:

此刻

所以,它被設置爲96%,這看起來不錯,直到你真的壁球在屏幕上 - 然後右手DIV變得有點飢餓的空間它需要。

固定寬度div的擠壓是另一柔性屬性:flex-shrink

默認情況下,撓曲項都是flex-shrink: 1這使它們能夠以防止容器的溢出收縮。

要禁用此功能,請使用flex-shrink: 0

欲瞭解更多詳情,請參閱在回答這裏的flex-shrink因素部分:


瞭解更多關於沿主軸這裏彎曲排列:

瞭解更多關於Flex對準沿十字軸這裏:

+2

剛剛意識到我從來沒有接受這個的答案! (我很抱歉。) –