Webページを作っていると、テキストや画像、ボタンなどを「中央に揃えたい」と思うことがよくありますよね。
でもCSSで中央寄せをしようとすると、「横はできたけど縦はできない」「そもそも要素が動かない」「思った位置にならない」など、意外とつまずきやすいポイントです。
実際、CSS初心者の多くが「中央寄せ」で挫折を経験しています。
しかし、安心してください。中央寄せのルールは実はとてもシンプルで、パターンさえ覚えてしまえば誰でも簡単にマスターできます。
この記事では、CSSで中央寄せをするための基本テクニックから、FlexboxやGridを使った最新の方法まで、初心者でもわかるようにやさしく解説します。
なぜ中央寄せは難しく感じるのか?

中央寄せの種類を理解しよう
中央寄せには、実は3つの種類があります。多くの人がつまずくのは、この違いを意識せずに作業しているからです。
1. 横方向の中央寄せ(水平方向)
- テキストを左右の中央に配置
- ボタンやロゴを画面の横中央に配置
2. 縦方向の中央寄せ(垂直方向)
- テキストを上下の中央に配置
- モーダルウィンドウを画面の縦中央に配置
3. 完全な中央寄せ(水平+垂直)
- 要素をど真ん中に配置
- カード要素を画面中央に配置
要素の種類による違い
CSS中央寄せのもう一つの落とし穴は、要素の種類によって方法が違うことです。
インライン要素(文字やリンク)
<span>
,<a>
,<strong>
,<em>
など- 横幅や高さを指定できない
text-align
で簡単に中央寄せ可能
ブロック要素(箱となる要素)
<div>
,<p>
,<h1>
,<img>
など- 横幅や高さを指定できる
margin
やflexbox
が必要
インラインブロック要素
<img>
,<input>
,<button>
など- インラインとブロックの特徴を併せ持つ
- 場合によって方法を使い分ける
これらの違いを知らずに作業すると、「なぜか中央寄せできない!」という状況に陥ってしまいます。
テキストを中央寄せする方法
基本の text-align を使った方法
テキストの中央寄せは最もシンプルです。CSSの text-align
プロパティを使うだけでOK。
p {
text-align: center;
}
HTML例
<p>この文章は中央に配置されます</p>
<h1>見出しも中央寄せ</h1>
<div>
<a href="#">リンクも中央に</a>
</div>
text-align が効く要素・効かない要素
効く要素
- すべてのテキスト(
<p>
,<h1>-<h6>
,<span>
など) - インライン要素(
<a>
,<strong>
,<em>
など) - インラインブロック要素(
<img>
,<button>
など)
効かない要素
- ブロック要素自体(
<div>
,<section>
など) float
されている要素position: absolute
の要素
テキストの様々な中央寄せパターン
複数行テキストの中央寄せ
.multi-line-center {
text-align: center;
line-height: 1.6; /* 行間も調整 */
}
リストの中央寄せ
ul {
text-align: center;
list-style: none; /* 箇条書きマークを消す */
padding: 0;
}
li {
display: inline-block; /* 横並びにして中央寄せ */
margin: 0 10px;
}
ナビゲーションメニューの中央寄せ
nav {
text-align: center;
}
nav a {
display: inline-block;
padding: 10px 20px;
margin: 0 5px;
text-decoration: none;
}
よくある失敗例と解決法
失敗例1: div要素に text-align を指定
/* これはdiv自体を中央寄せしない */
div {
text-align: center;
width: 200px;
background: lightblue;
}
正しい方法
/* 親要素に text-align を指定 */
.parent {
text-align: center;
}
.child {
width: 200px;
background: lightblue;
display: inline-block; /* インラインブロックにして中央寄せ対象に */
}
横方向にブロック要素を中央寄せ

