
- 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 索引
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪下
- CSS - 書寫模式
- CSS - Unicode 雙向
- CSS - min-content
- CSS - 全部
- CSS - 內嵌
- CSS - 隔離
- CSS - 滾動溢位
- CSS - Justify Items
- CSS - Justify Self
- CSS - 製表符大小
- 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 - 重要
- 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-auto-rows 屬性
CSS grid-auto-rows 屬性定義了隱式建立的網格行軌道或一組軌道的尺寸。當網格項放置在未透過 grid-template-rows 指定大小的行中時,網格系統會建立隱式網格軌道來容納它。如果使用 **grid-template-rows**,則此屬性無效。
語法
grid-auto-rows: auto | length | percentage | max-content | min-content | minmax(min, max)| fit-content();
屬性值
值 | 描述 |
---|---|
auto | 行的尺寸取決於容器的尺寸。預設值。 |
長度 | 使用長度單位設定行的尺寸。 |
百分比 | 使用百分比值設定行的尺寸。 |
max-content | 行的尺寸取決於內容的長度。不會發生換行。 |
min-content | 行的尺寸取決於內容的長度。會發生換行。 |
minmax(min, max) | 它指定了最小預設行大小和最大可達到的行大小。 |
fit-content() | 它指定了要顯示內容的尺寸範圍。可能會發生換行。 |
CSS 網格自動行屬性示例
以下示例說明了使用不同值的 **grid-auto-rows** 屬性。
使用 Auto 值的網格自動行屬性
為了允許網格項根據其內容和網格容器中的可用空間自行調整大小,我們使用 **auto** 值。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: auto; gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows property </h2> <h4> grid-auto-rows: auto </h4> <div class="grid-container"> <div class="grid-item item1"> Item-1 </div> <div class="grid-item item2"> Item-2 </div> <div class="grid-item item3"> Item-3 </div> <div class="grid-item item4"> Item-4 </div> <div class="grid-item item5"> Item-5 </div> </div> </body> </html>
使用長度值的網格自動行屬性
為了設定隱式行的尺寸,我們可以使用長度單位(例如 10px、20em 等)指定尺寸。因此,所有隱式建立的行都將具有指定的尺寸。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: 140px; gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows property </h2> <h4> grid-auto-rows: 140px (each row is 140px high) </h4> <div class="grid-container"> <div class="grid-item item1"> Item-1 </div> <div class="grid-item item2"> Item-2 </div> <div class="grid-item item3"> Item-3 </div> <div class="grid-item item4"> Item-4 </div> <div class="grid-item item5"> Item-5 </div> </div> </body> </html>
使用百分比值的網格自動行屬性
為了設定隱式行的尺寸,我們可以使用百分比值(例如 10%、20% 等)指定尺寸,這些值將尺寸分配為容器尺寸的百分比。因此,所有隱式建立的行都將具有指定的尺寸。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { height: 300px; background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: 40%; gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows property </h2> <h4> grid-auto-rows: 40% (each row has 40% of the container's height) </h4> <div class="grid-container"> <div class="grid-item item1"> Item-1 </div> <div class="grid-item item2"> Item-2 </div> <div class="grid-item item3"> Item-3 </div> <div class="grid-item item4"> Item-4 </div> <div class="grid-item item5"> Item-5 </div> </div> </body> </html>
使用 Max Content 值的網格自動行屬性
為了將行的高度設定為包含的最大內容的高度,我們使用 **max-content** 值。這意味著行將擴充套件以適應其最大的內容項,而不會有任何截斷或溢位。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: max-content; gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows property </h2> <h4> grid-auto-rows: max-content (the height of the items in each row is determined by the longest sentence height in the row) </h4> <div class="grid-container"> <div class="grid-item item1"> This is the first Item </div> <div class="grid-item item2"> This is the second box in the grid layout and is being made longer to demonstrate the effect. </div> <div class="grid-item item3"> This is third box in the layout. </div> <div class="grid-item item4"> This is fourth box in the layout. </div> <div class="grid-item item5"> This is the fifth box in the grid layout. </div> </div> </body> </html>
使用 Min Content 值的網格自動行屬性
為了將行高設定為適合內容且不會溢位的最小尺寸,我們使用 **min-content** 值。行將盡可能短,同時仍確保內容完全可見且不會被切斷。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: min-content; gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows Property </h2> <h4> grid-auto-rows: min-content (The rows will be sized to fit the minimum height required by their content.) </h4> <div class="grid-container"> <div class="grid-item item1"> This is first item. </div> <div class="grid-item item2"> This is Second item. </div> <div class="grid-item item3"> This is third item </div> <div class="grid-item item4"> This is fourth </div> <div class="grid-item item5"> This is fifth </div> </div> </body> </html>
使用 MinMax 函式的網格自動行屬性
為了定義網格項的尺寸範圍,我們使用 **minmax()** 函式來指定項的預設最小尺寸和最大可達到的尺寸(例如 minmax(50px,100px) 表示初始尺寸為 50px,但元素可以增長到 100px)。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: minmax(40px, 87px); gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows property </h2> <h4> grid-auto-rows: minmax(40px, 87px) (the initial height of the items is 40px and maximum attainable height is 87px) </h4> <div class="grid-container"> <div class="grid-item item1"> This is the first Item1 </div> <div class="grid-item item2"> This is the second box in the grid layout and is being made longer to demonstrate the effect. </div> <div class="grid-item item3"> This is third box in the layout. </div> <div class="grid-item item4"> This is fourth box in the layout. </div> <div class="grid-item item5"> This is the fifth box in the grid layout. </div> </div> </body> </html>
使用 Fit Content 函式的網格自動行屬性
為了設定適合其內容但位於指定最大尺寸內的行高(例如 fit-content(200px) 表示行將根據需要調整高度以適合內容,但不會超過 200 畫素)。這在以下示例中顯示。
示例
<!DOCTYPE html> <html> <head> <style> .grid-container { background-color: lightgray; padding: 10px; display: grid; grid-auto-rows: fit-content(50px); gap: 10px; } .grid-item { background-color: #336699; border: 3px solid blue; padding: 10px; text-align: center; color: white; } .item1 { grid-area: 1 / 1 / 2 / 2; } .item2 { grid-area: 1 / 2 / 2 / 3; } .item3 { grid-area: 1 / 3 / 2 / 4; } .item4 { grid-area: 2 / 1 / 3 / 2; } .item5 { grid-area: 2 / 2 / 3 / 3; } </style> </head> <body> <h2> CSS grid-auto-rows Property </h2> <h4> grid-auto-rows: fit-content (The rows will have a maximum height of 50px) </h4> <div class="grid-container"> <div class="grid-item item1"> This is first item. </div> <div class="grid-item item2"> This is Second item . </div> <div class="grid-item item3"> This is third item </div> <div class="grid-item item4"> This is fourth </div> <div class="grid-item item5"> This is fifth </div> </div> </body> </html>
支援的瀏覽器
屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
grid-auto-rows | 57 | 16 | 52 | 10 | 44 |