LoginSignup
2740
2486

More than 3 years have passed since last update.

CSS Grid Layout を極める!(基礎編)

Last updated at Posted at 2017-04-13

0. はじめに

CSS Grid Layout(グリッドレイアウト)は、2次元レイアウト を、HTML/CSS を使って簡単・自由に操作できる、CSSの新しい機能です。
格子状のマス目のグリッドに好きな順番に配置したり結合したりすることで、様々なレイアウトが可能になります。

img-grid.PNG

例えば、こんな簡単なHTMLで・・・

簡単なHTML
<body>
    <h1>タイトル</h1>
    <article>記事1</article>
    <article>記事2</article>
    <article>記事3</article>
    <article>記事4</article>
    <nav>ナビ</nav>
    <footer>フッター</footer>
</body>

CSS
1.PNG
 CSS Grid Layout 

CSS Grid Layout 20174
Fx52GC57Safari10.1Opera44 
Can I use... CSS Grid Layout
20173Firefox, Google Chrome, Opera, Safari  CSS Grid Layout 

4  
CSS Grid Layout  
CSS Grid Layout 
CSS Grid Layout 
CSS Grid Layout 

1. Grid Layout 


CSS Grid Layout

HTML







display: grid;  display: inline-grid;  Grid Layout   






<div style="display:grid;"> <!-- コンテナ -->
    <div></div> <!-- アイテム-->
    <p></p> <!-- アイテム -->
    <section> <!-- アイテム -->
        <div></div> <!-- これはアイテムではない -->
    </section>
</div>

その他の構成物

  • ライン


1-10





2













Grid Layout


用語1


用語2

2. Grid Layout 使


使3
2x2
s-手書き.jpg

 HTML  CSS 


 HTML 








HTML 
HTML(完成)
<div id="container"> <!-- コンテナ -->
    <div id="itemA">A</div> <!-- アイテム -->
    <div id="itemB">B</div> <!-- アイテム -->
    <div id="itemC">C</div> <!-- アイテム -->
</div>

そして、コンテナdisplay: grid; と記述します。

コンテナのCSS
#container {
    display: grid; /* グリッドレイアウト */
}

これで必須のHTML/CSSの記述は終わりです。

<ステップ2> CSS で各トラックの大きさを指定する

次に、縦横それぞれのトラックの大きさを指定します。
ここで使う新しいプロパティは下の2つです。これらを コンテナ のスタイルとして記述します。

  • grid-template-rows: 行のトラックの高さを半角スペースで区切って指定
  • grid-template-columns: 列のトラックの幅を半角スペースで区切って指定

(※「s」を忘れやすいので注意!)

行のトラックの高さは、1行目は100px、2行目は50pxなので、下のようになります。

grid-template-rows: 100px 50px;

列のトラックの幅は、1列目は150px、2列目は残り全部です。
ここで新しい単位 fr というのを使います。今は「残りの幅」という指定だと理解しておいてください。

grid-template-columns: 150px 1fr;

これで、トラックの高さ・幅の指定はできました。

#containerのCSS(完成)
#container {
    display: grid;
    grid-template-rows: 100px 50px;
    grid-template-columns: 150px 1fr;
}

<ステップ3> CSS でアイテムの配置を指定する


  
2

 -  


使2  


grid-row: 

grid-column: 


23
方法A-1.png 23row13column14
1+1


 


  
方法A-2.png row 12column 23 
CSS
#item {
    grid-row: 1 / 2;
    grid-column: 2 / 3;
}

尚、このように隣り合わせのラインを指定する場合は、スラッシュのあとを省略することも出来ます。

CSS
#item {
    grid-row: 1;
    grid-column: 2;
}


  
方法A-3.png row 13column 34 
CSS
#item {
    grid-row: 1 / 3;
    grid-column: 3 / 4;
}


 / span  
CSS
#item {
    grid-row: 1 / span 2; /* ライン番号1から始めて、2つ先のラインまで伸ばす */
    grid-column: 3 / span 1; /* ライン番号3から始めて、1つ先のラインまで伸ばす */
}


/ span 1 
CSS
#item {
    grid-row: 1 / span 2;
    grid-column: 3;
}

また、図に書いてあるように負のラインの番号で指定することも出来ます。

CSS
#item {
    grid-row: -3 / -1; /* or -3 / span 2 */
    grid-column: -2 / -1; /* or -2 / span 1 */ /* or -2  */
}

grid-row: -2;grid-column: -2;



手書きライン入り.png
<div id="itemA">A</div>  row 13column 12 
#itemAのCSS(完成)
#itemA {
    grid-row: 1 / 3;
    grid-column: 1 / 2;
    background: #f88;
}

<div id="itemB">B</div>row 1~2column 2~3 を占めているので、下のようになります。

