使用CSS Grid实现网格布局

使用CSS Grid实现网格布局

实现效果

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>
  <style>
    .box {
      
      
      display: grid;
      grid-template-columns: 1fr 1fr; /*属性定义每一列的列宽,fr比例关系*/
      /*grid-template-rows: repeat(3, 33.33%);*/ /*每一行的行高,repeat()函数,简化重复的值。第一个参数是重复的次数3,第二个参数是所要重复的值*/
      border: 1px solid red;
      gap: 20px 30px; /*行间距 列间距 grid-row-gap与grid-column-gap简写*/
      /* grid-row-gap: 20px;
      grid-column-gap: 30px; */
      grid-auto-flow: row dense; /* 表示"先行后列"并且尽量填满空格 */
    }
    .box div {
      
      
      border: 1px solid blue;
    }
    .box1 {
      
      
      height: 870px;
      grid-row: 1 / span 2; /*行的高度,span 2为两倍。即行为第一条上网格线和第三条网格线*/
    }
    .box2 {
      
      
      height: 350px;
    }
    .box3 {
      
      
      height: 540px;
    }
    .box4 {
      
      
      height: 350px;
      grid-row: 3 / span 2;
      grid-column: 1 / 2;/*左边框垂直网格线、右边框垂直网格线*/
    }
    .box5 {
      
      
      height: 205px;
      align-self: start; /*单元格内容的垂直位置(上中下)*/
    }
    .box6 {
      
      
      height: 100px;
      align-self: start;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="box1"><p>1</p></div>
    <div class="box2"><p>2</p></div>
    <div class="box3"><p>3</p></div>
    <div class="box4"><p>4</p></div>
    <div class="box5"><p>5</p></div>
    <div class="box6"><p>6</p></div>
    <div class="box7"><p>7</p></div>
    <div class="box8"><p>8</p></div>
    <div class="box9"><p>9</p></div>
  </div>
</body>
</html>

参考链接

阮一峰Grid教程

猜你喜欢

转载自blog.csdn.net/qq_34661750/article/details/128850773