CSS3中的display:grid布局怎样实现,效果是怎样的
Admin 2022-06-10 群英技术资讯 634 次浏览
1.网格布局(grid):
它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局;
2.基本概念:
容器和项目,如图所示:
<div class="content"> <div class="b">1</div> <div class="b">2</div> <div class="b">3</div> <div class="b">4</div> <div class="b">5</div> <div class="b">6</div> <div class="b">7</div> <div class="b">8</div> <div class="b">9</div> </div>
.content即为容器,.b即为项目。
行和列: 行:row;
列:column;
3.容器属性
display:grid; //默认是块元素;
display:inline-grid; //行内块元素
指定一个容器采用网格布局;
注意:设置为grid后,子元素的float,display: inline-block,display: table-cell、vertical-align和column-*等设置都将失效。
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; }
4.属性解释
grid-template-columns:定义每一列的列宽;
grid-template-columns:100px 100px 100px; //总共三列,每列列宽是100px;=
grid-template-rows:定义每一行的行高;
grid-template-rows:100px 100px 100px; //从上至下,每行高度为100px;
//除了使用像素,还可以使用百分比;
拓展:
重复写值很麻烦,可以用repeate函数;
repeat(次数,大小);
例如:repeat(3,100px); //重复3次,每次100px;
repeat写法:
grid-template-columns:repeat(3,100px);
grid-template-rows:repeat(3,100px);
也可以重复某一种不固定大小模式;
例如:
grid-template-columns:100px 80px 100px;
改写成:
grid-template-columns:repeat(2,100px 80px); //代表重复2次100px 80px的模式;也就是4列;
等同于:
grid-template-columns:100px 80px 100px 80px;
如图:
5.关键字
1,auto-fill;如果容器大小不固定,项目大小固定,可以用auto-fill关键字自动填充;
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: repeat(auto-fill, 100px); }
2,fr(fraction):如果两列的宽度分别为1fr和2fr,就表示后者是前者的两倍。
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 1fr 2fr; grid-template-rows: repeat(3, 100px 80px); }
fr也可以和px配合使用;
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 400px 1fr 2fr; }
3.minmax();
grid-template-columns: 1fr 5fr minmax(100px, 1fr);
解释:第一列是1fr,第二列是5fr,第三列最小值是100px,最大是1fr。当第二列fr无限大时和第三列到100px时,会往第一列借值;
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 1fr 5fr minmax(100px, 1fr); }
4.auto:自适应;
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 100px auto 100px; }
6.网格线名称:
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: [c1] 100px [c2] 100px [c3] auto [c4]; grid-template-rows: [r1] 100px [r2] 100px [r3] auto [r4]; }
解释:指定每一根网格线的名字,方便以后的引用。
也可以有多个名字;[c1,c1a]
7.间距
row-gap:行间距;
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; row-gap: 20px; }
colum-gap:列间距;
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; column-gap: 20px; }
简写:
gap:20px 20px;
row-gap和column-gap简写形式;
gap省略了第二个值,浏览器认为第二个值等于第一个值。
8.区域
grid-template-areas:网格布局允许指定"区域"(area),一个区域由单个或多个单元格组成。grid-template-areas属性用于定义区域。
.content { box-shadow: 0 0 1px #f6f; display: grid; grid-template-areas: 'a b c' 'd e f' 'g h i'; }
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
浏览器滚动条太宽,太丑,影响日常开发怎么办,本文介绍了如何使用CSS3改变浏览器滚动条样式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
使用 CSS 还是可以作出类似极光的一些特效的,今天我们就一起来尝试下。
CSS中的星号表示什么?有一些新手在学习CSS时,不是很了解CSS样式中的星号代表什么,有什么作用和用法。对此这篇就给大家来简单的介绍一下,对新手入门学习会有帮助,感兴趣的朋友就继续往下看吧。
本篇文章带大家深入了解CSS中一个非常有意思的属性 mask,希望对大家有所帮助!
方法:1、在文本元素中添加具有“display:inline-block;width:100%”样式的i标签;2、利用“text-indent”属性让插入i标签的文本元素两端对齐,语法为“文本元素{text-align:justify}”。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008