grid 栅格/网格布局学习笔记

这篇具有很好参考价值的文章主要介绍了grid 栅格/网格布局学习笔记。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、前言

         栅格布局或者说网格布局是很好用的东西,像一些商城类的排版就很适合用栅格布局,但是存在一定的兼容性问题,兼容性暂时还没有研究,这边学习总结是针对grid也就是栅格布局的使用的学习总结,下面将介绍我认为的常用的几个属性,如果想要更详细的学习,可以参考阮一峰老师的grid布局

2、使用

首先是html

<template>

<div class="grid-box">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>

</template>

然后使用grid 布局列与行的列数行数都是根据需求来设置的,可以更改,更详细的可以搜阮一峰老师的grid布局查看,在本文首页有链接

.grid-box {
  // 栅格布局三行三列
  display: grid;
  // 3是多少行列 后面 100px是列宽,行的设置同理
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  .grid-item {
    font-size: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    background: rgb(239,52,41);
    &:nth-child(2){
      background: rgb(246,143,37);
    }
    &:nth-child(3){
      background: rgb(75,168,70);
    }
    &:nth-child(4){
      background: rgb(4,118,194);
    }
    &:nth-child(5){
      background: rgb(192,119,175);
    }
    &:nth-child(6){
      background: rgb(248,210,157);
    }
    &:nth-child(7){
      background: rgb(180,168,127);
    }
    &:nth-child(8){
      background: rgb(208,228,168);
    }
    &:nth-child(9){
      background: rgb(77,199,236);
    }
  }
}

救会出现这样的效果了

grid 栅格/网格布局学习笔记

如果想要好看点可以给父容器(.grid-box)加上一点内边距与阴影

padding: 10px;
box-shadow:  0 0 10px #999;

然后就得到了这样效果

grid 栅格/网格布局学习笔记

此时我么可能会有让各个数字有间隙的需求,此时就可以用上下面两个属性了,也是在父容器设置的

// 行列间距
column-gap: 10px;
row-gap: 10px;

然后效果就是这样的

 grid 栅格/网格布局学习笔记

  此时我们克呢该有会有这样的需求,就是按列排,而不是像上面按行排列,此时就需要使用到

 // 默认是按先行后列的,现在也可以改为先列后行 默认值是 row
 grid-auto-flow: column;

然后效果就是这样的

grid 栅格/网格布局学习笔记以上内容的完整代码如下

<template>

<div class="grid-box">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>

</template>

<script setup lang='ts'>
</script>
<style scoped lang="scss">
.grid-box {
  // 栅格布局三行三列
  display: grid;
  // 3是多少行列 后面 100px是列宽,行的设置同理
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  // 行列间距
  column-gap: 10px;
  row-gap: 10px;
  padding: 10px;
  box-shadow:  0 0 10px #999;
  // 默认是按先行后列的,现在也可以改为先列后行
  grid-auto-flow: column;
  .grid-item {
    font-size: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    background: rgb(239,52,41);
    &:nth-child(2){
      background: rgb(246,143,37);
    }
    &:nth-child(3){
      background: rgb(75,168,70);
    }
    &:nth-child(4){
      background: rgb(4,118,194);
    }
    &:nth-child(5){
      background: rgb(192,119,175);
    }
    &:nth-child(6){
      background: rgb(248,210,157);
    }
    &:nth-child(7){
      background: rgb(180,168,127);
    }
    &:nth-child(8){
      background: rgb(208,228,168);
    }
    &:nth-child(9){
      background: rgb(77,199,236);
    }
  }
}
</style>

 假如每个单元格里面各自有内容,可以是使用以下链各个属性进行布局,它们的值可以是

// 设置单元格的布局,但是这两个属性设置后会减该内容压缩,所以使用的时候要注意
// 他么可以取的值是 start | end | center | stretch 
justify-items: center;
align-items: center;

效果是这样的

