2016-03-03 91 views
0

我完全不理解flex模型。在這個例子中,第一行(第一個和第二個)的元素應該顯示在一行中:第一個元素爲200px,第二個元素應該有剩餘空間。flex模型:第一行元素寬度第二行

\t .wrapper { 
 
\t display: flex; 
 
      width: 100%; 
 
\t } 
 
\t .first { 
 
\t width: 200px; 
 
\t background: red; 
 
\t } 
 
\t .second { 
 
\t flex: 1; 
 
\t background: blue; 
 
\t } 
 
\t
<div class="wrapper"> 
 
    <div class="first">first left</div> 
 
    <div class="second">first right</div> 
 
</div>

現在我要添加第三元素,其中應正下方的第二個具有相同的寬度,則所述第二元件被放置。所以它不應該有完整頁面的100%,但只能顯示在第二個元素下面。

<div class="wrapper"> 
    <div class="first">first left</div> 
    <div class="second">first right</div> 
    <div class="third">second</div> 
</div> 

回答

1

你可以這樣做,使用calc功能。

.wrapper { 
 
    display: flex; 
 
    display: -webkit-flex; 
 
    width: 100%; 
 
    flex-flow: row wrap; 
 
    -webkit-flex-flow: row wrap; 
 
    justify-content: flex-end; 
 
} 
 

 
.first { 
 
    width: 200px; 
 
    background: red; 
 
} 
 

 
.second { 
 
    width: calc(100% - 200px); 
 
    background: blue; 
 
} 
 

 
.third { 
 
    background: yellow; 
 
    width: calc(100% - 200px); 
 
}
<div class="wrapper"> 
 
    <div class="first">first left</div> 
 
    <div class="second">first right</div> 
 
    <div class="third">second right</div> 
 
</div>

+0

這不是工作正確的,因爲紅色和藍色元素不在一個行(Safari瀏覽器),如果我想保留一切動態顯示什麼?紅色彎曲1和藍色彎曲3 ... – user3142695

+1

這是正確的,只是缺少一些Safari版本的'-webkit-'前綴。 –

相關問題