#itemBのCSS(完成)
#itemB {
    grid-row: 1 / 2;
    grid-column: 2 / 3;
    background: #8f8;
}

同様に<div id="itemC">C</div>row 2~3column 2~3 を占めているので、下のようになります。

#itemCのCSS(完成)
#itemC {
    grid-row: 2 / 3;
    grid-column: 2 / 3;
    background: #88f;
}

これで、方法AによるGrid Layoutが完成しました。

HTML(方法Aで完成)
<div id="container">
    <div id="itemA">A</div>
    <div id="itemB">B</div>
    <div id="itemC">C</div>
</div>
CSS(方法Aで完成)
/* コンテナ */
#container {
    display: grid;
    grid-template-rows: 100px 50px;
    grid-template-columns: 150px 1fr;
}

/* アイテム */
#itemA {
    grid-row: 1 / 3;
    grid-column: 1 / 2;
    background: #f88;
}
#itemB {
    grid-row: 1 / 2;
    grid-column: 2 / 3;
    background: #8f8;
}
#itemC {
    grid-row: 2 / 3;
    grid-column: 2 / 3;
    background: #88f;
}

02.PNG  Can I use... CSS Grid Layout

 -  


使2


grid-template-areas: 

grid-area:  


grid-template-areas

s-手書き.jpg
2x2

手書きをエリアに.png
 grid-template-areas 使
#container {
    grid-template-areas:
        "areaA areaB"
        "areaA areaC";
}

"11
1areaAareaB 
()
areaA2




.
grid-row/grid-column使
#container {
    grid-template-areas:
        "header header header"
        "nav contents aside"
        ". footer .";
}

/* ↓分かりやすく見た目を整える↓ */

#container {
    grid-template-areas:
        " header header   header "
        " nav    contents aside  "
        " .....  footer   .....  ";
}

さてさきほども書きましたが、最初のサンプルの場合はこのように記述すればいいでしょう。

#container {
    grid-template-areas:
        "areaA areaB"
        "areaA areaC";
}

grid-area
grid-area

#itemA {
    grid-area: areaA;
}

これで、方法BによるGrid Layoutが完成しました。

HTML(方法Bで完成)
<div id="container">
    <div id="itemA">A</div>
    <div id="itemB">B</div>
    <div id="itemC">C</div>
</div>
CSS(方法Bで完成)
/* コンテナ */
#container {
    display: grid;
    grid-template-rows: 100px 50px;
    grid-template-columns: 150px 1fr;
    grid-template-areas:
        "areaA areaB"
        "areaA areaC";
}

/* アイテム */
#itemA {
    grid-area: areaA;
    background: #f88;
}
#itemB {
    grid-area: areaB;
    background: #8f8;
}
#itemC {
    grid-area: areaC;
    background: #88f;
}

02.PNG
実際のサンプル (サンプルが動かない場合には、ブラウザが対応しているかどうか確認してみてください。Can I use... CSS Grid Layout

【チートシート】Grid Layoutで使用するプロパティ

方法A(ラインの番号で指定する)で使うもの

  • コンテナに指定 grid-template-rows: 行のトラックの高さを半角スペースで区切って指定
  • コンテナに指定 grid-template-columns: 列のトラックの幅を半角スペースで区切って指定
  • アイテムに指定 grid-row: アイテムが占める行のラインの番号をスラッシュ区切りで指定する
  • アイテムに指定 grid-column: アイテムが占める列のラインの番号をスラッシュ区切りで指定する
方法AのCSS例
/* コンテナ */
#container {
    display: grid;
    grid-template-rows: 100px 50px;
    grid-template-columns: 150px 1fr;
}
/* アイテム */
#item {
    grid-row: 1 / 3;
    grid-column: 1 / 2;
}

方法B(エリアに名前を付けて指定する)で使うもの

  • コンテナに指定 grid-template-rows: 行のトラックの高さを半角スペースで区切って指定
  • コンテナに指定 grid-template-columns: 列のトラックの幅を半角スペースで区切って指定
  • コンテナに指定 grid-template-areas: エリアを作ってそれぞれに名前を付ける
  • アイテムに指定 grid-area: 各アイテム が占めるエリアの名前を指定する
方法BのCSS例
/* コンテナ */
#container {
    display: grid;
    grid-template-rows: 100px 50px;
    grid-template-columns: 150px 1fr;
    grid-template-areas:
        "areaA areaB"
        "areaA areaC";
}
/* アイテム */
#item {
    grid-area: areaA;
}

3. サンプル

  • 写真と文章を左右に振って配置する

写真と文章.PNG

