美文网首页
grid布局示例

grid布局示例

作者: 海豚先生的博客 | 来源:发表于2025-03-19 15:33 被阅读0次
<!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>

相关文章

  • Grid布局相关属性

    定义display:grid或inline-grid开启子元素的Grid布局。 不同于flex布局,grid布局是...

  • 【融职培训】Web前端学习 第2章 网页重构16 grid布局

    一、grid布局概述 grid布局与flex布局对比 grid布局可以为网页提供更强大的布局功能,它与flex布局...

  • CSS Grid 布局

    参考资料 CSS Grid 布局完全指南(图解 Grid 详细教程) CSS Grid 系列(上)-Grid布局完...

  • 2020-02-03

    六、栅格布局方式Grid 众人云,Grid布局是CSS中最强的布局方式。Grid 布局与 Flex 布局有一定的相...

  • css grid 布局 详解+代码示例

    本文基于阮一峰大神的文章 http://www.ruanyifeng.com/blog/2019/03/grid-...

  • css Grid布局

    Grid布局 css的布局方式主要有三种:float&position布局、flex布局、grid布局。 floa...

  • Grid布局参考资料

    张鑫旭-写给自己看的display: grid布局教程阮一峰-CSS Grid 网格布局教程 在Grid布局中,f...

  • grid 网格布局

    Grid网格布局 Grid布局是一个二维的布局方法,纵横两个方向总是同时存在。 作用在grid容器上作用在grid...

  • [2020-08-10]css3中的常用几种布局

    div的水平垂直居中 flex布局,使用display: flex实现水平垂直居中 grid布局 使用grid布局...

  • Grid网格布局学习

    Grid网格布局学习 引言 本文不对grid布局由来以及优劣做过多的介绍,仅介绍grid网格布局的用法及其效果显示...

网友评论

      本文标题:grid布局示例

      本文链接:https://www.haomeiwen.com/subject/acttvjtx.html