LoginSignup
27

More than 5 years have passed since last update.

要素を横も縦も中央寄せする方法

Posted at

要素を縦も横も中央寄せする方法。
tableでも同じように出来るけど、他の要素でやりたい場合。
この方法は、外側のタグのサイズに依存する場合に便利。

all_center_baseで縦横確保して、all_center_innerで位置調整をしている。
もし、サイズが決まっているのであれば、all_center_innerを付けている要素にサイズを設定すればall_center_baseタグは要らない。

この方法の他にもやり方はあるので、場合によって使い分ける。

.all_center_base {
    display: table;
    width: 100%;
    height: 100%;
}
.all_center_inner {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
<div class="all_center_base">
    <div class="all_center_inner">
        中央寄せしたい要素
    </div>
</div>

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27