グリッドレイアウトの作成方法と使い方についての説明です。
CSSに「display: grid」を指定した上で、作成するレイアウトに応じて下記の要素を指定します。
長さは、「px」や「%」などの単位を付けた数値を半角スペース区切りで記述します。
.grid1 {
display: grid;
grid-template-rows: 30% 40% 30%;
}
.grid1 div {
margin: 4px;
background-color: skyblue;
}
<div class="grid1">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
.grid2 {
display: grid;
grid-template-rows: 30% 40% 30%;
grid-template-columns: 40% 30% 30%;
}
.grid2 div {
margin: 4px;
background-color: skyblue;
}
<div class="grid2">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
画像のみをグリッド状に配置するソースコードです。画像1つにつき1つのグリッドです。
<div class="item5">
<span><img src="画像のURL" alt=""></span>
<span><img src="画像のURL" alt=""></span>
<span><img src="画像のURL" alt=""></span>
<span><img src="画像のURL" alt=""></span>
<span><img src="画像のURL" alt=""></span>
</div>
.item5 {
display: grid;
grid-template-columns: calc( 100% / 3 ) calc( 100% / 3 ) calc( 100% / 3 );
grid-template-rows: 50% 50%;
}
.item5 span {
padding: 15px;
}
.item5 {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: calc( 100% / 3 ) calc( 100% / 3 ) calc( 100% / 3 );
}
.item5 span {
padding: 10px;
}
画像の下にテキストを配置してグリッド化するソースコードです。1列につき、画像とテキストがセットで1つのグリッド、計3つのグリッドです。スマホでは「display: block」を指定し、すべて1列の縦並びにしています。
<div class="grid3x1">
<div>
<img src="画像のURL" alt="">
<p>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</p>
</div>
<div>
<img src="画像のURL" alt="">
<p>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</p>
</div>
<div>
<img src="画像のURL" alt="">
<p>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</p>
</div>
</div>
.grid3x1 {
display: grid;
grid-template-columns: calc( 100% / 3 ) calc( 100% / 3 ) calc( 100% / 3 );
}
.grid3x1 div {
padding: 15px;
}
.grid3x1 {
display: block;
}
.grid3x1 div {
padding: 15px 10px;
}
テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト
テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト
テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト