「画像を中央に配置したいだけなのに、なんだかうまくいかない…」
Web制作でよくある悩みのひとつが、画像(imgタグ)をきれいに中央寄せすることです。実はCSSでは横中央寄せ・縦中央寄せ・上下中央寄せでやり方が少し違います。
この記事では、CSSで画像を中央寄せする基本の方法から、flexやgridを使った応用まで、初心者にもわかりやすく解説します。「こんなに簡単だったの?」と驚くこと間違いなしです!
画像の中央寄せ:基本の考え方

中央寄せの種類を理解しよう
画像の中央寄せには、実は3つのパターンがあります。それぞれに最適な方法が異なるので、まずは違いを理解しましょう。
3つの中央寄せパターン
種類 | 説明 | よくある使用場面 |
---|---|---|
横中央寄せ | 左右の中央に配置 | ヘッダーロゴ、記事内画像 |
縦中央寄せ | 上下の中央に配置 | サイドバー、カードレイアウト |
完全中央寄せ | 横と縦の両方の中央 | ヒーローイメージ、モーダル |
画像要素の特性を知っておこう
imgタグの基本性質
<img src="sample.jpg" alt="サンプル画像">
デフォルトの表示特性
- インライン要素として動作
- ベースラインに揃って表示
- 親要素の幅を超えない
- アスペクト比を保持
中央寄せに影響する要因
- 親要素のサイズ:中央寄せの基準となる
- 画像のサイズ:親要素より大きいと問題が発生
- display プロパティ:inline、block、inline-blockで動作が変わる
まとめと次章への橋渡し
中央寄せの基本を理解したところで、まずは最もよく使われる「横中央寄せ」から詳しく見ていきましょう。
横(左右)に中央寄せする方法
方法1:text-align を使う(最も簡単)
基本のコード
<div class="container">
<img src="sample.jpg" alt="サンプル画像">
</div>
.container {
text-align: center;
}
なぜこれで中央寄せできるの?
img
タグはインライン要素text-align: center
はインライン要素を中央寄せする- テキストと同じように画像も中央に配置される
実用的な応用例
記事内の画像中央寄せ
.article-content {
text-align: center;
}
.article-content img {
max-width: 100%;
height: auto;
}
ヘッダーロゴの中央寄せ
.header {
text-align: center;
padding: 20px 0;
}
.logo {
max-height: 60px;
width: auto;
}
方法2:margin: auto を使う
基本のコード
img {
display: block;
margin: 0 auto;
}
重要なポイント
display: block
が必須- インライン要素では
margin: auto
が効かない margin: 0 auto
は「上下0、左右auto」の意味
この方法のメリット・デメリット
メリット
- 画像だけに適用できる(親要素に影響しない)
- 他の要素の中央寄せに影響しない
- レスポンシブ対応しやすい
デメリット
- display プロパティの変更が必要
- インライン要素の特性を失う
実用例:カードレイアウトでの画像
.card {
border: 1px solid #ddd;
padding: 20px;
border-radius: 8px;
}
.card img {
display: block;
margin: 0 auto 15px auto;
max-width: 100%;
border-radius: 4px;
}
.card h3 {
text-align: center;
}
方法3:flexbox を使う
基本のコード
.container {
display: flex;
justify-content: center;
}
flexbox の利点
- 複数の画像も一緒に中央寄せ
- 他の要素との配置も同時に制御
- レスポンシブ対応が簡単
複数画像の中央寄せ
.image-gallery {
display: flex;
justify-content: center;
gap: 20px;
flex-wrap: wrap;
}
.image-gallery img {
max-width: 200px;
height: auto;
border-radius: 8px;
}
方法の使い分けガイド
方法 | 適用場面 | 難易度 | おすすめ度 |
---|---|---|---|
text-align: center | 記事内、シンプルなレイアウト | ⭐ | ⭐⭐⭐⭐⭐ |
margin: 0 auto | 独立した画像、カードレイアウト | ⭐⭐ | ⭐⭐⭐⭐ |
justify-content: center | 複雑なレイアウト、複数要素 | ⭐⭐⭐ | ⭐⭐⭐⭐ |
まとめと次章への橋渡し
横中央寄せはtext-align: center
が最もシンプルで効果的です。次は、より難易度の高い「縦中央寄せ」について詳しく解説します。
縦(上下)に中央寄せする方法
なぜ縦中央寄せは難しいの?
横中央寄せとの違い
- 横方向:テキストの中央寄せの延長で簡単
- 縦方向:HTMLの自然な流れに逆らうため複雑
- 高さの概念:親要素の明確な高さが必要
方法1:flexbox を使う(推奨)
基本のコード
.container {
display: flex;
align-items: center;
height: 300px; /* 高さの指定が必要 */
}
完全なHTML例
<div class="vertical-center">
<img src="sample.jpg" alt="縦中央寄せ画像">
</div>
.vertical-center {
display: flex;
align-items: center;
height: 400px;
border: 1px solid #ccc;
padding: 20px;
}
.vertical-center img {
max-width: 200px;
height: auto;
}
flexbox の軸を理解しよう
flex の基本軸
- 主軸(Main Axis):
justify-content
で制御 - 交差軸(Cross Axis):
align-items
で制御
flex-direction: row
(デフォルト)の場合
- 主軸:横方向(左→右)
- 交差軸:縦方向(上→下)
align-items: center
:縦中央寄せ
方法2:CSS Grid を使う
基本のコード
.container {
display: grid;
align-items: center;
height: 300px;
}
Grid の利点
- 1行で簡潔に書ける
- 複雑なレイアウトにも対応
- 2次元レイアウトの制御が得意
Grid vs Flexbox の比較
特徴 | Flexbox | CSS Grid |
---|---|---|
学習コスト | 低い | 中程度 |
1次元レイアウト | 得意 | 可能 |
2次元レイアウト | 苦手 | 得意 |
ブラウザ対応 | 良好 | 良好(モダンブラウザ) |
方法3:position を使う(従来の方法)
基本のコード
.container {
position: relative;
height: 300px;
}
.container img {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
なぜこの方法が機能するの?
top: 50%
:親要素の高さの50%の位置に配置transform: translateY(-50%)
:自分の高さの50%分上に移動- 結果:画像の中心が親要素の中心に一致
position 方法の注意点
メリット
- 古いブラウザでも動作
- 絶対的な位置指定が可能
デメリット
- 他の要素との重なりが発生しやすい
- レスポンシブ対応が複雑
- レイアウトの流れを乱す
実用的な縦中央寄せ例
ヒーローセクションでの活用
.hero {
display: flex;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.hero-image {
max-width: 500px;
width: 100%;
height: auto;
border-radius: 12px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}
カードコンポーネントでの画像配置
.card {
display: flex;
align-items: center;
padding: 30px;
background: white;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
height: 200px;
}
.card-image {
width: 80px;
height: 80px;
object-fit: cover;
border-radius: 50%;
margin-right: 20px;
}
まとめと次章への橋渡し
縦中央寄せはflexbox
のalign-items: center
が最も確実で現代的な方法です。次は、横と縦を組み合わせた「完全中央寄せ」を見ていきましょう。
完全中央寄せ(横・縦の両方)

方法1:flexbox の組み合わせ(最も推奨)
基本のコード
.container {
display: flex;
justify-content: center; /* 横中央 */
align-items: center; /* 縦中央 */
height: 400px;
}
完全な実装例
<div class="perfect-center">
<img src="hero-image.jpg" alt="完全中央寄せ画像">
</div>
.perfect-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #f8f9fa;
}
.perfect-center img {
max-width: 600px;
width: 90%;
height: auto;
border-radius: 12px;
box-shadow: 0 25px 50px rgba(0,0,0,0.15);
}
flexbox 完全中央寄せのメリット
簡潔性
- 2つのプロパティだけで実現
- 直感的で理解しやすい
- 保守性が高い
柔軟性
- 複数の要素も同時に中央寄せ
- レスポンシブ対応が簡単
- 他のレイアウトとの組み合わせが容易
方法2:CSS Grid の place-items
基本のコード
.container {
display: grid;
place-items: center;
height: 400px;
}
place-items の魔法
place-items: center
は以下の省略形:align-items: center
(縦中央)justify-items: center
(横中央)
Grid を使った高度な中央寄せ
.grid-center {
display: grid;
place-items: center;
grid-template-columns: 1fr;
min-height: 100vh;
gap: 30px;
padding: 20px;
}
.grid-center img {
max-width: 500px;
width: 100%;
height: auto;
border-radius: 16px;
}
.grid-center h2 {
font-size: 2.5rem;
text-align: center;
margin: 0;
color: #333;
}
方法3:position + transform(レガシー対応)
基本のコード
.container {
position: relative;
height: 400px;
}
.container img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
transform の仕組み
top: 50%, left: 50%
:親要素の中心に左上角を配置transform: translate(-50%, -50%)
:自分のサイズの50%分だけ左上に移動- 結果:要素の中心が親要素の中心に一致
position 方法の現代的な改良版
.legacy-center {
position: relative;
height: 100vh;
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
.legacy-center img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 80%;
max-height: 80%;
width: auto;
height: auto;
border-radius: 20px;
box-shadow: 0 30px 60px rgba(0,0,0,0.3);
}
実用的な完全中央寄せの活用例
ランディングページのヒーロー画像
.hero-section {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
position: relative;
overflow: hidden;
}
.hero-image {
max-width: 700px;
width: 85%;
height: auto;
border-radius: 20px;
box-shadow:
0 50px 100px rgba(0,0,0,0.3),
0 0 0 1px rgba(255,255,255,0.1);
transform: perspective(1000px) rotateY(-5deg) rotateX(5deg);
transition: transform 0.3s ease;
}
.hero-image:hover {
transform: perspective(1000px) rotateY(0deg) rotateX(0deg) scale(1.02);
}
モーダルダイアログの画像表示
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.8);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-image {
max-width: 90%;
max-height: 90%;
width: auto;
height: auto;
border-radius: 8px;
box-shadow: 0 25px 50px rgba(0,0,0,0.5);
}
ローディング画面のスピナー
.loading-screen {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #fff;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 9999;
}
.loading-logo {
width: 120px;
height: auto;
margin-bottom: 30px;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
方法比較と選択指針
方法 | 学習コスト | 柔軟性 | モダン度 | おすすめ用途 |
---|---|---|---|---|
Flexbox | 低 | 高 | ⭐⭐⭐⭐⭐ | 一般的な中央寄せ全般 |
CSS Grid | 中 | 高 | ⭐⭐⭐⭐⭐ | 複雑なレイアウト |
Position + Transform | 中 | 低 | ⭐⭐ | レガシーブラウザ対応 |
まとめと次章への橋渡し
完全中央寄せはflexbox
の組み合わせが最も実用的で現代的です。次は、これらの基本を応用した実践的なテクニックを紹介します。
応用テクニックとレスポンシブ対応
レスポンシブな画像中央寄せ
基本的なレスポンシブ設定
.responsive-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 50vh;
padding: 20px;
}
.responsive-center img {
max-width: 100%;
height: auto;
border-radius: 12px;
}
/* タブレット */
@media (max-width: 768px) {
.responsive-center {
min-height: 40vh;
padding: 15px;
}
}
/* スマートフォン */
@media (max-width: 480px) {
.responsive-center {
min-height: 30vh;
padding: 10px;
}
.responsive-center img {
border-radius: 8px;
}
}
ビューポート単位を活用した中央寄せ
.viewport-center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* ビューポートの高さ */
width: 100vw; /* ビューポートの幅 */
}
.viewport-center img {
max-width: 80vw; /* ビューポート幅の80% */
max-height: 80vh; /* ビューポート高さの80% */
width: auto;
height: auto;
object-fit: contain;
}
アスペクト比を保った中央寄せ
aspect-ratio プロパティの活用
.aspect-container {
display: flex;
justify-content: center;
align-items: center;
aspect-ratio: 16/9; /* 16:9 の比率を維持 */
background: #f0f0f0;
border-radius: 12px;
}
.aspect-container img {
max-width: 90%;
max-height: 90%;
object-fit: contain;
border-radius: 8px;
}
object-fit と object-position の組み合わせ
.crop-container {
display: flex;
justify-content: center;
align-items: center;
width: 400px;
height: 300px;
overflow: hidden;
border-radius: 16px;
}
.crop-container img {
width: 100%;
height: 100%;
object-fit: cover; /* 画像をトリミングして全体を覆う */
object-position: center; /* トリミング位置を中央に */
}
複数画像の中央寄せパターン
水平方向に並べる
.horizontal-gallery {
display: flex;
justify-content: center;
align-items: center;
gap: 30px;
padding: 40px 20px;
flex-wrap: wrap;
}
.horizontal-gallery img {
width: 200px;
height: 150px;
object-fit: cover;
border-radius: 12px;
transition: transform 0.3s ease;
}
.horizontal-gallery img:hover {
transform: scale(1.05);
}
グリッドレイアウトでの中央寄せ
.grid-gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 30px;
justify-items: center;
align-items: center;
padding: 40px 20px;
}
.grid-gallery img {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 12px;
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
}
アニメーション付き中央寄せ
フェードイン効果
.fade-in-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.fade-in-center img {
max-width: 600px;
width: 90%;
height: auto;
border-radius: 16px;
opacity: 0;
transform: translateY(30px);
animation: fadeInUp 1s ease forwards;
}
@keyframes fadeInUp {
to {
opacity: 1;
transform: translateY(0);
}
}
ズームイン効果
.zoom-in-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
}
.zoom-in-center img {
max-width: 500px;
width: 80%;
height: auto;
border-radius: 20px;
transform: scale(0.8);
animation: zoomIn 0.8s ease forwards;
}
@keyframes zoomIn {
to {
transform: scale(1);
}
}
高度な中央寄せテクニック
可変高さコンテナでの中央寄せ
.dynamic-height {
display: flex;
justify-content: center;
align-items: center;
min-height: calc(100vh - 200px); /* ヘッダー・フッター分を除く */
padding: 50px 20px;
}
.dynamic-height img {
max-width: min(600px, 90%); /* 最小値を取る */
height: auto;
border-radius: 16px;
}
Container Queries を使った未来的なアプローチ
.container-query-center {
container-type: inline-size;
display: flex;
justify-content: center;
align-items: center;
min-height: 400px;
}
@container (max-width: 500px) {
.container-query-center img {
max-width: 100%;
border-radius: 8px;
}
}
@container (min-width: 501px) {
.container-query-center img {
max-width: 400px;
border-radius: 16px;
}
}
よくある問題と解決法
問題1:画像が親要素をはみ出る
原因
/* 問題のあるコード */
.container {
width: 300px;
display: flex;
justify-content: center;
}
.container img {
width: 500px; /* 親要素より大きい */
}
解決法
.container {
width: 300px;
display: flex;
justify-content: center;
}
.container img {
max-width: 100%;
width: auto;
height: auto;
}
問題2:縦中央寄せが効かない
原因
/* 高さが指定されていない */
.container {
display: flex;
align-items: center;
/* height の指定なし */
}
解決法
.container {
display: flex;
align-items: center;
min-height: 400px; /* 高さの指定が必要 */
}
問題3:画像の比率が崩れる
原因
/* 幅と高さを固定している */
.container img {
width: 300px;
height: 200px;
}
解決法
.container img {
width: 300px;
height: auto; /* 比率を保持 */
/* または */
max-width: 300px;
max-height: 200px;
object-fit: contain;
}
パフォーマンス最適化
画像最適化と中央寄せの両立
.optimized-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.optimized-center img {
max-width: 100%;
height: auto;
/* GPU加速による滑らかなアニメーション */
will-change: transform;
/* 高解像度ディスプレイ対応 */
image-rendering: -webkit-optimize-contrast;
image-rendering: crisp-edges;
}
レイジーローディングとの組み合わせ
<div class="lazy-center">
<img
src="placeholder.jpg"
data-src="high-resolution.jpg"
alt="遅延読み込み画像"
loading="lazy"
>
</div>
.lazy-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 400px;
}
.lazy-center img {
max-width: 100%;
height: auto;
transition: opacity 0.3s ease;
}
.lazy-center img[data-src] {
opacity: 0.6; /* 読み込み前は薄く表示 */
}
まとめ