margin: 0 auto の基本
画像やボタン、divなどのブロック要素を横方向に中央寄せしたい場合は、margin: 0 auto
を使う方法が最も確実です。
.center-block {
width: 200px; /* 幅の指定が必須 */
margin: 0 auto;
}
重要なポイント
- width(幅)の指定が必須:幅がないと中央寄せできません
- ブロック要素のみ有効:インライン要素では動作しません
具体的な使用例
画像の中央寄せ
img {
display: block; /* ブロック要素にする */
width: 300px;
margin: 0 auto;
}
ボタンの中央寄せ
.button-container {
text-align: center; /* インライン要素として中央寄せ */
}
/* または */
.button {
display: block;
width: 200px;
margin: 0 auto;
}
カード要素の中央寄せ
.card {
width: 320px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
}
max-width を使った レスポンシブ対応
固定幅だと小さい画面で困るので、max-width
を使うのがおすすめです。
.responsive-center {
max-width: 600px; /* 最大幅を指定 */
margin: 0 auto;
padding: 0 20px; /* 左右に余白を追加 */
}
メリット
- 大きい画面:指定した幅で中央寄せ
- 小さい画面:画面幅に合わせて自動調整
- スマホ対応も自動的にOK
よくある問題と解決法
問題1: margin: 0 auto が効かない
/* ダメな例:幅が指定されていない */
div {
margin: 0 auto; /* 効果なし */
}
/* 良い例:幅を指定 */
div {
width: 300px; /* または max-width: 300px */
margin: 0 auto;
}
問題2: 親要素からはみ出してしまう
/* ダメな例:親より大きな幅 */
.parent {
width: 400px;
}
.child {
width: 500px; /* 親より大きい */
margin: 0 auto;
}
/* 良い例:親に収まる幅 */
.child {
max-width: 350px; /* 親より小さく */
margin: 0 auto;
}
横+縦 両方中央に寄せるには?(Flexbox編)
Flexboxの基本概念
Flexboxは「柔軟なボックスレイアウト」の略で、現代CSSレイアウトの主役です。中央寄せだけでなく、複雑なレイアウトも簡単に作れます。
基本的な仕組み
- 親要素(コンテナ):
display: flex
を指定 - 子要素(アイテム):自動的にflex itemになる
- 軸の概念:メイン軸(横)とクロス軸(縦)
基本的な中央寄せ
.flex-container {
display: flex;
justify-content: center; /* 横方向(メイン軸)の中央寄せ */
align-items: center; /* 縦方向(クロス軸)の中央寄せ */
height: 300px; /* 高さがないと縦中央寄せが見えない */
}
HTML例
<div class="flex-container">
<div class="content">
中央に配置される要素
</div>
</div>
Flexboxのプロパティ詳細
justify-content(横方向の配置)
flex-start
:左寄せ(デフォルト)center
:中央寄せflex-end
:右寄せspace-between
:両端寄せ(間隔均等)space-around
:周囲に均等な余白
align-items(縦方向の配置)
stretch
:高さを揃える(デフォルト)flex-start
:上寄せcenter
:中央寄せflex-end
:下寄せbaseline
:ベースライン揃え
実用的なFlexboxパターン
画面全体の中央寄せ
html, body {
height: 100%;
margin: 0;
}
.full-center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* ビューポートの高さ */
}
カード要素の中央寄せ
.card-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 400px;
padding: 20px;
}
.card {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
max-width: 400px;
}
複数要素の横並び中央寄せ
.buttons-center {
display: flex;
justify-content: center;
align-items: center;
gap: 20px; /* 要素間の間隔 */
padding: 20px;
}
<div class="buttons-center">
<button>ボタン1</button>
<button>ボタン2</button>
<button>ボタン3</button>
</div>
Flexboxの便利な機能
flex-direction で方向変更
.vertical-center {
display: flex;
flex-direction: column; /* 縦方向に配置 */
justify-content: center;
align-items: center;
height: 300px;
}
flex-wrap で折り返し
.wrap-center {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap; /* 要素が多い時は折り返し */
gap: 10px;
}
align-self で個別調整
.flex-container {
display: flex;
justify-content: center;
align-items: flex-start;
height: 200px;
}
.special-item {
align-self: center; /* この要素だけ個別に中央寄せ */
}
Gridを使った中央寄せ

