Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
css Grid模板区域grid-template-areas使用方法_创想鸟

css Grid模板区域grid-template-areas使用方法

grid-template-areas通过命名区域定义网格布局,需在容器设置,用引号内名称对应子元素的grid-area,相同名称合并区域,配合grid-template-rows和columns设置尺寸,用点号表示空白,每行单词数须一致。

css grid模板区域grid-template-areas使用方法

CSS Grid 的 grid-template-areas 是一种直观且可读性强的布局方式,它允许你通过命名网格区域来定义页面结构。使用它可以轻松构建复杂的二维布局,比如页头、侧边栏、主内容区、页脚等。

基本语法与规则

grid-template-areas 需要在容器上设置,通过为每一行的网格单元格指定名称来划分布局。每个名称代表一个命名区域,相同的名称会合并成一个连续的区域。

示例:

.container {  display: grid;  grid-template-areas:    "header header"    "sidebar main"    "footer footer";}

上面代码定义了一个三行两列的网格:

第一行:左右两个单元格都叫 header,合并为一个横跨两列的头部区域 第二行:左侧是 sidebar,右侧是 main 第三行:两个单元格都是 footer,形成一个全宽页脚

为元素分配区域

使用 grid-area 属性将子元素与模板中定义的名称对应起来。

立即学习“前端免费学习笔记(深入)”;

HTML 示例:

Header
Main Content
Footer

CSS 设置:

header   { grid-area: header; }aside    { grid-area: sidebar; }main     { grid-area: main; }footer   { grid-area: footer; }

这样每个元素就会自动放入对应命名区域中。

博思AIPPT 博思AIPPT

博思AIPPT来了,海量PPT模板任选,零基础也能快速用AI制作PPT。

博思AIPPT 117 查看详情 博思AIPPT

控制行高与列宽

grid-template-areas 只定义结构,尺寸需要配合 grid-template-rowsgrid-template-columns 使用。

示例:

.container {  display: grid;  grid-template-areas:    "header header"    "sidebar main"    "footer footer";  grid-template-rows: 60px 1fr 40px;  grid-template-columns: 200px 1fr;}

行高分别为:60px(头部)、自适应高度(主体)、40px(页脚) 列宽为:200px 固定宽度(侧边栏),剩余空间由主内容区占据

使用点号留空区域

如果某个网格位置不需要内容,可以用点号 . 表示空白单元格。

示例:

.grid {  grid-template-areas:    "header  .       header"    "sidebar main    aside"    "footer  footer  footer";}

中间上方的位置被留空,不放置任何元素。

基本上就这些。合理使用 grid-template-areas 能让布局更清晰易维护,特别适合整体页面结构设计。注意每一行引号内的单词数量必须一致,否则会出错。

以上就是css Grid模板区域grid-template-areas使用方法的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/957443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
不止Mate 70华为全家桶新品将同台亮相:11月26日见
上一篇 2025年12月1日 18:27:19
电脑待机gpu负载跳动
下一篇 2025年12月1日 18:27:22

相关推荐

发表回复

登录后才能评论
关注微信