css中grid属性的使用
grid布局
加在父元素上的属性
grid-template-columns/grid-template-rows
- 定义元素的行或列的宽高
- 如果父元素被等分成了9等分,则,不管有多少个子元素,都显示9等分
- grid-template-columns: 33% 33% 33%;可以写成grid-template-columns:repeat(3, 33%);
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container1 {
grid-template-columns: 33% 33% 33%;
grid-template-rows: 33% 33% 33%;
}
<div class="container container1">
<div class="item item-1"></div>
<div class="item item-2"></div>
</div>
grid-template-areas
- 父元素的grid-template-areas配合子元素的grid-area定义网格区域
- 一个句点表示一个空的网格单元
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container2 {
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "header header . footer"
"main main . sidebar"
"main main . sidebar";
}
.container2 .item-1 {
grid-area: header;
}
.container2 .item-2 {
grid-area: main;
}
.container2 .item-3 {
grid-area: sidebar;
}
.container2 .item-4 {
grid-area: footer;
}
<div class="container container2">
<div class="item item-1">header</div>
<div class="item item-2">main</div>
<div class="item item-3">sidebar</div>
<div class="item item-4">footer</div>
</div>
grid-column-gap/grid-row-gap/grip-gap
- 指网格线的大小,也可以说是网格子项之间的间距
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container3 {
grid-template-columns: repeat(3, 30%);
grid-template-rows: repeat(3, 30%);
grid-column-gap: 2%;
grid-row-gap: 2%;
}
<div class="container container3">
<div class="item item-1"></div>
<div class="item item-2"></div>
<div class="item item-3"></div>
<div class="item item-4"></div>
<div class="item item-5"></div>
<div class="item item-6"></div>
<div class="item item-7"></div>
<div class="item item-8"></div>
<div class="item item-9"></div>
</div>
justify-items/align-items
- justify-items让子元素的内容和纵向列轴对齐
- align-items让子元素的内容和横向行轴对齐
- 两个属性都有四个值
- 我的例子中,为了方便对比,嵌套了grid网格
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container4 {
width: 400px;
height: 200px;
grid-template-columns: repeat(4, 25%);
grid-template-rows: repeat(2, 50%);
}
.container4 .item {
display: grid;
}
.container4 .item div {
background-color: coral;
border: 1px dashed #aaa
}
.container4 .item-1 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
justify-items: start;
}
.container4 .item-2 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
justify-items: end;
}
.container4 .item-3 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
justify-items: center;
}
.container4 .item-4 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
justify-items: stretch;
}
.container4 .item-5 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
align-items: start;
}
.container4 .item-6 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
align-items: end;
}
.container4 .item-7 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
align-items: center;
}
.container4 .item-8 {
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
align-items: stretch;
}
<div class="container container4">
<div class="item item-1">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-2">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-3">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-4">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-5">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-6">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-7">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-8">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
justify-content/align-content
- 设置子元素的对齐方式,justify表示纵向,align表示横向
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container5 {
width: 700px;
height: 200px;
grid-template-columns: repeat(7, 14%);
grid-template-rows: repeat(2, 50%);
}
.container5 .item {
display: grid;
}
.container5 .item div {
background-color: coral;
border: 1px dashed #aaa
}
.container5 .item-1 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: start;
}
.container5 .item-2 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: end;
}
.container5 .item-3 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: center;
}
.container5 .item-4 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: stretch;
}
.container5 .item-5 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: space-around;
}
.container5 .item-6 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: space-between;
}
.container5 .item-7 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
justify-content: space-evenly;
}
.container5 .item-8 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: start;
}
.container5 .item-9 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: end;
}
.container5 .item-10 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: center;
}
.container5 .item-11 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: stretch;
}
.container5 .item-12 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: space-around;
}
.container5 .item-13 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: space-between;
}
.container5 .item-14 {
grid-template-columns: repeat(2, 40%);
grid-template-rows: repeat(2, 40%);
align-content: space-evenly;
}
<div class="container container5">
<div class="item item-1">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-2">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-3">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-4">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-5">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-6">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-7">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-8">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-9">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-10">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-11">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-12">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-13">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
<div class="item item-14">
<div>list</div>
<div>list</div>
<div>list</div>
<div>list</div>
</div>
</div>
grid-auto-columns/grid-auto-rows
- grid-column后面的值如果是1 / 2形式,表示的是从第1列网格线开始到第2列网格线结束,如果这个属性定义的网格超出了父元素的范围,则会自动生成隐式网格
- grid-auto-columns和grid-auto-rows两个属性来指定这些隐式网格轨迹的宽度
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container6 {
width: 120px;
height: 180px;
grid-template-columns: 60px 60px;
grid-template-rows: 90px 90px;
grid-auto-columns: 60px;
}
.container6 .item-1 {
grid-column: 1 / 2;
grid-row: 2 / 3;
border: 1px solid #ccc;
}
.container6 .item-2 {
grid-column: 5 / 6;
grid-row: 2 / 3;
border: 1px solid #ccc;
}
<div class="container container6">
<div class="item-1">1/2&2/3</div>
<div class="item-2">5/6&2/3</div>
</div>
grid-auto-flow
- 没有网格元素的时候,自动排列
- row表示从左到右排列,column表示从上到下排列
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container7 {
display: grid;
width: 200px;
height: 40px;
grid-template-columns: 40px 40px 40px 40px 40px;
grid-template-rows: 40px 40px;
/* grid-auto-flow: row; */
grid-auto-flow: column;
}
.container7 .item-1 {
grid-column: 1;
grid-row: 1 / 3;
}
.container7 .item-5 {
grid-column: 5;
grid-row: 1 / 3;
}
<div class="container container7">
<div class="item item-1">1</div>
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
<div class="item item-5">5</div>
</div>
加在子元素上的属性
grid-column-start/grid-column-end/grid-row-start/grid-row-end/grid-column/grid-row
- 定义网格开始或者结束的位置
- 值为数字,表示从这条线开始。值为span加数字,表示覆盖了这条线的位置
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container8 {
margin-top: 20px;
grid-template-columns: repeat(5, 20%);
grid-template-rows: repeat(5, 20%);
}
.container8 .item-1 {
grid-column-start: 2;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 2;
}
.container8 .item-2 {
grid-column-start: 4;
grid-column-end: span 5;
grid-row-start: 2;
grid-row-end: span 5;
}
.container8 .item-3 {
grid-column: 1 / span 2;
grid-row: 2 / span 4;
}
<div class="container container8">
<div class="item item-1">item-1</div>
<div class="item item-2">item-2</div>
<div class="item item-3">item-3</div>
</div>
justify-self/align-self
- 网格子项内容与格线对齐
.container {
width: 200px;
height: 200px;
display: grid;
background-color: coral;
margin: 10px;
}
.container .item {
border: 1px solid #ccc;
background-color: chocolate;
}
.container9 {
width: 400px;
height: 200px;
grid-template-columns: repeat(4, 25%);
grid-template-rows: repeat(2, 50%);
}
.container9 .item-1 {
justify-self: start;
}
.container9 .item-2 {
justify-self: end;
}
.container9 .item-3 {
justify-self: center;
}
.container9 .item-4 {
justify-self: stretch;
}
.container9 .item-5 {
align-self: start;
}
.container9 .item-6 {
align-self: end;
}
.container9 .item-7 {
align-self: center;
}
.container9 .item-8 {
align-self: stretch;
}
<div class="container container9">
<div class="item item-1">item-1</div>
<div class="item item-2">item-2</div>
<div class="item item-3">item-3</div>
<div class="item item-4">item-4</div>
<div class="item item-5">item-5</div>
<div class="item item-6">item-6</div>
<div class="item item-7">item-7</div>
<div class="item item-8">item-8</div>
</div>
参考借鉴地址
css中grid属性的使用的更多相关文章
- 理解css中Grid布局,在项目中如何实现grid页面布局
简介 CSS中Grid是一种二维网格式布局方式.我们常规使用table.float.position.inline-block等布局,但它们遗漏了很多功能,例如垂直居中.后来css3中flexbox的 ...
- 深入理解css中position属性及z-index属性
深入理解css中position属性及z-index属性 在网页设计中,position属性的使用是非常重要的.有时如果不能认识清楚这个属性,将会给我们带来很多意想不到的困难. position属性共 ...
- CSS中Position属性
也许你看到这个标题觉得很简单,确实这是一篇关于CSS中Position属性基础知识的文章,但是关于Position的一些细节也许你不了解. 1.简介 position有五个属性: static | r ...
- CSS中 Zoom属性
CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...
- DIV css中cursor属性详解-鼠标移到图片变换鼠标形状 (转)
css中cursor属性详解-鼠标移到图片变换鼠标形状 语法: cursor : auto | all-scroll | col-resize| crosshair | default | han ...
- CSS中Position属性static、absolute、fixed、relative
在html中网页可以看成一个立体的空间,一个完整的页面是由很多个页面堆积形成的,如下图所示 CSS中Position属性有四个可选值,它们分别是:static.absolute.fixed.rel ...
- 深入理解css中position属性及z-index属性 https://www.cnblogs.com/zhuzhenwei918/p/6112034.html
深入理解css中position属性及z-index属性 请看出处:https://www.cnblogs.com/zhuzhenwei918/p/6112034.html 在网页设计中,positi ...
- CSS中box-sizing属性的理解与部分用法
今天看了一些关于box-sizing的一些资料,在这里整理一下,希望也能对大家有所帮助. box-sizing是CSS的一个属性,很好的解决了盒模型的相关问题.CSS中的盒模型(Box model)分 ...
- css中clip-path属性的运用
今天看到一位同学的需求,要在一个div中加一个小尖尖,对话时发的图片,旁边这个三角是怎么实现与图片的颜色一致,效果如下: 当然,解决这个问题有各种奇淫巧技,现在我们来看一个css属性clip-path ...
随机推荐
- Octave 常用命令
GNU Octave 官方文档 GNU Octave Documentation(Online) GNU Octave Documentation(PDF) 安装额外的包 Installing and ...
- 1)关于配置centos的网络问题
网址: http://jingyan.baidu.com/article/f3e34a12d886d2f5eb653515.html
- PAT甲级——1002 A+B for Polynomials
PATA1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomia ...
- “pip install tensorflow ”出现错误
在控制台命令窗口输入:pip install tensorflow之后出现一长串bug怎么解决 网上百度了一些方法: 安装Python3.5 安装Python3.6 总结原因:Python3.7没有合 ...
- [LC] 15. 3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- spring-boot jpa mysql emoji utfmb4 异常处理
spring-boot jpa mysql utf8mb4 emoji 写入失败 mysql database,table,column 默认为utf8mb4 Caused by: java.sql. ...
- E丢丢App重设计总结
E丢丢学习App是华夏大地教育可以有限公司旗下的一款产品,专为提升学历者打造,它整合了线上+跟踪的 (E平台)功能,方便工作人员随时随地管理账号.跟进学员:同时还可以随时了解教育行业的新闻资讯.一对一 ...
- MOOC(7)- case依赖、读取json配置文件进行多个接口请求-xlrd操作excel(11)
xlrd操作excel # -*- coding: utf-8 -*- # @Time : 2020/2/12 9:14 # @File : do_excel_xlrd_11.py # @Author ...
- hadoop创建两大错误:Bad connection to FS. command aborted. exception和Shutting down NameNode at hadoop
1.问题目录表: Error代码 failed on connection exception: java.net.ConnectException: Connection refused 3. ...
- 启动Tomcat报WEB-INF\lib\j2ee.jar jar not loaded异常的解决办法
今天加载工程时突然发现Tomcat报: 2010-7-1 12:11:38 org.apache.catalina.loader.WebappClassLoader validateJarFile 信 ...