grid布局示例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>css grid</title>
</head>
<style>
  * {
    margin: 0;
    padding: 0;
  }
  .warp {
    width: 100%;
    width: 900px;
    height: 900px;
    background-color: #e6d4c2;
  }
  /* 容器元素 */
  #container {
    /* 指定一個容器采用網格布局 */
    display: grid;

    /* 定義每一列的列寬 */
    grid-template-columns: 200px 200px 200px;

    /* 定義每一行的行高 */
    grid-template-rows: 200px 200px 200px;

    /* 使用repeat函數,接受兩個參數,第一個參數是重復的次數,第二個參數是所要重復的值。 */
    /* grid-template-columns: repeat(3, 200px); */

    /* 按某種模式進行列寬的重復 */
    /* grid-template-columns: repeat(2, 200px 50px 100px); */

    /* 單元格的大小是固定的,但是容器的大小不確定。如果希望每一行(或每一列)容納盡可能多的單元格 */
    /* grid-template-columns: repeat(auto-fill, 200px); */
    /* grid-template-columns: repeat(auto-fit, 200px); */

    /* 列寬的比例關系 */
    /* grid-template-columns: 1fr 2fr 3fr; */
    
    /* 與絕對長度的單位結合使用 */
    /* grid-template-columns: 200px 1fr 2fr; */

    /* minmax函數表示一個長度范圍,它接受兩個參數,分別為最小值和最大值 */
    /* grid-template-columns: 200px 3fr minmax(100px, 1fr); */

    /* 由瀏覽器自己決定長度 */
    /* grid-template-columns: 1fr 1fr auto; */
    
    /* 行、列間距 */
    /* grid-column-gap: 20px; */
    
    /* grid-row-gap: 20px; */
    /* grid-gap: 20px 10px; */
    
    /* 行、列間距 */
    /* grid-gap: 20px; */
    
    width: 100%;
    width: 800px;
    height: 800px;
    background-color: #ccc;

    /* 都在一行時,多出空間的處理不同,fill最大200,fit平均分配 */
    /* grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); */
    /* grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); */
    
    /* 排序,先行后列或先列后行,盡可能緊密填滿 */
    /* grid-auto-flow: row; */
    
    /* 項目內容位置 */
    /* justify-items: end; */

    /* align-items: center; */

    /* place-items: center end; */
    /* place-items: center; */

    /* 容器內容位置 */
    /* justify-content: space-between; */

    /* align-content: center ; */
    /* 項目與項目的間隔相等,項目與容器邊框之間也是同樣長度的間隔 */
    /* place-content: space-evenly; */

    /* 自動設置多余的網格列寬和行高 */
    /* grid-auto-columns: 100px; */
    /* grid-auto-rows: 100px; */

    /* 定義每一行的行高 */
    grid-template-rows: 200px 200px 200px;
    
    /* 按某種模式進行行高的重復 */
    /* grid-template-rows:  repeat(1, 400px 200px); */
    
    /* 使用相對單位,相對于容器元素的父元素 */
    /* grid-template-columns: 33.33% 33.33% 33.33%; */
    
    
    /* grid-template-rows:  repeat(3, 100px); */

    /* 網格區域命名 */
    /* grid-template-areas: 'a a c' 
                         'd e c'
                         'g g h'
                         'j k l'; */
    }

  /* 子元素 */
  .item-1 {
    /* 需要去掉寬高 */
    /* 左邊框所在網格線 */
    /* grid-column-start: 1; */
    /* 右邊框所在網格線 */
    /* grid-column-end: 3; */

    /* 起止邊框線位置的簡寫形式 */
    /* grid-column: 1 / 3;
    
    grid-row: 1 / 3; */

    /* 指定該項目在網格中的哪個區域 */
    /* grid-area: a; */
  }
  .item-2 {
    /* grid-area: g; */
    /* 左邊框所在網格線 */
    /* grid-column-start: 2; */
    /* 右邊框所在網格線 */
    /* grid-column-end: 3; */
    /* grid-column: 3 / 5; */
    /* min-width: 200px; */
  }
  .item-3 {
    /* grid-area: c; */
    /* grid-column: 3 / 5;
    grid-row: 2 / 4; */
  }

  .item {
    width: 100px;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;

    color: #fff;
    font-size: 50px;
    /* text-align: center; */
  }

  p {
    display: inline-block;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    margin: 0;
  }

</style>
<body>
  <div class="warp">
    <div id="container">
      <div class="item item-1" style="background-color: #002ea6"><p>1</p></div>
      <div class="item item-2" style="background-color: #573023"><p>2</p></div>
      <div class="item item-3" style="background-color: #eb5c20"><p>3</p></div>
      <div class="item" style="background-color: #91b822"><p>4</p></div>
      <div class="item" style="background-color: #003153"><p>5</p></div>
      <div class="item" style="background-color: #80d1c8"><p>6</p></div>
      <div class="item" style="background-color: #006442"><p>7</p></div>
      <div class="item" style="background-color: #fac03d"><p>8</p></div>
      <div class="item" style="background-color: #b699dd"><p>9</p></div>
    </div>
  </div>
</body>
</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容