grid 栅格/网格布局学习笔记  

 还可以设置整体内容的排版

// 设置容器内整体内容的排版
// 可取的值为 start | end | center | stretch | space-around | space-between | space-evenly;
justify-content: center;
align-content: center;

效果

grid 栅格/网格布局学习笔记

 以上就是我认为常用的容器的属性,解释一下容器与项目(容器内的第一层子元素(项目里面的元素不是项目))在这里其实就是

.grid-box与.grid-item

3、项目属性

 这里值写四个属性,其实不止,只不过作者是我认为常用的属性

grid-column-start属性:左边框所在的垂直网格线
grid-column-end属性:右边框所在的垂直网格线
grid-row-start属性:上边框所在的水平网格线
grid-row-end属性:下边框所在的水平网格线
// 设置单元格的内容样式
grid-column-start: 2;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;

效果

grid 栅格/网格布局学习笔记

代码:

<template>

<div class="grid-box">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>

</template>

<script setup lang='ts'>
</script>
<style scoped lang="scss">
.grid-box {
  // 栅格布局三行三列
  height: 100%;
  display: grid;
  // 3是多少行列 后面 100px是列宽,行的设置同理
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  // 行列间距
  column-gap: 10px;
  row-gap: 10px;
  padding: 10px;
  box-shadow:  0 0 10px #999;
  // 默认是按先行后列的,现在也可以改为先列后行()colunm)
  grid-auto-flow: row;
  // 设置单元格的布局,但是这两个属性设置后会减该内容压缩,所以使用的时候要注意
  // justify-items: center;
  // align-items: center;
  // 设置容器内整体内容的排版
  justify-content: center;
  align-content: center;
  .grid-item {
    font-size: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    background: rgb(239,52,41);
    &:nth-child(1) {
      // 设置单元格的内容样式
      grid-column-start: 2;
      grid-column-end: 4;
      grid-row-start: 1;
      grid-row-end: 3;
    }
    &:nth-child(2){
      background: rgb(246,143,37);
    }
    &:nth-child(3){
      background: rgb(75,168,70);
    }
    &:nth-child(4){
      background: rgb(4,118,194);
    }
    &:nth-child(5){
      background: rgb(192,119,175);
    }
    &:nth-child(6){
      background: rgb(248,210,157);
    }
    &:nth-child(7){
      background: rgb(180,168,127);
    }
    &:nth-child(8){
      background: rgb(208,228,168);
    }
    &:nth-child(9){
      background: rgb(77,199,236);
    }
  }
}
</style>

这是一个组件内容,需要在app.vue中引入使用

以上就是我对grid学习的笔记总结,欢迎批评指正,交流学习 文章来源地址https://www.toymoban.com/news/detail-450230.html

