grid
grid
は CSS のプロパティで、一括指定プロパティとして明示的・暗黙的なすべてのグリッドプロパティを単一の宣言で設定します。
grid
を使用すると、一方の軸を grid-template-rows
または grid-template-columns
を使用して設定し、もう一方の軸でどのように内容物を自動反復させるかを、暗黙のグリッドプロパティである grid-auto-rows
, grid-auto-columns
, grid-auto-flow
で設定します。
試してみましょう
メモ: 指定しないサブプロパティは、通常の一括指定と同様に初期値に設定されます。また、溝に関するプロパティはこの一括指定では初期化されません。
構成要素のプロパティ
このプロパティは以下の CSS プロパティの一括指定です。
構文
/* <'grid-template'> 値 */
grid: none;
grid: "a" 100px "b" 1fr;
grid: [linename1] "a" 100px [linename2];
grid: "a" 200px "b" min-content;
grid: "a" minmax(100px, max-content) "b" 20%;
grid: 100px / 200px;
grid: minmax(400px, min-content) / repeat(auto-fill, 50px);
/* <'grid-template-rows'> /
[ auto-flow && dense? ] <'grid-auto-columns'>? 値 */
grid: 200px / auto-flow;
grid: 30% / auto-flow dense;
grid: repeat(3, [line1 line2 line3] 200px) / auto-flow 300px;
grid: [line1] minmax(20em, max-content) / auto-flow dense 40%;
/* [ auto-flow && dense? ] <'grid-auto-rows'>? /
<'grid-template-columns'> 値 */
grid: auto-flow / 200px;
grid: auto-flow dense / 30%;
grid: auto-flow 300px / repeat(3, [line1 line2 line3] 200px);
grid: auto-flow dense 40% / [line1] minmax(20em, max-content);
/* グローバル値 */
grid: inherit;
grid: initial;
grid: revert;
grid: unset;
値
<'grid-template'>
-
grid-template
を定義し、これにはgrid-template-columns
,grid-template-rows
,grid-template-areas
が含まれます。 <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>?
-
grid-template-rows
プロパティを明示的に設定 (およびgrid-template-columns
プロパティをnone
に設定) することで行トラックを設定し、grid-auto-columns
プロパティを設定 (およびgrid-auto-rows
をauto
に設定) することで列トラックの自動反復方法を設定します。dense
が設定されていれば、grid-auto-flow
もcolumn
に設定されます。ほかの
grid
のサブプロパティはすべて、初期値に初期化されます。 [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>
-
grid-template-columns
プロパティを明示的に設定 (およびgrid-template-rows
プロパティをnone
に設定) することで列トラックを設定し、grid-auto-rows
プロパティを設定 (およびgrid-auto-columns
をauto
に設定) することで行トラックの自動反復方法を設定します。dense
が設定されていれば、grid-auto-flow
もcolumn
に設定されます。ほかの
grid
のサブプロパティはすべて、初期値に初期化されます。
公式定義
初期値 | 一括指定の次の各プロパティとして
|
---|---|
適用対象 | グリッドコンテナー |
継承 | なし |
パーセント値 | 一括指定の次の各プロパティとして
|
計算値 | 一括指定の次の各プロパティとして
|
アニメーションの種類 | 一括指定の次の各プロパティとして
|
形式文法
例
グリッドレイアウトの作成
HTML
<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
CSS
#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
結果
仕様書
Specification |
---|
CSS Grid Layout Module Level 2 # grid-shorthand |
ブラウザーの互換性
BCD tables only load in the browser
関連情報
- 関連する CSS プロパティ:
grid-template
,grid-template-rows
,grid-template-columns
,grid-template-areas
,grid-auto-columns
,grid-auto-rows
,grid-auto-flow
- グリッドレイアウトガイド: CSS グリッドでの行ベースの配置
- グリッドレイアウトガイド: グリッドテンプレート領域 - グリッド定義の一括指定