彈性盒子用於頁面布局。
之前面試經常會問到:寫乙個三列布局,左右兩個定寬,中間的寬度自動伸縮門。
解決方法有很多種,可以設定三列浮動,左右定寬,中間寬度自動。
下面要說的是一種利用css布局來結局的比較好的方案。
使用box-flex屬性告訴瀏覽器如何分配元素之間的未使用的空間。
看個例子:
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
上述例子中就實現了要求中的布局。
這裡使用了彈性應用盒屬性。
父元素屬性設定display:box;子元素中,想要自動伸縮的盒子屬性設定box-flex,瀏覽器會分配定寬的元素,然後將剩下的空間按比例分配給伸縮的元素。
上述例子中修改:
.left
.mindle
可以發現,左邊元素和中間元素的比例為3:1,而且改變瀏覽器寬度,二者始終保持3:1。
這是最簡單的彈性布局,除此之外,彈性盒子還可以完成很多方向的布局:
比如子元素的高度比父元素的小,這樣設定資源的box-align來保持垂直高度的分配。
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
上面這個例子中,設定box-direction為reverse,意味將子元素反防線顯示。box-align表示彈性元素的對齊方向,擁有四個值:
start 元素沿容器頂邊放置,任何空間都在其下方顯示。
end元素沿容器底邊放置,任何空間都在其上方顯示。
center對於控制項北平分為兩部分,分別顯示在元素的上方和下方。
strech調整元素高度,已填充可用空間。
也可以為元素設定最大寬度,當元素達到最大寬度時,剩餘的空間自動分配。
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;this is a div;
box-pack屬性的值:
start:元素從左邊界開始放置,任何未分配的控制項顯示到最後乙個元素的右邊。
end:元素從右邊界開始放置,任何未分配的控制項顯示到最後乙個元素的左邊。
center:多餘空間分配到第乙個與阿奴的左邊和最後乙個元素的右邊。
justify:多餘空間均勻分配到各個元素之間。
目前沒有瀏覽器支援 box-flex 屬性。
firefox 支援替代的 -moz-box-flex 屬性。
safari、opera 以及 chrome 支援替代的 -webkit-box-flex 屬性。
CSS3學習站點,持續更新
w3schools css basics tizag html.net css dog html dog webdesign from scratch maxdesign university of texas css beauty little css stuff newcomers get co...
css3 彈性盒子
display webkit flex 定義元素為彈性盒子 屬性 direction 方向 flex direction row 預設值,子元素正向1 2 3 布局 flex direction row reverse 子元素正向3 2 1 布局 flex direction column 縱向布局...
Css3 彈性盒子
彈性盒子是 css3 的一種新的布局模式。css3 彈性盒 flexible box 或 flexbox 是一種當頁面需要適應不同的螢幕大小以及裝置型別時確保元素擁有恰當的行為的布局方式。引入彈性盒布局模型的目的是提供一種更加有效的方式來對乙個容器中的子元素進行排列 對齊和分配空白空間。彈性盒子由彈...