
- 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 - 全部
- CSS - 內嵌
- CSS - 隔離
- CSS - 溢位滾動
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大塊尺寸
- CSS - 最小塊尺寸
- CSS - 混合模式
- CSS - 最大內聯尺寸
- CSS - 最小內聯尺寸
- CSS - 偏移量
- CSS - 口音色
- CSS - 使用者選擇
- 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 - 二維變換
- CSS - 三維變換
- 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 屬性
CSS grid 屬性是一個簡寫屬性,用於在一個宣告中宣告所有顯式和隱式網格屬性。這是一種定義元素網格佈局的便捷且簡潔的方法。grid 屬性是以下各個網格相關屬性的簡寫:grid-template-rows,grid-template-columns,grid-template-areas,grid-auto-columns,grid-auto-flow 和 grid-auto-rows
語法
grid: none| grid-template-rows / grid-template-columns | grid-template-areas | grid-template-rows / [grid-auto-flow] grid-auto-columns | [grid-auto-flow] grid-auto-rows / grid-template-columns | initial | inherit;
屬性值
值 | 描述 |
---|---|
none | 表示不指定行或列的特定大小。預設值。 |
grid-template-rows / grid-template-columns | 指定行數及其高度和列數及其寬度。 |
grid-template-areas | 使用名稱指定網格佈局。 |
grid-template-rows / grid-auto-columns | 指定行數及其高度和列的自動大小。 |
grid-auto-rows / grid-template-columns | 指定行的自動大小和列數及其寬度。 |
grid-template-rows / grid-auto-flow grid-auto-columns | 指定行數及其高度,自動放置專案的position和列的自動大小。 |
grid-auto-flow grid-auto-rows / grid-template-columns | 指定如何放置自動放置的專案,行的自動大小,以及列數及其寬度。 |
initial | 將屬性設定為其初始值。 |
inherit | 從父元素繼承屬性。 |
CSS Grid 屬性示例
以下示例解釋了具有不同值的grid 屬性。
具有行高和列寬的 Grid 屬性
要在網格佈局中定義具有其大小的行和列,我們為行指定高度值,為列指定寬度值,並用 `/` 分隔(例如,10px 10px / 20px 20px 指定 2 行 10px 高度和 2 列 20px 寬度)到grid 屬性。如下例所示。
示例
<!DOCTYPE html> <html> <head> <style> .container { display: grid; grid: 150px 100px/ auto auto auto; gap: 10px; background-color: lightblue; } .container>div { border: 2px solid gray; color: white; text-align: center; padding: 30px 0px; margin: 10px; background-color: lightcoral; } </style> </head> <body> <h2> CSS Grid Property </h2> <p> <strong> grid: 150px 100px / auto auto auto </strong>; There are two rows: first row's height is 150px, second row's height is 100px. There are three columns: all colums have auto width, meaning they will be adjusted according to their content. </p> <div class="container"> <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 屬性
為了定義網格佈局某些部分的大小,以便元素可以放置在這些部分中,我們為這些部分提供名稱及其大小(例如,“header header content content”表示 2 行和 2 列)到grid 屬性。如下例所示。
示例
<!DOCTYPE html> <html> <head> <style> .container { display: grid; grid: "header header header header" 100px "sidebar content content content" 100px "footer footer footer footer" 100px; padding: 10px; gap: 20px; background-color: lightblue; } .item1 { grid-area: header; } .item2 { grid-area: sidebar; } .item3 { grid-area: content; } .item4 { grid-area: footer; } .container>div { background-color: lightcoral; text-align: center; color: white; padding: 20px; } </style> </head> <body> <h2> CSS Grid Property </h2> <p> <strong> grid: "header header header header 100px sidebar content content content 100px footer footer footer footer 100px" </strong>; There are three rows and four columns. The header is first row and takes four columns, sidebar and content are second row and take single column and 3 columns respectively and footer is third column and takes all four columns. All rows have 100px height. </p> <div class="container"> <div class="item1"> This is header </div> <div class="item2"> This is sidebar </div> <div class="item3"> This is content </div> <div class="item4"> This is footer </div> </div> </body> </html>
支援的瀏覽器
屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
grid | 57 | 16 | 52 | 10 | 44 |
css_properties_reference.htm
廣告