LoginSignup
27
23

More than 5 years have passed since last update.

複数行のテキストを縦横中央揃えにする方法

Last updated at Posted at 2014-08-31

横の中央揃えは margin使えば簡単なんだけど、縦の中央揃えは
何かと厄介だったりします。

親要素のサイズが決まっている場合は、以下の方法が簡単です。

html

<p class="container">
    <span class="text">ここに複数行テキスト<br/>が入ります。</span>
</p>

css

.container{
    width: 100px;
    height: 100px;
}

.container .text{
    display: table-cell;
    width: 100px;
    height: 100px;
    text-align: center;
    vertical-align: middle;
}

このように、
html側では中央揃えしたい要素をinline要素として置き、
css側ではその要素に対して 親要素と同じサイズ指定をした上で

display: table-cell;
text-align: center;
vertical-align: middle;

の3つのプロパティを設定することで実現できます。

27
23
0

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
23