HTML
<dl id="images">
    <dt id="dt1"><img src="cat0.jpg" alt="猫0"></dt>
    <dd id="dd1"><p>< ごはんまだかな?</p></dd>
    <dt id="dt2"><img src="cat1.jpg" alt="猫1"></dt>
    <dd id="dd2"><p>少しの辛抱にゃ ></p></dd>
</dl>
CSS
#images {
    display: grid;
    grid-template-columns: 200px 200px
}
#dt1 {
    grid-row: 1; grid-column: 1;
    margin-bottom: 20px;
}
#dd1 {
    grid-row: 1; grid-column: 2;
}
#dt2 {
    grid-row: 2; grid-column: 2;
}
#dd2 {
    grid-row: 2; grid-column: 1;
}
  • よくある2カラムレイアウト

よくある2カラムレイアウト.PNG

HTML
<h1>タイトル</h1>
<main>メイン</main>
<nav>ナビ</nav>
<footer>フッター</footer>
CSS
body {
    display: grid;
    margin: 0;
    min-height: 100vh;
    grid-template-rows: 100px 1fr 100px;
    grid-template-columns: 100px 1fr;
}
body > * {
    margin: 3px;
    padding: 10px;
}
h1 {
    grid-row: 1;
    grid-column: 1 / span 2;
    background: #faa;
    border-radius: 10px 10px 0 0;
}
main {
    grid-row: 2;
    grid-column: 2;
    background: #afa;
}
nav {
    grid-row: 2;
    grid-column: 1;
    background: #aaf;
}
footer {
    grid-row: 3;
    grid-column: 1 / span 2;
    background: #aaa;
    border-radius: 0 0 10px 10px;
}
  • よくある3カラムレイアウト

よくある3カラムレイアウト.PNG

HTML
<h1>タイトル</h1>
<main>メイン</main>
<nav>ナビ</nav>
<aside id="side">サイド</aside>
<footer>フッター</footer>
<aside id="ad">広告</aside>
CSS
body {
    display: grid;
    margin: 0;
    min-height: 100vh;
    grid-template-rows: 100px 100px 1fr 100px;
    grid-template-columns: 100px 1fr 100px;
    grid-template-areas:
        "title  title  title "
        "nav    main   ad    "
        "nav    main   side  "
        "footer footer footer";
}
body > * {
    margin: 3px;
    padding: 10px;
}
h1 {
    grid-area: title;
    background: #faa;
    border-radius: 10px 10px 0 0;
}
main {
    grid-area: main;
    background: #afa;
}
nav {
    grid-area: nav;
    background: #aaf;
}
#side {
    grid-area: side;
    background: #faf;
}
footer {
    grid-area: footer;
    background: #aaa;
    border-radius: 0 0 10px 10px;
}
#ad {
    grid-area: ad;
    background: #888;
}

4. 今までと何が違う?


 CSS Grid LayoutCSS Flexible Box Layout  HTML<table> 


CSS Grid Layout  CSS Flexible Box Layout


CSS Flexible Box Layout  display: flex; 使

CSS Grid Layout CSS Flexible Box Layout
CSSの記述 display: grid; /* or inline-grid */ display: flex; /* or inline-flex */
配置の次元 2次元 1次元
img-grid.PNG
縦横自由に配置
img-flex.PNG
折り返しはするけど一方向

CSS Grid Layout と HTMLの<table>

HTMLの<table>も同様にグリッドレイアウトを実現できますが、用法が全く異なります。
決定的な違いは、HTMLのセマンティック(意味)です。

CSS Grid Layout では、HTMLを操作せずにグリッドレイアウトを実現します。
つまり、HTMLのセマンティックを変えずに表示を変えることができるのです。

CSS Grid Layout の HTML
<!-- CSS を用いてグリッドレイアウトにする -->
<h1>タイトル</h1>
<main>メイン</main>
<nav>ナビ</nav>
<footer>フッター</footer>

当然、これを<table>を使って記述するのはダメです。なぜなら、HTMLのセマンティックがおかしなことになってしまうからです。

ダメなHTML
<table>
    <tr><td colspan="2"><h1>タイトル</h1>
    <tr><td><nav>ナビ</nav><td><main>メイン</main>
    <tr><td colspan="2"><footer>フッター</footer>
</table>

しかし逆に、HTMLの<table>を使うべき場面もあります。表をデータとして表示する場合などです。

<table>を使うべき例
<table>
    <caption>果物の値段</caption>
    <thead>
        <tr><th>店名<th>りんご<th>もも
    </thead>
    <tbody>
        <tr><th>A店<td>100円<td>300円
        <tr><th>B店<td>110円<td>280円
    </tbody>
</table>


5. 


CSS Grid Layout 
CSS

6. 


CSS Grid Layout Module Level 1
CSS Grid Layout - CSS | MDN
2740
2486
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

2740
2486