CSS Gridの特徴
CSS Gridは2次元レイアウトシステムで、Flexboxよりもさらに強力です。中央寄せに関しては、最もシンプルな記述で実現できます。
.grid-container {
display: grid;
place-items: center; /* 縦横同時に中央寄せ */
height: 300px;
}
Gridの利点
- 最短コード:
place-items: center
だけでOK - 2次元対応:縦と横を同時に制御
- 複雑なレイアウト:複数の要素を自在に配置
Gridプロパティの詳細
place-items(ショートハンド)
place-items: center; /* align-items: center; justify-items: center; と同じ */
個別指定
.grid-container {
display: grid;
justify-items: center; /* 横方向の配置 */
align-items: center; /* 縦方向の配置 */
height: 300px;
}
place-content(コンテンツ全体の配置)
.grid-container {
display: grid;
place-content: center; /* Grid全体を中央寄せ */
height: 100vh;
}
実用的なGridパターン
ロゴの中央配置
.logo-container {
display: grid;
place-items: center;
height: 200px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.logo {
max-width: 150px;
filter: brightness(0) invert(1); /* 白色に変更 */
}
モーダルウィンドウの中央配置
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
display: grid;
place-items: center;
}
.modal-content {
background: white;
padding: 40px;
border-radius: 10px;
max-width: 500px;
width: 90%;
}
複数要素のグリッド中央寄せ
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
place-items: center;
gap: 30px;
padding: 40px;
}
.feature-card {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 300px;
}
Grid vs Flexbox の使い分け
用途 | Grid | Flexbox |
---|---|---|
単純な中央寄せ | ◎ 最短コード | ○ 汎用的 |
1次元レイアウト | △ オーバースペック | ◎ 最適 |
2次元レイアウト | ◎ 本領発揮 | △ 複雑になる |
ブラウザ対応 | ○ IE11以外OK | ◎ IE10以上OK |
学習コスト | ○ やや高い | ◎ 比較的低い |
古典的な方法(position + transform)
position を使った中央寄せ
Flexbox や Grid が使えない環境では、position
と transform
を組み合わせる方法があります。
.position-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
仕組みの説明
top: 50%; left: 50%
:要素の左上角を中央に配置transform: translate(-50%, -50%)
:要素自身の幅・高さの半分だけ戻す- 結果:要素の中心が画面中央に
具体的な使用例
モーダルウィンドウの中央配置
.modal-backdrop {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
}
.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 40px;
border-radius: 8px;
max-width: 500px;
width: 90%;
}
ローディングスピナーの中央配置
.loading-container {
position: relative;
height: 200px;
}
.spinner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: translate(-50%, -50%) rotate(0deg); }
100% { transform: translate(-50%, -50%) rotate(360deg); }
}
この方法のメリット・デメリット
メリット
- 古いブラウザでも動作する
- 要素のサイズが不明でも中央寄せできる
position: fixed
でウィンドウ中央に固定可能
デメリット
- コードが複雑
position: absolute
により他の要素に影響を与える- レスポンシブ対応が難しい
ケーススタディ:よくあるレイアウトパターン