CSSで画像を中央寄せする方法は用途に応じて使い分けることが重要です。現代のWeb開発では、flexboxとCSS Gridが主流となっています。
方法別まとめ
横中央寄せ
text-align: center
:最もシンプル、記事やシンプルなレイアウトに最適margin: 0 auto
:画像のみに適用、ブロック要素として扱いたい場合justify-content: center
:複雑なレイアウト、複数要素の配置
縦中央寄せ
align-items: center
:flexboxの交差軸で制御- Grid の
align-items
:グリッドレイアウトでの縦配置 - Position + Transform:レガシー対応が必要な場合
完全中央寄せ
- Flexbox組み合わせ:
justify-content: center
+align-items: center
- CSS Grid:
place-items: center
で一発設定 - Position + Transform:古いブラウザサポートが必要な場合
実践的な選択指針
初心者向け推奨
- 横中央寄せ:
text-align: center
から始める - 縦中央寄せ:
display: flex
+align-items: center
- 完全中央寄せ:
display: flex
+ 両方のプロパティ
中級者向け推奨
- レスポンシブ:viewport単位とmedia queriesの組み合わせ
- 複数画像:CSS Gridでの配置制御
- アニメーション:transformとtransitionの活用
上級者向け推奨
- パフォーマンス最適化:will-changeとGPU加速
- Container Queries:次世代レスポンシブ技術
- アクセシビリティ:画像の代替テキストと適切なコントラスト
プロジェクト規模別アプローチ
小規模サイト(個人ブログなど)
/* シンプルで確実な方法 */
.content img {
display: block;
margin: 20px auto;
max-width: 100%;
height: auto;
border-radius: 8px;
}
中規模サイト(企業サイトなど)
/* コンポーネント指向の設計 */
.image-container {
display: flex;
justify-content: center;
align-items: center;
padding: 2rem;
}
.image-container--hero {
min-height: 60vh;
}
.image-container--card {
height: 200px;
}
.responsive-image {
max-width: 100%;
height: auto;
border-radius: var(--border-radius, 8px);
box-shadow: var(--box-shadow, none);
}
大規模サイト(Webアプリケーションなど)
/* CSS カスタムプロパティとユーティリティクラス */
:root {
--center-padding: clamp(1rem, 5vw, 3rem);
--image-radius: clamp(8px, 1vw, 16px);
--transition-speed: 0.3s;
}
.u-center-flex {
display: flex;
justify-content: center;
align-items: center;
}
.u-center-grid {
display: grid;
place-items: center;
}
.responsive-media {
max-width: 100%;
height: auto;
border-radius: var(--image-radius);
transition: transform var(--transition-speed) ease;
}
.responsive-media:hover {
transform: scale(1.02);
}
ブラウザ対応とフォールバック
モダンブラウザ(推奨)
.modern-center {
display: grid;
place-items: center;
min-height: 100vh;
}
幅広いブラウザ対応
.universal-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* IE11以下のフォールバック */
.ie-fallback {
text-align: center;
}
.ie-fallback::before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.ie-fallback img {
vertical-align: middle;
max-width: 100%;
}
デバッグとトラブルシューティング
中央寄せがうまくいかない時のチェックリスト
1. 親要素の高さを確認
/* 縦中央寄せに高さは必須 */
.parent {
height: 400px; /* または min-height */
display: flex;
align-items: center;
}
2. 画像のサイズ制限を確認
/* 画像が親要素をはみ出していないか */
img {
max-width: 100%;
height: auto;
}
3. display プロパティを確認
/* margin: auto はブロック要素でのみ有効 */
img {
display: block;
margin: 0 auto;
}
4. flexboxの軸を確認
/* flex-direction の値によって軸が変わる */
.container {
display: flex;
flex-direction: row; /* デフォルト */
justify-content: center; /* 主軸(横)の中央寄せ */
align-items: center; /* 交差軸(縦)の中央寄せ */
}
デバッグ用CSS
/* 開発時のデバッグ用スタイル */
.debug * {
outline: 1px solid red;
}
.debug .container {
background: rgba(255, 0, 0, 0.1);
}
.debug img {
background: rgba(0, 255, 0, 0.1);
}
将来性と新技術
CSS Subgrid(近い将来)
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
place-items: center;
}
.grid-item {
display: subgrid;
/* 親グリッドの配置を継承 */
}
CSS Container Queries(現在実装中)
.responsive-container {
container-type: inline-size;
}
@container (min-width: 400px) {
.image-center {
display: grid;
place-items: center;
}
}
CSS Anchor Positioning(実験段階)
.anchor-center {
position: anchor(center);
/* より直感的な中央寄せ */
}
アクセシビリティの考慮
画像の適切な代替テキスト
<!-- 装飾的な画像 -->
<img src="decoration.jpg" alt="" role="presentation">
<!-- 意味のある画像 -->
<img src="chart.jpg" alt="2023年売上グラフ:前年比120%増加">
<!-- 複雑な画像 -->
<img src="complex-chart.jpg" alt="詳細は以下の表を参照"
longdesc="chart-description.html">
フォーカス管理
.center-image {
display: flex;
justify-content: center;
align-items: center;
}
.center-image img:focus {
outline: 2px solid #007bff;
outline-offset: 4px;
}
動きの配慮
/* アニメーションを無効にする設定への対応 */
@media (prefers-reduced-motion: reduce) {
.animated-center img {
animation: none;
transition: none;
}
}
パフォーマンス最適化の詳細
Critical CSS の抽出
/* Above the fold の画像中央寄せは最小限のCSS */
.hero-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.hero-image {
max-width: 100%;
height: auto;
}
GPU加速の活用
.gpu-optimized {
display: flex;
justify-content: center;
align-items: center;
/* GPU加速を促進 */
transform: translateZ(0);
will-change: transform;
}
画像最適化との連携
<!-- レスポンシブ画像 -->
<picture class="responsive-center">
<source media="(min-width: 1200px)" srcset="large.jpg">
<source media="(min-width: 800px)" srcset="medium.jpg">
<img src="small.jpg" alt="レスポンシブ画像" loading="lazy">
</picture>
.responsive-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 50vh;
}
.responsive-center img {
max-width: 100%;
height: auto;
border-radius: clamp(8px, 2vw, 16px);
}
コメント