
- 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
廣告