中央ぞろえにしたい

上下中央揃え

おそらく最も手軽な方法は画像等の親要素にdisplay: flex;を設定する方法です。(Sample)

height: 100vh;という指定は高さをウィンドウサイズと同じにするためです。この指定がないとdivタグの高さが写真の高さのため左右中央にしか見えません。

<!DOCTYPE html>
<html>
    <head>
        <title>in-the-center1.html</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .parent {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
        </style>
    </head>
    <body>
        <div class="parent">
            <img src="https://picsum.photos/id/237/200/300" width="200" alt="黒犬の画像">
        </div>
    </body>
</html>

上下中央揃え(Sample)

display: flex;を使わない方法です。(Sample)

<!doctype html>
<html>
    <head>
        <title>in-the-center2.html</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
        <style>
            .wrapper {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
            }
        </style>
    </head>
    <body>
        <div class="wrapper">
            <img src="https://picsum.photos/200">
        </div>
    </body>
</html>

水平方向の中央揃え

インライン要素か、ブロック要素かによって中央ぞろえにする方法が異なります。

インライン要素では、中央揃えにしたい要素の親要素にtext-align: center;を指定します。

インライン要素の代表例は以下のタグです。

<a>、<img>、<input>など

ブロック要素では、中央揃えにしたい要素そのものにmargin: 0 auto;を指定します。

ブロック要素の代表例は以下のタグです。

<div>、<form>、<h1>-<h6>、<p>、<table>など

インライン要素を中央ぞろえにする場合(Sample)

<!DOCTYPE html>
<html>
    <head>
        <title>text-align-center.html</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .center{
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="center">
            <button>ボタン</button>
            <img src="https://picsum.photos/200/300" alt="Lorem Picsum">
        </div>
    </body>
</html>

ブロック要素を中央ぞろえにする場合(Sample)

この場合、幅がないと中央揃えにならないためwidth: 250pxとしています。

<html>
    <head>
        <title>margin-0-auto.html</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .center{
                width: 250px; 
                margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div class="center">
            <h2>フォームの中央揃え</h2>
            <form>
                <input type="text">
                <button type="submit">submit</button>
            </form>
        </div>
    </body>
</html>

Bootstrap4を使って中央ぞろえにする場合(Sample)

横方向のマージンをautoにすることで中央揃えにできます。

この場合も横幅が必要です。


<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 

        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> 

        <title>centered.html</title>
    </head>
    <body>
        <div class="mx-auto" style="width: 200px;">
            中央揃え
        </div>
    </body>
</html>

中央ぞろえにしたい 最後までお読みいただきありがとうございます。