「ボタンやテキストを画面のど真ん中に置きたい!」
「上下左右、完全に中央寄せしたいのにうまくいかない…」
Webデザインをしていると必ず出てくるのが「中央寄せ」。特にページの**上下左右の真ん中(画面全体の中央)**に配置するのは、やり方を知らないと意外と難しいものです。
この記事では、初心者にもわかりやすく次のことを解説します:
- Flexboxを使った基本的な中央寄せ
- CSS Gridでのシンプルな中央寄せ
- 従来のposition + transformテクニック
- よくある失敗と解決方法
CSSで要素を画面全体の中央に寄せる基本的な方法をマスターして、美しいレイアウトを作りましょう。
画面全体の中央に寄せる基本

1. Flexboxで中央寄せ
今一番よく使われる方法は、flexbox
を使った中央寄せです。
基本的なコード
.wrapper {
display: flex;
justify-content: center; /* 横方向の中央寄せ */
align-items: center; /* 縦方向の中央寄せ */
height: 100vh; /* ビューポートの高さに合わせる */
}
<div class="wrapper">
<div class="box">中央に配置</div>
</div>
これだけで、.box
が画面の上下左右の中央に配置されます。
プロパティの説明
justify-content: center
- 主軸(デフォルトでは横方向)での中央寄せ
- 要素を左右の中央に配置
align-items: center
- 交差軸(デフォルトでは縦方向)での中央寄せ
- 要素を上下の中央に配置
height: 100vh
- ビューポート(表示領域)の高さを100%指定
- これがないと縦方向の中央寄せが効かない
実際の使用例
.hero-section {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.hero-content {
text-align: center;
color: white;
}
.hero-title {
font-size: 3rem;
margin-bottom: 1rem;
}
.hero-subtitle {
font-size: 1.2rem;
opacity: 0.9;
}
<div class="hero-section">
<div class="hero-content">
<h1 class="hero-title">ようこそ</h1>
<p class="hero-subtitle">美しいWebサイトへ</p>
</div>
</div>
2. CSS Gridを使って中央寄せ
最近はCSS Grid
でもっとシンプルに書けます。
基本的なコード
.wrapper {
display: grid;
place-items: center; /* 縦・横中央をまとめて指定 */
height: 100vh;
}
place-itemsの詳細
**place-items: center
**は以下の2つのプロパティの短縮形です:
.wrapper {
display: grid;
align-items: center; /* 縦方向の中央寄せ */
justify-items: center; /* 横方向の中央寄せ */
height: 100vh;
}
Grid レイアウトの利点
シンプルな記述:
- 1行で縦横両方の中央寄せが可能
- より直感的な書き方
柔軟性:
- 複数要素の配置も簡単
- レスポンシブ対応も容易
実際の使用例
.login-page {
display: grid;
place-items: center;
height: 100vh;
background-color: #f5f5f5;
}
.login-form {
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
3. 複数要素を中央に配置
Flexboxで複数要素
.wrapper {
display: flex;
flex-direction: column; /* 縦に並べる */
justify-content: center;
align-items: center;
height: 100vh;
gap: 1rem; /* 要素間の間隔 */
}
<div class="wrapper">
<h1>メインタイトル</h1>
<p>説明文</p>
<button>ボタン</button>
</div>
Gridで複数要素
.wrapper {
display: grid;
place-items: center;
height: 100vh;
gap: 1rem;
}
.content {
text-align: center;
}
ちょっと応用:固定幅のボックスを中央に置く
基本パターン
例えば幅300px、高さ200pxのボックスを画面の中央に置きたいときも、同じくflex
やgrid
が便利です。
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
width: 300px;
height: 200px;
background: #f0f0f0;
border-radius: 8px;
padding: 2rem;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
レスポンシブ対応
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 1rem; /* 画面端からの余白 */
}
.box {
width: 100%;
max-width: 400px; /* 最大幅を制限 */
background: white;
border-radius: 8px;
padding: 2rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* スマホ対応 */
@media (max-width: 480px) {
.wrapper {
padding: 0.5rem;
}
.box {
padding: 1.5rem;
}
}
カードコンポーネントの例
.card-container {
display: grid;
place-items: center;
height: 100vh;
background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%);
}
.card {
background: white;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 300px;
width: 90%;
}
.card-title {
font-size: 1.5rem;
margin-bottom: 1rem;
color: #2d3436;
}
.card-content {
color: #636e72;
line-height: 1.6;
}
よくあるミスとポイント
1. heightを忘れない
上下中央にしたい場合は、height: 100vh;
を指定して、親要素の高さを画面に合わせるのがポイントです。これを忘れると中央に寄りません。
間違いの例
/* これでは縦方向の中央寄せが効かない */
.wrapper {
display: flex;
justify-content: center;
align-items: center;
/* height: 100vh; がない! */
}
正しい例
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* これが重要! */
}
2. ビューポート単位の使い分け
100vh
vs 100%
の違い:
/* ビューポートの高さに合わせる(推奨) */
.full-screen {
height: 100vh;
}
/* 親要素の高さに合わせる */
.relative-height {
height: 100%;
}
使い分けのポイント:
100vh
:画面全体の高さを基準にしたい場合100%
:親要素の高さを基準にしたい場合
3. モバイル環境での注意
モバイルブラウザでは、アドレスバーの表示・非表示により100vh
の値が変わることがあります:
/* モバイル対応の改善案 */
.wrapper {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh; /* minを使用 */
/* または */
height: 100dvh; /* 動的ビューポート(新しいブラウザ) */
}
従来の方法:position + transform

