CSS 网格布局(Grid)能够将网页分成具有简单属性的行和列,可以直接使用 CSS 来定位和调整网格内的每个元素,也不需要为了实现某种布局进行多层嵌套,总而言之,css网格布局非常好用,下面我们就来看一看这篇文章给大家讲述的css网格布局的内容。
1、CSS 网格布局(Grid)
CSS Grid 布局由两个核心组成部分是父元素和子元素,父元素 是实际的 grid(网格),子元素是 grid(网格) 内的内容。
下面是一个父元素和六个子元素
47eabecc5a4cd172f9cb299731b3ed23 181adfc339d83b58b49135c0bbc46a71116b28748ea4df4d9c2150843fecfba68 da23579204dc6cdba6efd2ccedebf62f216b28748ea4df4d9c2150843fecfba68 a55f0ac2ef5d4597a2929ed69514cdd0316b28748ea4df4d9c2150843fecfba68 bd0dee8fae24d19c962e4f2f9671ebc8416b28748ea4df4d9c2150843fecfba68 214d8ebcf64bc4e125b30ecf124e8266516b28748ea4df4d9c2150843fecfba68 a0d450056a6c645a4453d467ea30acf7616b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68
要把父元素元素变成一个 grid(网格),只要简单地把其 display 属性设置为 grid
效果图:
下面进行网格布局:
.box { width: 350px; height: 350px; /* background: #ccc; */ margin: 0 auto; grid-gap: 5px; display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { border: 1px solid black; box-sizing: border-box; } .div1 { grid-column-start: 1; grid-column-end: 3; /*(div1从占据从第一条网格线开始,到第三条网格线结束)*/ line-height: 100px; text-align: center; background: rgb(252, 0, 0); /* grid-column: 1/3;(这是缩写的形式) */ } .div2 { line-height: 100px; text-align: center; background: rgb(252, 134, 0); } .div3 { grid-row-start: 2; grid-row-end: 4; /* grid-row: 2/4;(这是缩写的形式) */ line-height: 200px; text-align: center; background: rgb(21, 207, 108); } .div4 { grid-column-start: 2; grid-column-end: 4; line-height: 100px; text-align: center; background: rgb(18, 21, 189); /* grid-column: 2/4;(这是缩写的形式) */ } .div5 { line-height: 100px; text-align: center; background: rgb(16, 170, 197); } .div6 { line-height: 100px; text-align: center; background: rgb(172, 126, 199); }
上面代码中的网格线(如图箭头所指的地方就是一根网格线):
2、响应式网格布局
和上面的差不多(添加了以下的内容)
使用grid-template-columns 属性创建一个 12 列的网格,每个列都是一个单位宽度(总宽度的 1/12 )
使用 grid-template-rows 属性创建 3 行
使用 grid-gap 属性在网格中的网格项之间添加一个间隙。
代码如下:
<div class="container"> <div class="header">顶部(一个点表示一个空白的格子),所以距离左边和右边各有一个格子的距离。</div> <div class="menu">中间1</div> <div class="content">中间2所以可以利用空白的格子来对你所要拍的网页来进行布局</div> <div class="footer">底部(一个点表示一个空白的格子),所以距离右边有三个格子的距离。</div> </div>
添加 grid-template-areas
这个属性被称为网格区域,也叫模板区域,能够让我们轻松地进行布局实验。
效果图:
代码如下:
.container { display: grid; grid-template-columns: repeat(12, 1fr); grid-template-rows: 50px 350px 50px; grid-gap: 5px; grid-template-areas: ". h h h h h h h h h h ." " m m c c c c c c c c c c" "f f f f f f f f f . . ."; } .container>p { border: 1px solid #ccc; box-sizing: border-box; } .header { text-align: center; line-height:50px; grid-area: h; color:rgb(219, 52, 169); } .menu { grid-area: m; text-align: center; line-height:350px; } .content { text-align: center; line-height:350px; grid-area: c; color:rgb(25, 158, 69); } .footer { color:rgb(212, 112, 18); text-align: center; line-height:50px; grid-area: f; }
相关文章推荐:
以上是CSS 网格布局(Grid)的两种方式介绍(附代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

我知道,我知道:有大量的内容管理系统选项可用,而我进行了几个测试,但实际上没有一个是一个,y&#039;知道吗?怪异的定价模型,艰难的自定义,有些甚至最终成为整个&

链接CSS文件到HTML可以通过在HTML的部分使用元素实现。1)使用标签链接本地CSS文件。2)多个CSS文件可通过添加多个标签实现。3)外部CSS文件使用绝对URL链接,如。4)确保正确使用文件路径和CSS文件加载顺序,优化性能可使用CSS预处理器合并文件。

选择Flexbox还是Grid取决于布局需求:1)Flexbox适用于一维布局,如导航栏;2)Grid适合二维布局,如杂志式布局。两者在项目中可结合使用,提升布局效果。

包含CSS文件的最佳方法是使用标签在HTML的部分引入外部CSS文件。1.使用标签引入外部CSS文件,如。2.对于小型调整,可以使用内联CSS,但应谨慎使用。3.大型项目可使用CSS预处理器如Sass或Less,通过@import导入其他CSS文件。4.为了性能,应合并CSS文件并使用CDN,同时使用工具如CSSNano进行压缩。

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重构自己的代码看起来是什么样的?约翰·瑞亚(John Rhea)挑选了他写的一个旧的CSS动画,并介绍了优化它的思维过程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版
视觉化网页开发工具

Atom编辑器mac版下载
最流行的的开源编辑器