使用 Flex 布局与其他普通布局的简单对比
最近使用 flex 布局來做各種居中真的帶來了不少方便,現在來總結一下平時的普通布局是怎樣使用 flex 布局來實現一樣的效果。
一、左右 1:1 布局
布局:
<div class="container"><div class="child">LEFT</div><div class="child">RIGHT</div> </div> 復制代碼利用 float 屬性
在使用 flex 之前,實現這種布局主要是利用 float 屬性,使元素脫離文檔流,同時因為要實現 1:1 的比例,要將子元素的 width 設為 50%。同時要記得在后面的元素添加 clear:both 屬性。
.container {margin: 40px;height: 600px; }.child {width: 50%;height: 100%;float: left;box-sizing: border-box;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼利用 flex-basis
如果使用 flex 布局,要將容器(父元素)display 設為 flex,項目(子元素)設定 flex-basis 值為 50%就可以實現 1:1 布局了,這個屬性和 width 作用差不多,主要是指定 flex 元素在主軸方向上的初始大小。
.container {margin: 40px;height: 600px;display: flex;flex-direction: row; }.child {height: 100%;flex-basis: 50%;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼利用 flex: 1
當 flex 取值為一個非負數字,則該數字為 flex-grow 值,flex-shrink 取 1,flex-basis 取 0%。于是就可以在子元素上使用 flex: 1 來實現平均布局,并且對于不同數量的子元素都適用。謝謝 @pingan8787
.container {margin: 40px;height: 600px;display: flex;flex-direction: row; }.child {height: 100%;flex: 1;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼二、左中右 1:1:1 布局
實現方法其實和上面差不多,都是使用 float 和 flex-basis 屬性來指定比例為 33.3%來實現。
布局:
<div class="container"><div class="child">LEFT</div><div class="child">MIDDLE</div><div class="child">RIGHT</div> </div> 復制代碼利用 float 屬性
.container {margin: 40px;height: 600px; }.child {width: 33.3%;height: 100%;float: left;box-sizing: border-box;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼利用 flex: basis
.container {margin: 40px;height: 600px;display: flex;flex-direction: row; }.child {height: 100%;flex-basis: 33.3%;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼利用 flex: 1
和上面的左右布局代碼一樣,比 flex-basis: 33.3% 好,推薦使用。
三、水平居中布局
布局:
<div class="container"><div class="child">CHILD</div> </div> 復制代碼利用 margin
相信大部分人都知道對于塊級子元素利用簡單的margin: 0 auto就能實現了吧,這就不多介紹了。
.container {margin: 40px;height: 600px; }.child {height: 100%;width: 50%;margin: 0 auto;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼利用 flex 布局
主要起作用的屬性是 justify-content,它定義了項目在主軸上的對齊方式,所以只需把它設為 center 就能實現了,前提是父元素的 flex-direction 屬性為 row,不過默認就是 row。
.container {margin: 40px;height: 600px;display: flex;justify-content: center; }.child {height: 100%;width: 50%;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;text-align: center;font-size: 40px;color: rgba(255, 255, 255, 0.7); } 復制代碼四、垂直居中布局
對于一般來說,都是利用上下padding來實現垂直居中,很少會有父元素height值為固定值這種需求。這里為了體現flex的特性,特地對比在父元素height值固定的情況下如何實現垂直居中。
布局:
<div class="container"><div class="child">CHILD</div> </div> 復制代碼利用 line-height
對于垂直居中就沒有水平居中這么好寫了,我平時主要是設置 line-height 配合 inline-block 這種方法來實現的。
將父元素 line-height 設為其 height 的值,并且將子元素的 display 屬性設為 inline-block,再利用 vertical-align 將自己定位在父元素垂直軸上中心就可實現了。但是要記得將子元素的 line-height 初始化(initial),因為 line-height 默認是繼承父元素的值的。
.container {margin: 40px;height: 600px;border: rgb(65, 184, 131) 8px solid;line-height: 600px;text-align: center; }.child {display: inline-block;height: 50%;width: 80%;vertical-align: middle;line-height: initial;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;color: rgba(255, 255, 255, 0.7);font-size: 40px; } 復制代碼利用 flex 布局
如果使用 flex 就很簡單了,要控制父元素的 align-items 屬性即可,它主要定義項目(子元素)在交叉軸上如何對齊。
.container {margin: 40px;height: 600px;border: rgb(65, 184, 131) 8px solid;display: flex;justify-content: center;align-items: center; }.child {height: 50%;width: 80%;background-color: rgb(53, 73, 94);border: rgb(65, 184, 131) 8px solid;color: rgba(255, 255, 255, 0.7);font-size: 40px;text-align: center; } 復制代碼利用 table
小結
flex布局對于做各種居中帶來了不小方便,同時如今現代瀏覽器對其兼容性也不錯了。最后推薦兩篇干貨文章。
- 阮一峰:Flex 布局教程(語法篇)
- 顏海鏡:CSS實現水平垂直居中的1010種方式
總結
以上是生活随笔為你收集整理的使用 Flex 布局与其他普通布局的简单对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于mysqld_multi实现MySQ
- 下一篇: PYPL 12 月 IDE 榜单:Ecl