昔からあるテクニックとしてposition: absolute;
とtransform
を組み合わせる方法もあります。
基本的な書き方
.wrapper {
position: relative; /* 基準となる親要素 */
height: 100vh;
}
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
仕組みの説明
top: 50%; left: 50%;
:要素の左上角を中央に配置transform: translate(-50%, -50%);
:要素自身のサイズの半分だけ左上に移動
この方法のメリット・デメリット
メリット:
- 古いブラウザでも対応
- 親要素の
display
プロパティに依存しない - 要素のサイズが不明でも使える
デメリット:
- 記述が複雑
position: absolute
による副作用- レスポンシブ対応が難しい
実際の使用例
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
}
.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border-radius: 8px;
padding: 2rem;
max-width: 500px;
width: 90%;
}
実用的な応用例
1. ローディング画面
.loading-screen {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #f8f9fa;
display: grid;
place-items: center;
z-index: 9999;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #e9ecef;
border-top: 4px solid #007bff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
2. エラーページ
.error-page {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
text-align: center;
padding: 2rem;
}
.error-code {
font-size: 6rem;
font-weight: bold;
color: #e74c3c;
margin: 0;
}
.error-message {
font-size: 1.5rem;
color: #7f8c8d;
margin: 1rem 0 2rem;
}
.error-button {
background: #3498db;
color: white;
padding: 1rem 2rem;
border: none;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
text-decoration: none;
display: inline-block;
}
3. ランディングページのヒーロー
.hero {
display: grid;
place-items: center;
height: 100vh;
background: url('hero-bg.jpg') center/cover;
color: white;
text-align: center;
}
.hero-content {
max-width: 600px;
padding: 2rem;
}
.hero-title {
font-size: clamp(2rem, 5vw, 4rem);
margin-bottom: 1rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
.hero-subtitle {
font-size: clamp(1rem, 3vw, 1.5rem);
margin-bottom: 2rem;
opacity: 0.9;
}
.cta-button {
background: #e74c3c;
color: white;
padding: 1rem 2rem;
border: none;
border-radius: 6px;
font-size: 1.1rem;
cursor: pointer;
transition: background 0.3s ease;
}
.cta-button:hover {
background: #c0392b;
}
ブラウザサポートと使い分け
モダンブラウザ対応
Flexbox:
- IE11以降でサポート
- 最も汎用的で安全
CSS Grid:
- IE11以降でサポート(一部制限あり)
- モダンブラウザで推奨
Position + Transform:
- 全ブラウザでサポート
- レガシー環境での最終手段
推奨する使い分け
/* 推奨順位 */
/* 1. 基本的な中央寄せ → Flexbox */
.center-flex {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* 2. よりシンプルに → Grid */
.center-grid {
display: grid;
place-items: center;
height: 100vh;
}
/* 3. 特殊な要件 → Position + Transform */
.center-absolute {
position: relative;
height: 100vh;
}
.center-absolute .content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
まとめ
CSSで「画面全体の中央」に要素を配置する方法について、重要なポイントをまとめます:
主要な3つの方法
- Flexbox(justify-content + align-items):最も汎用的で安全
- CSS Grid(place-items):よりシンプルな記述が可能
- Position + Transform:古いブラウザ対応や特殊用途
成功のポイント
height: 100vh
の指定:縦方向の中央寄せには必須- レスポンシブ対応:
max-width
とpadding
で画面サイズに配慮 - 適切な方法の選択:要件とブラウザサポートに応じて使い分け
コメント