パターン1: ヒーローセクション
.hero {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
text-align: center;
}
.hero h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
.hero p {
font-size: 1.2rem;
margin-bottom: 2rem;
max-width: 600px;
}
.hero .cta-button {
padding: 15px 30px;
background: white;
color: #667eea;
border: none;
border-radius: 25px;
font-size: 1.1rem;
cursor: pointer;
transition: transform 0.3s ease;
}
.hero .cta-button:hover {
transform: translateY(-2px);
}
パターン2: カードグリッド
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 30px;
padding: 40px;
max-width: 1200px;
margin: 0 auto;
}
.card {
background: white;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
}
.card-content {
padding: 30px;
text-align: center;
}
.card-icon {
width: 60px;
height: 60px;
margin: 0 auto 20px;
background: #667eea;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 24px;
}
パターン3: フォーム中央配置
.form-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #f5f5f5;
padding: 20px;
}
.form-card {
background: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
.form-title {
text-align: center;
margin-bottom: 30px;
color: #333;
}
.form-group {
margin-bottom: 20px;
}
.form-input {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
}
.form-button {
width: 100%;
padding: 15px;
background: #667eea;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
パターン4: ナビゲーションメニュー
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 40px;
height: 70px;
background: white;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.logo {
font-size: 1.5rem;
font-weight: bold;
color: #667eea;
}
.nav-menu {
display: flex;
list-style: none;
margin: 0;
padding: 0;
gap: 30px;
}
.nav-menu a {
text-decoration: none;
color: #333;
font-weight: 500;
transition: color 0.3s ease;
}
.nav-menu a:hover {
color: #667eea;
}
.nav-button {
padding: 10px 20px;
background: #667eea;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
レスポンシブ対応の中央寄せ
モバイルファーストアプローチ
/* モバイル(デフォルト) */
.responsive-center {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
text-align: center;
}
/* タブレット */
@media (min-width: 768px) {
.responsive-center {
flex-direction: row;
justify-content: center;
text-align: left;
}
}
/* デスクトップ */
@media (min-width: 1024px) {
.responsive-center {
max-width: 1200px;
margin: 0 auto;
padding: 40px;
}
}
コンテナクエリ対応
.card-container {
container-type: inline-size;
}
.card {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
@container (min-width: 300px) {
.card {
flex-direction: row;
text-align: left;
}
}
ビューポート単位の活用
.full-screen-center {
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
height: 100dvh; /* 動的ビューポート高さ */
}
.content {
max-width: 90vw;
max-height: 90vh;
padding: clamp(20px, 5vw, 40px);
}
よくあるトラブルと解決方法
トラブル1: Flexboxで高さが効かない
問題のコード
.container {
display: flex;
justify-content: center;
align-items: center;
/* height: 300px; ← これがないと縦中央寄せされない */
}
解決方法
.container {
display: flex;
justify-content: center;
align-items: center;
height: 300px; /* 高さを明示的に指定 */
/* または min-height: 100vh; で画面全体の高さ */
}
トラブル2: margin: 0 auto が効かない
問題のコード
.element {
margin: 0 auto; /* 幅が指定されていないので効果なし */
background: lightblue;
}
解決方法
.element {
width: 300px; /* 幅を指定 */
margin: 0 auto;
background: lightblue;
}
/* または */
.element {
max-width: 300px; /* レスポンシブ対応 */
margin: 0 auto;
background: lightblue;
}
トラブル3: テキストが中央寄せされない
問題のコード
div {
text-align: center; /* div自体には効果なし */
width: 200px;
background: lightgreen;
}
解決方法
.parent {
text-align: center; /* 親要素に指定 */
}
.child {
width: 200px;
background: lightgreen;
display: inline-block; /* または margin: 0 auto; */
}
トラブル4: position中央寄せで要素が切れる
問題のコード
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 90vw; /* 画面幅の90%だと大きすぎる場合がある */
height: 90vh;
}
解決方法
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 500px; /* 最大幅を制限 */
max-height: 80vh; /* 最大高さを制限 */
width: 90%;
overflow-y: auto; /* 必要に応じてスクロール */
}
トラブル5: IE11での互換性問題
IE11で動かない場合の対処法
/* モダンブラウザ用 */
.modern {
display: flex;
justify-content: center;
align-items: center;
}
/* IE11用フォールバック */
.ie11-fallback {
text-align: center;
}
.ie11-fallback::before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.ie11-content {
display: inline-block;
vertical-align: middle;
text-align: left;
}
パフォーマンスと最適化
効率的なCSS設計
避けるべきパターン
/* 重いセレクタ(避ける) */
div div div .center {
display: flex;
justify-content: center;
}
/* 不要な !important(避ける) */
.center {
text-align: center !important;
}
/* 過度なネスト(避ける) */
.container .wrapper .content .item .text {
text-align: center;
}
推奨パターン
/* シンプルなセレクタ */
.center {
display: flex;
justify-content: center;
align-items: center;
}
/* 再利用可能なユーティリティクラス */
.text-center { text-align: center; }
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
.mx-auto { margin-left: auto; margin-right: auto; }
GPU加速の活用
transform使用時の最適化
.gpu-optimized {
transform: translate3d(-50%, -50%, 0); /* GPU加速 */
will-change: transform; /* ブラウザに変更予告 */
}
/* アニメーション終了時は will-change を削除 */
.animation-complete {
will-change: auto;
}
CSS論理プロパティの活用
/* 従来の方法 */
.traditional {
margin-left: auto;
margin-right: auto;
}
/* 論理プロパティ(国際化対応) */
.logical {
margin-inline: auto; /* 左右のマージン */
}
/* 縦書きにも対応 */
.vertical-text {
writing-mode: vertical-rl;
margin-block: auto; /* 上下のマージン */
}
実践的なユーティリティクラス集

基本的な中央寄せクラス
/* テキスト中央寄せ */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }
/* 水平中央寄せ */
.mx-auto { margin-left: auto; margin-right: auto; }
.ml-auto { margin-left: auto; }
.mr-auto { margin-right: auto; }
/* Flexbox中央寄せ */
.flex { display: flex; }
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
.flex-col { flex-direction: column; }
.justify-center { justify-content: center; }
.items-center { align-items: center; }
/* Grid中央寄せ */
.grid { display: grid; }
.place-center { place-items: center; }
.place-content-center { place-content: center; }
レスポンシブ対応クラス
/* モバイルファースト */
.text-center { text-align: center; }
/* タブレット以上 */
@media (min-width: 768px) {
.md\:text-left { text-align: left; }
.md\:flex-row { flex-direction: row; }
}
/* デスクトップ以上 */
@media (min-width: 1024px) {
.lg\:justify-between { justify-content: space-between; }
.lg\:max-w-4xl { max-width: 1024px; }
}
空間制御クラス
/* 間隔 */
.gap-1 { gap: 0.25rem; }
.gap-2 { gap: 0.5rem; }
.gap-4 { gap: 1rem; }
.gap-8 { gap: 2rem; }
/* パディング */
.p-4 { padding: 1rem; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-4 { padding-top: 1rem; padding-bottom: 1rem; }
/* マージン */
.m-4 { margin: 1rem; }
.mx-4 { margin-left: 1rem; margin-right: 1rem; }
.my-4 { margin-top: 1rem; margin-bottom: 1rem; }
新しいCSS機能と中央寄せ
CSS Subgrid
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.grid-item {
display: grid;
grid-template-rows: subgrid; /* 親のグリッドを継承 */
place-items: center;
}
CSS Container Queries
.card-container {
container-type: inline-size;
}
.card {
display: flex;
flex-direction: column;
align-items: center;
}
/* コンテナの幅に応じて中央寄せ方法を変更 */
@container (min-width: 400px) {
.card {
flex-direction: row;
justify-content: space-between;
}
}
CSS :has() セレクタ
/* 画像を含む場合は特別な中央寄せ */
.card:has(img) {
display: grid;
place-items: center;
text-align: center;
}
/* 複数の子要素がある場合の調整 */
.container:has(> * + *) {
display: flex;
justify-content: space-evenly;
align-items: center;
}
デバッグとトラブルシューティング
開発者ツールでの確認方法
Chrome DevToolsでの確認手順
- 要素を右クリック → 「検証」
- Elementsタブで該当要素を選択
- Stylesパネルで適用されているCSSを確認
- Computedタブで実際の計算値を確認
よく確認すべきプロパティ
display
: flex, grid, block, inline-blockのどれかwidth
/height
: 数値が設定されているかmargin
: auto が適用されているかposition
: static, relative, absolute, fixedのどれか
視覚的デバッグテクニック
/* 一時的な境界線でレイアウトを可視化 */
* {
outline: 1px solid red !important;
}
/* 特定の要素のみ可視化 */
.debug {
outline: 2px solid blue;
background: rgba(255, 0, 0, 0.1);
}
/* Flexboxの軸を可視化 */
.flex-debug::before {
content: '主軸→';
position: absolute;
top: 0;
left: 0;
background: yellow;
padding: 2px;
font-size: 10px;
}
一般的な問題の診断
チェックリスト
/* 1. display プロパティは適切か? */
.element { display: flex; } /* または grid, block など */
/* 2. 幅や高さは指定されているか? */
.element { width: 100px; height: 100px; }
/* 3. 親要素の制約はないか? */
.parent { overflow: hidden; position: relative; }
/* 4. z-index の問題はないか? */
.element { z-index: 1; position: relative; }
/* 5. box-sizing は考慮されているか? */
* { box-sizing: border-box; }
まとめ
CSSの中央寄せは、適切な方法を選択すれば誰でも簡単にマスターできます。
方法別まとめ
テキストの中央寄せ
.text-center {
text-align: center; /* 最もシンプル */
}
横方向のブロック中央寄せ
.horizontal-center {
max-width: 600px;
margin: 0 auto; /* 王道の方法 */
}
完全な中央寄せ(現代的な方法)
/* Flexbox(推奨) */
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
/* Grid(最短) */
.grid-center {
display: grid;
place-items: center;
}
使い分けの指針
状況 | 推奨方法 | 理由 |
---|---|---|
テキストのみ | text-align: center | シンプルで確実 |
画像やボタン | margin: 0 auto | 軽量で互換性が高い |
カード要素 | Flexbox | 柔軟性が高い |
複雑なレイアウト | Grid | 2次元制御が可能 |
旧ブラウザ対応 | position + transform | IE11でも動作 |
覚えておきたいポイント
成功のコツ
- 要素の種類を理解する:インライン、ブロック、インラインブロック
- 親子関係を意識する:どの要素に何を指定するか
- 高さの指定を忘れない:縦中央寄せには高さが必要
- レスポンシブを考慮する:max-width を活用
よくある間違い
- 幅を指定せずに
margin: 0 auto
を使う - 高さを指定せずに
align-items: center
を使う - インライン要素に
margin: 0 auto
を使う - ブロック要素に
text-align: center
を使う
デバッグのコツ
- 開発者ツールでプロパティを確認
- 一時的な背景色や境界線で要素を可視化
- 1つずつプロパティを追加して動作確認
コメント