grid-template-rows

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

* Some parts of this feature may have varying levels of support.

grid-template-rowsCSS のプロパティで、グリッド行の線名とトラックのサイズ変更機能を定義します。

試してみましょう

構文

css
/* キーワード値 */
grid-template-rows: none;

/* <track-list> 値 */
grid-template-rows: 100px 1fr;
grid-template-rows: [line-name] 100px;
grid-template-rows: [line-name1] 100px [line-name2 line-name3];
grid-template-rows: minmax(100px, 1fr);
grid-template-rows: fit-content(40%);
grid-template-rows: repeat(3, 200px);
grid-template-rows: subgrid;
grid-template-rows: masonry;

/* <auto-track-list> 値 */
grid-template-rows: 200px repeat(auto-fill, 100px) 300px;
grid-template-rows:
  minmax(100px, max-content)
  repeat(auto-fill, 200px) 20%;
grid-template-rows:
  [line-name1] 100px [line-name2]
  repeat(auto-fit, [line-name3 line-name4] 300px)
  100px;
grid-template-rows:
  [line-name1 line-name2] 100px
  repeat(auto-fit, [line-name1] 300px) [line-name3];

/* グローバル値 */
grid-template-rows: inherit;
grid-template-rows: initial;
grid-template-rows: revert;
grid-template-rows: revert-layer;
grid-template-rows: unset;

このプロパティは以下のように指定することができます。

  • キーワード値 none
  • <track-list>
  • <auto-track-list> 値のいずれか

none

明示的なグリッドがないことを示すキーワードです。どの列も暗黙的に生成され、それらのサイズは grid-auto-rows プロパティによって決定されます。

[line-name]

<custom-ident> で、その位置にある線の名称を指定します。識別子には、予約語の spanauto 以外の有効な文字列を指定してください。行は、[line-name-a line-name-b]のように、角括弧内のスペースで区切られた複数の名前を持つことができます。

<length>

負の値ではない長さで、列の幅を指定します。

<percentage>

グリッドコンテナーのブロック方向の寸法に対する相対値で、負ではない <percentage> です。グリッドコンテナーのサイズがそのトラックのサイズに依存している場合、グリッドコンテナーの内在サイズを計算するために、パーセント値は auto として扱われなければなりません。トラックの内在的なサイズの寄与によって、グリッドコンテナーの寸法に合わせられ、パーセント値を尊重した結果、最小の大きさによってトラックの最終的な寸法を増加させる可能性があります。

<flex>

fr の単位の付いた負の数ではない距離で、トラックのフレックス係数を指定します。 <flex> の寸法のトラックは、残りの空間をフレックス係数の割合に比例して共有します。 minmax() 表記の外に現れた場合は、最小値が自動として扱われます (つまり minmax(auto, <flex>))。

max-content

グリッドトラックを占有しているグリッドアイテムの中で、コンテンツ貢献度の最大値を表すキーワードです。

min-content

グリッドトラックを占有しているグリッドアイテムの中で、コンテンツ貢献度の最小値を表すキーワードです。

minmax(min, max)

min 以上、 max 以下の寸法の範囲を定義する関数表記法です。 maxmin より小さい場合、 max は無視され、関数は min として扱われます。最大値として、 <flex> 値はトラックのフレックス係数を設定します。最小値としては無効です。

auto

最大値であれば max-content と同一のキーワードです。

最小値の場合は、グリッドトラックを占めるグリッドアイテムの最大の最小寸法 (min-width/min-height で指定されたもの) を表します。ふつうは min-content ですが、必ずそうとは限りません。

minmax() の外で使われた場合、 auto は上記の最小値と最大値の間の範囲を表します。これはほとんどの場合、minmax(min-content,max-content) と同じように動作します。

メモ: トラックの寸法が auto の場合 (そして auto の場合だけ)、 align-content およびjustify-content プロパティによって引き伸ばされることがあります。

fit-content( [ <length> | <percentage> ] )

min(max-content, max(auto, argument)) の式を表し、 auto と同様に(すなわち minmax(auto, max-content) と)計算されますが、トラックの寸法が auto の最小値よりも大きい場合は argument でクランプされるところが異なります。

repeat( [ <positive-integer> | auto-fill | auto-fit ] , <track-list> )

トラックリストの繰り返しフラグメントを表し、繰り返しパターンを示す多数の行をよりコンパクトな形式で記述できるようにします。

masonry

masonry の値は、この軸が組積アルゴリズムに従ってレイアウトされるべきであることを示します。

subgrid

subgrid の値は、グリッドがその軸に親グリッドのスパン部分を採用することを示します。グリッドの行や列のサイズは、明示的に指定されるのではなく、親グリッドの定義から取得されます。

公式定義

初期値none
適用対象グリッドコンテナー
継承なし
パーセント値コンテンツ領域の対応する寸法に対する相対値
計算値指定通り。ただし相対的な長さはは絶対的な長さに変換される
アニメーションの種類長さ、パーセント値、 calc の単純なリストであり、唯一の違いはリスト内の長さ、パーセント値、 calc の部分の値のみ

形式文法

grid-template-rows = 
none |
<track-list> |
<auto-track-list> |
subgrid <line-name-list>?

<track-list> =
[ <line-names>? [ <track-size> | <track-repeat> ] ]+ <line-names>?

<auto-track-list> =
[ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>? <auto-repeat> [ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>?

<line-name-list> =
[ <line-names> | <name-repeat> ]+

<line-names> =
'[' <custom-ident>* ']'

<track-size> =
<track-breadth> |
minmax( <inflexible-breadth> , <track-breadth> ) |
fit-content( <length-percentage [0,∞]> )

<track-repeat> =
repeat( [ <integer [1,∞]> ] , [ <line-names>? <track-size> ]+ <line-names>? )

<fixed-size> =
<fixed-breadth> |
minmax( <fixed-breadth> , <track-breadth> ) |
minmax( <inflexible-breadth> , <fixed-breadth> )

<fixed-repeat> =
repeat( [ <integer [1,∞]> ] , [ <line-names>? <fixed-size> ]+ <line-names>? )

<auto-repeat> =
repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )

<name-repeat> =
repeat( [ <integer [1,∞]> | auto-fill ] , <line-names>+ )

<track-breadth> =
<length-percentage [0,∞]> |
<flex [0,∞]> |
min-content |
max-content |
auto

<inflexible-breadth> =
<length-percentage [0,∞]> |
min-content |
max-content |
auto

<length-percentage> =
<length> |
<percentage>

<fixed-breadth> =
<length-percentage [0,∞]>

グリッド行の寸法の指定

HTML

html
<div id="grid">
  <div id="areaA">A</div>
  <div id="areaB">B</div>
</div>

CSS

css
#grid {
  display: grid;
  height: 100px;
  grid-template-rows: 30px 1fr;
}

#areaA {
  background-color: lime;
}

#areaB {
  background-color: yellow;
}

結果

仕様書

Specification
CSS Grid Layout Module Level 2
# track-sizing
CSS Grid Layout Module Level 2
# subgrids

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
grid-template-rows
Animation of tracks
auto
fit-content()
masonry
Experimental
max-content
min-content
minmax()
none
repeat()
subgrid

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
User must explicitly enable this feature.
Uses a non-standard name.
Has more compatibility info.

関連情報