CSS Grid 布局(Grid Layout)完全指南 #flight.Archives003
Title/ CSS Grid 布局(Grid Layout)完全指南 #flight.Archives003
序 :
写完这篇文章后,我准备一直做下去了,包括flight的各个分区,也看到前方的路.
我要做最简约高效的前端教程 //表达最张狂的性格简介(from Ruanyf) :
2017三月,主流浏览器更新了对Grid(网格布局)的支持.
这是最强大的 CSS 布局方案.
它将网页划分成一个个网格,做出各种各样的布局. 以前,只能通过复杂的 CSS 框架达到的效果,现在浏览器内置了.
Tag/Grid布局介绍
.container{
display:grid; /*or inline-grid*/
}
这段代码定义了.container元素为Grid容器,.container的直接子元素为Grid项目
其中网格的分界线称作Grid Line,两条相邻网格线之间的间隔称作Grid Track.
单个格子称作Grid Cell,多条网格线包围的区块称作Grid Area
Tag/Grid容器(Grid Container)属性
| 属性 | 可取值 | 作用 |
|---|---|---|
display |
grid, inline-grid |
指定一个块状/行内Grid容器 |
grid-template-columns, grid-template-rows |
多个值, [line-name] <track-size>...(网格线的名称,间隔的宽度) |
指定每个行/列的名称和大小, 详见Details |
grid-template-areas |
多个值, <grid-area-name>(区块的名称) 或 .(空的区块) |
指定每一个区块的名称, 详见Details |
grid-template |
<grid-template-rows> <grid-template-columns> |
对于 <grid-template-rows>, <grid-template-columns> 和 <grid-template-areas>的CSS简写属性 |
column-gap(前grid-column-gap),row-gap(前grid-row-gap) |
<line-size>(网格线的宽度) |
指定行/列的间距 |
gap(前grid-gap) |
<grid-row-gap> <grid-column-gap> |
对于 row-gap和 column-gap的CSS简写属性 |
align-items, justify-items |
start,end,center,stretch(默认值) |
align-items指定项目的行对齐方式, justify-items指定项目的列对齐方式 |
place-items |
<align-items> <justify-items> |
CSS简写属性, 如果只有一个值则同时指定两个属性 |
justify-content,align-content |
start, end, center, stretch, space-around, space-between, space-evenly |
justify-content指定项目在容器中的水平位置, align-content指定项目在容器中的垂直位置 |
place-content |
<align-content> <justify-content> |
CSS简写属性, 如果只有一个值则同时指定两个属性 |
grid-auto-columns, grid-auto-rows |
多个值, <track-size> |
指定在有多余区块时浏览器自动创建的多余网格的列宽和行高 |
grid-auto-flow |
row, column, row dense, column dense |
指定项目的放置顺序,默认是 row 即"先行后列" |
grid |
<grid-template>, <'grid-template-rows'> [ auto-flow && dense? ] <'grid-auto-columns'>, [ auto-flow && dense? ] <'grid-auto-rows'>? <'grid-template-columns'> |
grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns 和 grid-auto-flow的CSS简写属性, 详见Details |
Tag/Grid项目(Grid item)属性
| 属性 | 可取值 | 作用 |
|---|---|---|
grid-column-start, grid-column-end, grid-row-start, grid-row-end |
<line>, span <number>, span <name> |
指定项目所占的Grid Area, 详见Details |
grid-column, grid-row |
<start-line> / <end-line> |
grid-column是 grid-column-start和 grid-column-end的CSS简写属性, grid-row是 grid-row-start和 grid-row-end的CSS简写属性 |
grid-area |
<name>, <row-start> / <column-start> / <row-end> / <column-end> |
指定项目所在区域 |
justify-self,align-self |
start,end,center,stretch |
justify-self指定项目在区块中的水平位置, align-self指定项目在区块中的垂直位置, |
place-self |
auto(默认值), <align-self> <justify-self> |
<align-self>, <justify-self> 的CSS简写属性, 如果只有一个值则同时指定两个属性 |
->> Details
容器属性 - grid-template-columns, grid-template-rows
可取值:
grid-template-columns: 100px 1fr; /*区块的大小*/
grid-template-columns: [linename] 100px; /*方括号内可以定义网格线的名称, 方便以后引用*/
grid-template-columns: [linenameA linenameB] 100px; /*一条线可以有多个名字*/
grid-template-columns: [linenameA linenameB]; /*可以只定义名称,不定义大小*/
特殊内容介绍:
repeat() 函数
CSS函数 repeat(times, value) 可以简化重复值输入的繁琐
.container {
display: grid;
grid-template-columns: repeat(4, 25%); /*等同于 grid-template-columns: 25% 25% 25% 25%;*/
grid-template-rows: repeat(2, 50px 100px 80px); /*等同于 grid-template-columns: 50px 100px 80px 50px 100px 80px;*/
}
特殊说明:
times参数接受auto-fill和auto-fit关键字, 适用于容器大小不固定的情况grid-template-columns: repeat(auto-fill, 100px) /*在一行内不断放置100px的项目直到填满该行*/
auto-fill表示自动判断重复次数以在每一行/列填充尽可能多的项目auto-fit表示自动判断重复次数以在每一行/列填充尽可能多的空间
在含有minmax()函数等可变因素的情况下, 二者可能会产生不同表现
二者对比: https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/
Demo: https://codepen.io/flightmakers/pen/GRmLKyZ?editors=0100
长度单位
fr
"fr" 是 "fraction"("片段") 的缩写, 可以用于指定宽度比例.container {
display:grid;
grid-template-columns: 200px 1fr 2fr;
}
fr单位可以和绝对单位一起使用, 上面代码指定了第一行200px, 第二行宽度是第三行的一半用于控制大小的关键字
min-content分配最小宽度max-content分配最大宽度fit-content分配的宽度在min-content和max-content之间auto自动分配宽度
用于控制大小的函数
minmax(min,max)接受两个参数, 表示一个长度范围, 表示宽度不小于min不超过max.
如果只定义其一, 可以使用min()和max()函数
容器属性 - grid-template-areas
可取值:
/*<string>+*/
grid-template-areas:
"a a a"
"b c c"; /*划分出6个区块, 然后将其命名为 `a`, `b` 和 `c` 的3个区域*/
grid-template-areas:
"a a ."
"b c c"; /* . 可以将某个区块指定为空*/
项目属性 - grid
可取值:
/*<'grid-template'> (grid-template-columns 和 grid-template-rows) */
grid: "a" 100px "b" 1fr;
grid: [linename1] "a" 100px [linename2];
grid: "a" 200px "b" min-content;
grid: "a" minmax(100px, max-content) "b" 20%;
/* <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'> */
grid: 200px / auto-flow;
grid: 30% / auto-flow dense;
grid: repeat(3, [line1 line2 line3] 200px) / auto-flow 300px;
grid: [line1] minmax(20em, max-content) / auto-flow dense 40%;
/* [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>*/
grid: auto-flow / 200px;
grid: auto-flow dense / 30%;
grid: auto-flow 300px / repeat(3, [line1 line2 line3] 200px);
grid: auto-flow dense 40% / [line1] minmax(20em, max-content);
这个属性对代码的易读性存在负面影响QwQ...
->> Reference link
MDN中文文档 https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Grid_Layout
MDN 英文文档 https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout
CodingStartUp https://www.bilibili.com/video/BV1XE41177oN?from=search&seid=7045917601727025410
CSS-Tricks https://css-tricks.com/snippets/css/complete-guide-grid/
关于auto-fill和auto-fit属性的对比 https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/Scotch.io#1 https://scotch.io/tutorials/deep-dive-into-css-grid-2
Scotch.io#2 https://scotch.io/tutorials/getting-started-with-css-grid-layout
->> Version History
现在版本为V1.0
详见 Github(@flightmakers)2021.8.13 在jj发布V1.0
2021.8.14 奋(mo)战(yu)了两天!!! 这篇文章终于发布了QwQ!!!
CSS Grid 布局(Grid Layout)完全指南 #flight.Archives003的更多相关文章
- 全新的css网站布局--Grid布局
Grid布局全新的css网站布局 CSS Grid 布局由两个核心组成部分是 wrapper(父元素)和 items(子元素). wrapper 是实际的 grid(网格),items 是 grid( ...
- CSS Grid布局入门
相信大家都比较熟悉flex布局了,最近有空研究了波grid布局,感觉虽然兼容性还不是太高,应用不是太普遍,但是功能非常强大.未来应该是grid+flex为主流,grid是二维布局,很灵活,适合整体构架 ...
- CSS Grid布局指南
简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,主要目的是改变我们基于网格设计的用户接口方式.如我们所知,CSS 总是用于网页的样式设置,但它并没有起到 ...
- CSS Grid 布局完全指南(图解 Grid 详细教程)
CSS Grid 布局是 CSS 中最强大的布局系统.与 flexbox 的一维布局系统不同,CSS Grid 布局是一个二维布局系统,也就意味着它可以同时处理列和行.通过将 CSS 规则应用于 父元 ...
- css:display:grid布局
简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,主要目的是改变我们基于网格设计的用户接口方式.如我们所知,CSS 总是用于网页的样式设置,但它并没有起到 ...
- 快速使用CSS Grid布局,实现响应式设计
常用Grid布局属性介绍 下面从一个简单Grid布局例子说起. CSS Grid 布局由两个核心组成部分是 wrapper(父元素)和 items(子元素). wrapper 是实际的 grid(网格 ...
- Grid布局指南
简介 CSS网格布局(又称“网格”),是一种二维网格布局系统.CSS在处理网页布局方面一直做的不是很好.一开始我们用的是table(表格)布局,然后用float(浮动),position(定位)和in ...
- CSS Grid 布局
CSS Grid 布局是 CSS 中最强大的布局系统.与 flexbox 的一维布局系统不同,CSS Grid 布局是一个二维布局系统,也就意味着它可以同时处理列和行.通过将 CSS 规则应用于 父元 ...
- css 中的grid布局基础
CSS Grid Layout为CSS引入了一个二维网格系统.网格可用于布局主要页面区域或小型用户界面元素. 网格是一组交叉的水平和垂直线 - 一组定义列,其他行.元素可以放在网格上,以行或者列为标准 ...
随机推荐
- 一、.Net Core 依赖注入详解及Autofac使用
.NET中的依赖注入实际上帮助我们解耦了我们的代码,是控制反转和依赖反转原则的具体实现. .Net Core的依赖注入的好处: 1. application 更稳定,容易维护和演化: 2. 实现细节的 ...
- Flex中利用事件机制进行主程序与子窗体间参数传递
在开发具有子窗体,或者itemrenderer的应用时,常常涉及到子窗体向父窗体传递参数或者从itemrenderer内的控件向外部的主程序传递参数的需求.这些都可以通过事件机制这一统一方法加以解决. ...
- 关于TreeView的实例
前台代码 (只需要有TreeView控件, 添加ID,其他默认生成) <form id="form1" runat="server"> <di ...
- POJ 3026 Borg Maze 广搜(BFS)+最小生成树
题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...
- C. Learning Languages 求联通块的个数
C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g ...
- 在docker中使用nginx部署前端项目
前言 部署了三个nginx用于前端项目, 并使用keepalived部署好热备, 所以总共有5个nginx 创建好nginx的文件和配置 根据上面的指令创建好目录 mkdir /home/web/ng ...
- Windows 上连接蓝牙耳机
"开始"菜单 –> 输入蓝牙 点击蓝牙设备,选择连接设备即可.
- linux学习之路第三天(vim和vi使用)
vi和vim编辑器 vi和vim的三种常见模式 1.正常模式 在正常模式下,我们可以使用快捷键 以vim打开一个档案就直接进入一般模式了(这是默认的模式).在这个模式中,你可以使用 上下左右按键来移动 ...
- 两个字符串,s1 包含 s2,包含多次,返回每一个匹配到的索引---python
#两个字符串,s1 包含 s2,包含多次,返回每一个匹配到的索引 def findSubIndex(str1,subStr): str_len = len(str1) sub_len = len(su ...
- DIY一个智能开关kwswitch
源码地址:https://gitee.com/kerwincui/kwswitch 平台简介 该智能开关平台包括服务端.硬件端.PC端和安卓端.硬件使用ESP8266模块,成本相对较低,可以发挥想象力 ...