Flex布局-實例

一、骰子的布局

骰子的一面最多可以放置9個點,下面用flex實現(xiàn)從1個點到9個點的布局。


image.png

如果不加說明,本節(jié)的HTML模板一律如下。

<div class="box">
  <span class="item"></span>
</div>

上面代碼中,div元素是Flex容器,span元素是Flex項目。如果有多個項目,就要添加多個span元素,以此類推。

1.1 單項目

首先,只有左上角1個點的情況。Flex布局默認(rèn)就是首行左對齊,所以一行代碼就夠了。

  • 1.1.1


    image.png
.box{
  display: flex;
}

設(shè)置項目的對齊方式,就能實現(xiàn)居中對齊和右對齊。

  • 1.1.2


    image.png
.box{
  display: flex;
  justify-content: flex-center;
}
  • 1.1.3


    image.png
.box{
   display: flex;
   justify-content: flex-end;
}

設(shè)置交叉軸的對齊方式

  • 1.1.4


    image.png
.box{
  display: flex;
  align-items: center;
}
  • 1.1.5


    image.png
.box{
  display: flex;
  justify-content: center;
  align-items: center;
}
  • 1.1.6


    image.png
.box{
  display: flex;
  justify-content: center;
  align-items: flex-end;
  • 1.1.7


    image.png
.box{
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
}
1.2 雙項目
  • 1.2.1


    image.png
.box{
  display: flex;
  justify-content: space-between;
}
  • 1.2.2


    image.png
.box{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
  • 1.2.3


    image.png
.box{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
  • 1.2.4


    image.png
.box{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-end;
}
  • 1.2.5


    image.png
.box{
  display: flex;
}
.item:nth-child(2){
  align-self: center;
}
  • 1.2.6


    image.png
.box{
  display: flex;
  justify-content: space-between;
}
 .item:nth-child(2) {
    align-self: flex-end;
  }
1.3 三項目
  • 1.3.1


    image.png
.box {
  display: flex;
}

.item:nth-child(2) {
  align-self: center;
}

.item:nth-child(3) {
  align-self: flex-end;
}
1.4 四項目
  • 1.4.1


    image.png
.box {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-content: space-between;
}
  • 1.4.2


    image.png
<div class="box">
  <div class="column">
    <span class="item"></span>
    <span class="item"></span>
  </div>
  <div class="column">
    <span class="item"></span>
    <span class="item"></span>
  </div>
</div>
.box {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
}

.column {
  flex-basis: 100%;
  display: flex;
  justify-content: space-between;
}
1.5 六項目
  • 1.5.1


    image.png
.box {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
}
  • 1.5.2


    image.png
.box {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: space-between;
}
  • 1.5.3


    image.png
<div class="box">
  <div class="row">
    <span class="item"></span>
    <span class="item"></span>
    <span class="item"></span>
  </div>
  <div class="row">
    <span class="item"></span>
  </div>
  <div class="row">
     <span class="item"></span>
     <span class="item"></span>
  </div>
</div>

...
<style>
.box {
  display: flex;
  flex-wrap: wrap;
}
.row{
  flex-basis: 100%;
  display:flex;
}
.row:nth-child(2){
  justify-content: center;
}
.row:nth-child(3){
  justify-content: space-between;
}
</style>
1.6 九項目
image.png
.box {
  display: flex;
  flex-wrap: wrap;
}

二、網(wǎng)格布局

2.1 基本網(wǎng)格布局

最簡單的網(wǎng)格布局,就是平均分布。在容器里面平均分配空間,跟上面的骰子很像,但是需要設(shè)置項目的自動縮放。


image.png

HTML代碼如下:

<div class="Grid">
  <div class="Grid-cell">...</div>
  <div class="Grid-cell">...</div>
  <div class="Grid-cell">...</div>
</div>

CSS代碼如下:

  .Grid{
    display: flex;
  }
  .Grid-cell{
    flex: 1;
  }
2.2 百分比布局

某個網(wǎng)格的寬度為固定的百分比,其余網(wǎng)格平均分配剩余的空間。


image.png

HTML代碼如下:

<div class="Grid">
  <div class="Grid-cell u-1of4">...</div>
  <div class="Grid-cell">...</div>
  <div class="Grid-cell u-1of3">...</div>
</div>
.Grid {
  display: flex;
}
.Grid-cell {
  flex: 1;
}
.Grid-cell.u-full {
  flex: 0 0 100%;
}
.Grid-cell.u-1of2 {
  flex: 0 0 50%;
}
.Grid-cell.u-1of3 {
  flex: 0 0 33.3333%;
}
.Grid-cell.u-1of4 {
  flex: 0 0 25%;
}

三、圣杯布局

圣杯布局(Holy Grail Layout)指的是一種常見的網(wǎng)站布局。頁面從上到下,分為三個部分:頭部(header),軀干(body),尾部。其中軀干又水平分為三欄,從左到右為:導(dǎo)航、主欄、副欄。


image.png

HTML代碼如下:

<body class="HolyGrail">
  <header>...</header>
  <div class="HolyGrail-body">
    <main class="HolyGrail-content">...</main>
    <nav class="HolyGrail-nav">...</nav>
    <aside class="HolyGrail-ads">...</aside>
  </div>
  <footer>...</footer>
</body>

CSS代碼如下:

.HolyGrail {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
}
header,
footer {
  flex: 1;
}
.HolyGrail-body {
  display: flex;
  flex: 1;
}
.HolyGrail-content {
  flex: 1;
}
.HolyGrail-nav, .HolyGrail-ads {
  /* 兩個邊欄的寬度設(shè)為12em */
  flex: 0 0 12em;
}
.HolyGrail-nav {
  /* 導(dǎo)航放到最左邊 */
  order: -1;
}

如果是小屏幕,軀干的三欄自動變?yōu)榇怪悲B加。

@media (max-width: 768px) {
  .HolyGrail-body {
    flex-direction: column;
    flex: 1;
  }
  .HolyGrail-nav,
  .HolyGrail-ads,
  .HolyGrail-content {
    flex: auto;
  }
}

四、輸入框的布局

我們常常需要在輸入框的前方添加提示,后方添加按鈕。


image.png

HTML代碼如下:

<div class="InputAddOn">
  <span class="InputAddOn-item">...</span>
  <input class="InputAddOn-field">
  <button class="InputAddOn-item">...</button>
</div>

CSS代碼如下:

.InputAddOn {
  display: flex;
}
.InputAddOn-field {
  flex: 1;
}
五、懸掛式布局

有時,主欄的左側(cè)或右側(cè),需要添加一個圖片欄。


image.png

HTML代碼如下:

<div class="Media">
  <img class="Media-figure" src="" alt="">
  <p class="Media-body">...</p>
</div>

CSS代碼如下:

.Media {
  display: flex;
  align-items: flex-start;
}
.Media-figure {
  margin-right: 1em;
}
.Media-body {
  flex: 1;
}
六、固定的底欄

有時,頁面內(nèi)容太少,無法占滿一屏的高度,底欄就會抬高到頁面的中間。這時可以采用Flex布局,讓底欄總是出現(xiàn)在頁面的底部。


image.png

HTML代碼:

<body class="Site">
  <header>...</header>
  <main class="Site-content">...</main>
  <footer>...</footer>
</body>

CSS代碼:

.Site {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
}
.Site-content {
  flex: 1;
}
七、流式布局

每行的項目數(shù)固定,會自動分行。


image.png

CSS寫法:

.parent {
  width: 200px;
  height: 150px;
  background-color: black;
  display: flex;
  flex-flow: row wrap;
  align-content: flex-start;
}
.child {
  box-sizing: border-box;
  background-color: white;
  flex: 0 0 25%;
  height: 50px;
  border: 1px solid red;
}

本文來自阮一峰-Flex 布局教程:實例篇,僅供自己學(xué)習(xí)參考整理。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 上一篇文章介紹了Flex布局的語法,今天介紹常見布局的Flex寫法。你會看到,不管是什么布局,F(xiàn)lex往往都可以幾...
    君臨天下夜未央閱讀 3,906評論 1 12
  • 先來看看幾種常見的布局方式 我的主要參考資料是Landon Schropp的文章和Solved by Flexbo...
    Sylvie_9459閱讀 727評論 0 2
  • 前言: Flex布局的靈活性讓它可以應(yīng)用在任何場景,徹底代替了傳統(tǒng)布局,除了IE基本都可以兼容,是PC和移動端的布...
    Chris__Liu閱讀 447評論 0 0
  • 一、基本網(wǎng)格布局 最簡單的網(wǎng)格布局,就是平均分布。 HTML代碼如下: CSS代碼如下: 二、左邊固定,右邊自適應(yīng)...
    gitJason閱讀 314評論 0 1
  • 在CSS布局中,實現(xiàn)垂直居中、相同高度的列等一直是令人頭疼的問題,但不管是什么布局,F(xiàn)lex往往都可以幾行命令搞定...
    虹貓1992閱讀 9,361評論 0 7

友情鏈接更多精彩內(nèi)容