グリッドコンテナ内のアイテムの水平方向(インライン軸)の配置を制御するプロパティです。全てのグリッドアイテムに対して一括で配置を指定できます。
justify-items: start | end | center | stretch| justify-items-start | justify-items: start |
| justify-items-center | justify-items: center |
| justify-items-stretch | justify-items: stretch |
.grid-container {
display: grid;
grid-template-columns: repeat(3, 200px);
justify-items: center;
}各グリッドアイテムをセル内の中央に配置
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
justify-items: start;
}各グリッドアイテムをセル内の左端に配置
.grid-container {
display: grid;
grid-template-columns: repeat(2, 300px);
justify-items: stretch;
}各グリッドアイテムをセルの幅に合わせて伸縮(デフォルト)
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 2rem;
justify-items: end;
}レスポンシブカードグリッドで各カードをセル内の右端に配置
justify-selfは個別のアイテムに、justify-itemsは全アイテムに適用される。Flexboxのjustify-contentとは用途が異なるので注意。
Flexboxでは効果がない。グリッドでのみ有効。個別制御にはjustify-selfを使う。
justify-itemsはgrid(とブロックレイアウト)専用で、flexコンテナでは無効です。AIはflexに書いて「効かない」コードを出しがちで、flexで同じことをするなら子側のmargin-inline: autoやコンテナのjustify-contentを使います。
gridの各セル内でアイテムを左寄せ・中央・右寄せに出し分けたい。justify-itemsとjustify-selfで実装して
そのままAIチャットに貼り付けて使える依頼文の例です。ページ上部の「AI用にコピー」でこのページ全体をコンテキストとして渡せます。