- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 度量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪切
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - Important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2d 变换
- CSS - 3d 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - grid-template 属性
CSS grid-template 属性指定网格布局的网格列、网格行和网格区域。该属性是各个网格相关属性的简写:grid-template-areas、grid-template-columns 和 grid-template-rows
语法
grid-template: none | grid-template-rows / grid-template-columns | grid-template-areas | initial | inherit;
属性值
| 值 | 描述 |
|---|---|
| none | 未设置列和行的特定大小。默认值。 |
| grid-template-rows / grid-template-columns | 它指定行和列的大小。 |
| grid-template-area | 它使用命名网格项定义网格布局。 |
| initial | 这将属性设置为其默认值。 |
| inherit | 这从父元素继承属性。 |
CSS 网格模板属性示例
以下示例使用不同的值解释了 grid-template 属性。
使用 None 值的网格模板属性
为了不具有任何预定义的行、列或区域,我们使用 none 值,它表示未定义显式网格模板。网格将根据网格项的内容和布局自动调整。这在以下示例中显示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
background-color: lightgrey;
grid-gap: 10px;
padding: 20px;
grid-template: none;
}
.container>div {
padding: 10px;
text-align: center;
color: white;
border: 3px solid blue;
background-color: #4775d1;
}
</style>
</head>
<body>
<h2>
CSS grid-template property
</h2>
<h4>
grid-template: none
</h4>
<div class=" container container1">
<div>
Item-1
</div>
<div>
Item-2
</div>
<div>
Item-3
</div>
<div>
Item-4
</div>
</div>
</body>
</html>
用于行和列的网格模板属性
要定义网格布局中行和列的大小,我们将行的高度和列的宽度(用“/”分隔)指定给 grid-template 属性。“/”之前的指定大小数量表示行数,“/”之后的指定大小数量表示列数。这些在以下示例中显示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
height: 200px;
background-color: lightgrey;
grid-gap: 20px;
padding: 10px;
}
.container1 {
grid-template: 120px 60px / auto auto auto;
}
.container2 {
grid-template: 100px 60px / 160px 130px 160px;
}
.container>div {
padding: 10px;
text-align: center;
color: white;
border: 3px solid blue;
background-color: #4775d1;
}
</style>
</head>
<body>
<h2>
CSS grid-template property
</h2>
<p>
<strong>
grid-template: 120px 60px / auto
auto auto (2 rows and three columns)
</strong>
; the first row is 120px high and second row
is 60px high, the width of the columns has
been set to auto and thus depends on the
content.
</p>
<div class=" container container1">
<div>
Item-1
</div>
<div>
Item-2
</div>
<div>
Item-3
</div>
<div>
Item-4
</div>
<div>
Item-5
</div>
<div>
Item-6
</div>
</div>
<p>
<strong>
grid-template: 100px 60px / 160px
130px 160px (2 rows and three columns)
</strong>
; the first row is 100px high and second row
is 60px high, the first column is 160px wide,
second column is 130px wide, third column is
160px wide.
</p>
<div class="container container2">
<div>
Item-1
</div>
<div>
Item-2
</div>
<div>
Item-3
</div>
<div>
Item-4
</div>
<div>
Item-5
</div>
<div>
Item-6
</div>
</div>
</body>
</html>
使用命名网格项的网格模板属性
要定义网格容器内的命名网格区域,我们将使用单引号 '' 指定行,并在其中使用名称数量指定列到 grid-template 属性。元素需要使用单引号中的名称来引用布局的一部分。这在以下示例中显示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
background-color: lightgrey;
grid-gap: 10px;
padding: 20px;
}
.container>div {
padding: 20px;
text-align: center;
color: white;
border: 3px solid blue;
background-color: #4775d1;
}
.container1 {
grid-template:
'header header header header'
'sidebar content content content'
'footer footer footer footer';
}
.container2 {
grid-template:
'item item ..'
'item item..';
}
.cont1-item1 {
grid-area: header;
}
.cont1-item2 {
grid-area: sidebar;
}
.cont1-item3 {
grid-area: content;
}
.cont1-item4 {
grid-area: footer;
}
.cont2-item1 {
grid-area: item;
}
</style>
</head>
<body>
<h2>
CSS grid-template property
</h2>
<p>
<strong>
grid-template: 'header header header header'
'sidebar content content content'
'footer footer footer footer'
</strong>
; The layout has three rows indicated by three different
apostrophes' ' and four columns indicated by the number
of items inside apostrophes ' '
</p>
<div class=" container container1">
<div class="cont1-item1">
This is header
</div>
<div class="cont1-item2">
This is sidebar
</div>
<div class="cont1-item3">
This is content
</div>
<div class="cont1-item4">
This is footer
</div>
</div>
<p>
<strong>
grid-template: 'item item . .''item item . .'
</strong>
; item1 takes two rows indicated by two separate
apostrophes ' ' and 4 columns indicated by the
number of items inside apostrophes '
'</p>
<div class=" container container2">
<div class="cont2-item1">
Item-1
</div>
<div>
Item-2
</div>
<div>
Item-3
</div>
<div>
Item-4
</div>
<div>
Item-5
</div>
<div>
Item-6
</div>
</div>
</body>
</html>
支持的浏览器
| 属性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| grid-template | 57 | 16 | 52 | 10 | 44 |
css_properties_reference.htm
广告




