1. 程式人生 > 其它 >CSS3中的display:grid網格佈局介紹

CSS3中的display:grid網格佈局介紹

1.網格佈局(grid):

        它將網頁劃分成一個個網格,可以任意組合不同的網格,做出各種各樣的佈局;

2.基本概念:

        容器和專案,如圖所示:

    <div class="content">
        <div class="b">1</div>
        <div class="b">2</div>
        <div class="b">3</div>
        <div class="b">4</div>
        <div class="b">5</div>
        <div class="b">6</div>
        <div class="b">7</div>
        <div class="b">8</div>
        <div class="b">9</div>
    </div>

        .content即為容器,.b即為專案。

        行和列:

        行:row;

        列:column;

        

3.容器屬性

        display:grid;            //預設是塊元素;

        display:inline-grid;         //行內塊元素

        指定一個容器採用網格佈局;

        

        注意:設定為grid後,子元素的float,display: inline-block,display: table-cellvertical-aligncolumn-*

等設定都將失效。

        

.content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 100px 100px 100px;
            grid-template-rows: 100px 100px 100px;
        }

4.屬性解釋

        grid-template-columns:定義每一列的列寬;

        grid-template-columns:100px 100px 100px;      //總共三列,每列列寬是100px;=

        grid-template-rows:定義每一行的行高;

        grid-template-rows:100px 100px 100px;        //從上至下,每行高度為100px;

        //除了使用畫素,還可以使用百分比;

        拓展:

            重複寫值很麻煩,可以用repeate函式;

            repeat(次數,大小);

            例如:repeat(3,100px);            //重複3次,每次100px;

        repeat寫法:

        grid-template-columns:repeat(3,100px);

        grid-template-rows:repeat(3,100px);

        也可以重複某一種不固定大小模式;

        例如:

        grid-template-columns:100px 80px 100px;        

        改寫成:

        grid-template-columns:repeat(2,100px 80px);    //代表重複2次100px 80px的模式;也就是4列;

        等同於:

        grid-template-columns:100px 80px 100px80px;

        如圖: 

        

5.關鍵字

        1,auto-fill;如果容器大小不固定,專案大小固定,可以用auto-fill關鍵字自動填充;

        

    .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: repeat(auto-fill, 100px);
}

      

      2,fr(fraction):如果兩列的寬度分別為1fr2fr,就表示後者是前者的兩倍。

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 1fr 2fr;
            grid-template-rows: repeat(3, 100px 80px);
}

      

      fr也可以和px配合使用;

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 400px 1fr 2fr;
}

      

      3.minmax();

      grid-template-columns:1fr 5fr minmax(100px, 1fr);

      解釋:第一列是1fr,第二列是5fr,第三列最小值是100px,最大是1fr。當第二列fr無限大時和第三列到100px時,會往第一列借值;

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 1fr 5fr minmax(100px, 1fr);
}

      4.auto:自適應;

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 100px auto 100px;
}

  

6.網格線名稱:

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: [c1] 100px [c2] 100px [c3] auto [c4];
            grid-template-rows: [r1] 100px [r2] 100px [r3] auto [r4];
}

        解釋:指定每一根網格線的名字,方便以後的引用。

        也可以有多個名字;[c1,c1a]

7.間距

      row-gap:行間距;

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 100px 100px 100px;
            grid-template-rows: 100px 100px 100px;
            row-gap: 20px;
}

      

      colum-gap:列間距; 

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 100px 100px 100px;
            grid-template-rows: 100px 100px 100px;
            column-gap: 20px;
}

      

      簡寫:

      gap:20px 20px;

      row-gapcolumn-gap簡寫形式;

      gap省略了第二個值,瀏覽器認為第二個值等於第一個值。

8.區域

  grid-template-areas:網格佈局允許指定"區域"(area),一個區域由單個或多個單元格組成。grid-template-areas屬性用於定義區域。

        .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-areas: 'a b c' 'd e f' 'g h i';
        }

 

9.放置順序:

      grid-auto-flow:劃分網格以後,容器的子元素會按照順序,自動放置在每一個網格。預設的放置順序是"先行後列",即先填滿第一行,再開始放入第二行;

      預設是row;

      

.content{ box-shadow:001px#f6f; display:grid; grid-template-columns:100px100px100px; grid-template-rows:100px100px100px; grid-template-areas:'abc''def''ghi'; grid-auto-flow:column; }

      

10.單元格水平位置與垂直位置

      justify-items:屬性設定單元格內容的水平位置(左中右);

      align-items:屬性設定單元格內容的垂直位置(上中下);      

  • start:對齊單元格的起始邊緣。
  • end:對齊單元格的結束邊緣。
  • center:單元格內部居中。
  • stretch:拉伸,佔滿單元格的整個寬度(預設值)。

      簡寫:place-items

      place-items:align-itemsjustify-items


11.整體內容的位置:

      justify-content:整個內容區域在容器裡面的水平位置(左中右);

      align-content:整個內容區域的垂直位置(上中下)。

      

      .content {
            box-shadow: 0 0 1px #f6f;
            display: grid;
            grid-template-columns: 100px 100px 100px;
            grid-template-rows: 100px 100px 100px;
            justify-content: center;
            align-content: center;
        }

      簡寫:place-content

      place-contentalign-content justify-content