到了这里,关于grid 栅格/网格布局学习笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Unity 网格布局控件-Grid Layout Group

    Unity 网格布局控件-Grid Layout Group

    Unity 网格布局控件-Grid Layout Group是Unity中的UGUI控件,用于在 UI 中创建网格布局, 它的作用是:自动将子对象排列成网格,即我们可以通过该组件对子对象按行和列的形式排列,根据指定的约束条件自动调整它们的大小和位置。通常我们使用它创建具有规律排列的 UI 元素,如

    2024年02月04日
    浏览(6)
  • draggable + grid 拖拽插件 + 网格布局 动态生成首页模版

    draggable + grid 拖拽插件 + 网格布局 动态生成首页模版

    背景:         1、首页模板由多个子组件组成,如图表、新闻、公告、轮播图等,一般都由前端引入子组件,写在固定的位置上,最终形成一个固定的首页模板;         2、像这样直接在代码中写死的首页没有灵活性,不同用户想展示出来的首页模板千篇一律;        

    2024年02月01日
    浏览(17)
  • Vue Grid Layout -️ 适用Vue.js的栅格布局系统(保姆级使用教程)

    Vue Grid Layout -️ 适用Vue.js的栅格布局系统(保姆级使用教程)

    官网:Vue Grid Layout -️ 适用Vue.js的栅格布局系统 Gitee:https://gitee.com/wfeng0/vue2-grid-layout  在官网的描述中,我们可以看出,该栅格布局具有以下特性:  在具有 拖拽组成页面、组件动态调整大小、边缘碰撞监测 的系统中,使用该布局无疑是最合适的。当然,目前也有很多现成

    2023年04月16日
    浏览(13)
  • HarmonyOS应用开发学习笔记 UI布局学习 栅格布局(GridRow/GridCol)

    HarmonyOS应用开发学习笔记 UI布局学习 栅格布局(GridRow/GridCol)

    HarmonyOS应用开发学习笔记 UI布局学习 相对布局 (RelativeContainer) 官方文档:栅格布局(GridRow/GridCol) 通过设置GridRow的direction属性来指定栅格子组件在栅格容器中的排列方向 代码 描述 GridRowDirection.Row 从左往右排列 GridRowDirection.RowReverse 从右往左排列 左往右排列 子组件从右

    2024年02月03日
    浏览(14)
  • HTML5+CSS3学习笔记(九)前端页面六大布局(文档流布局、浮动布局、定位布局、表格布局、弹性布局、网格布局)

    HTML5+CSS3学习笔记(九)前端页面六大布局(文档流布局、浮动布局、定位布局、表格布局、弹性布局、网格布局)

    本系列更多文章,可以查看专栏 HTML+CSS学习笔记 块级元素自上至下垂直排列,行内元素自左至右水平排列 块级元素独占一行,行内元素不会另起一行 默认情况下,height和width决定内容区的大小;内容区、内边距和边框构成可见区域的大小;外边距决定元素的位置 更多内容可

    2024年02月02日
    浏览(21)
  • 突破经典网格特征?AutoFocusFormer: Image Segmentation off the Grid 论文阅读笔记

    突破经典网格特征?AutoFocusFormer: Image Segmentation off the Grid 论文阅读笔记

    写在前面   这一周赶上五一五天假了,朋友们出去 happy 了吗?有没有赶上人山人海的热闹?反正我只是在 5.1 那天出去走走,哈哈。   这是一篇关于实例分割的文章,所解决的问题在于实例分割中需要的小目标像素分辨率太低,于是本文提出一种自适应下采样的方法来

    2024年02月06日
    浏览(37)
  • Flutter学习之旅 -网格布局

    Flutter学习之旅 -网格布局

    GridView列表三种形式 可以通过 GridView.count 实现网格布局 可以通过 GridView.extent 实现网格布局 可以通过 GridView.builder 实现网格布局 参数:可以通过 SliveGridDelegateWithFiexdCrossAxisCount 来设置 GridView.count 参数: 可以通过 SliveGridDelegateWithMaxCrossAxisExtent 来设置 GridView.extent 常用属性 名称

    2024年02月03日
    浏览(5)
  • 【Android笔记97】Android之RecyclerView使用GridLayoutManager网格布局

    这篇文章,主要介绍Android之RecyclerView使用GridLayoutManager网格布局。 目录 一、GridLayoutManager网格布局 1.1、功能效果 1.2、案例代码 (1)创建网格布局

    2024年02月15日
    浏览(10)
  • 鸿蒙开发-UI-布局-栅格布局

    鸿蒙开发-UI-布局-栅格布局

    鸿蒙开发-UI-布局 鸿蒙开发-UI-布局-线性布局 鸿蒙开发-UI-布局-层叠布局 鸿蒙开发-UI-布局-弹性布局 鸿蒙开发-UI-布局-相对布局 文章目录 前言 一、基本概念 二、格栅容器组件 1.栅格系统断点 2.布局的总列数 3.排列方向 4.子组件间距 三、格栅容器子组件 1.span 2.offset 3.order 四、

    2024年02月20日
    浏览(8)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包