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>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容