0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CSSで中央揃え:テキストを水平・垂直方向に中央配置する

0
Posted at

😄この投稿は、codingeverybody.jpのコンテンツをもとに作成しています。

CSSでテキストを水平方向(横方向)に中央揃えする方法

# 左右に余白がある要素:text-align: center

---- HTML ----
<h5>左右に余白がある要素</h5>

---- CSS ----
h5 {
    background-color: orange; /* 左右に余白のある要素です */
    text-align: center;       /* この要素の横幅を基準にテキストを中央揃えにします */
}

# 左右に余白のない要素:左右に余白のある親要素に対して、text-align: centerを指定する

---- HTML ----
<div class="container">
    <span>左右に余白のない要素</span>
</div>

---- CSS ----
container {
    background-color: orange; /* 左右に余白のある要素です */
    text-align: center;       /* 左右に余白のある親要素の横幅を基準に、子要素のテキストを中央揃えにします */
}
.container span {
    background-color: pink;   /* 左右に余白のない要素です */
}

# 左右に余白のない要素:左右に余白のある親要素に対して、display: flexと justify-content: center

---- HTML ----
<div class="container">
    <span>左右に余白のない要素</span>
</div>

---- CSS ----
.container {
    background-color: orange; /* 左右に余白のある要素です */
    display: flex;
    justify-content: center;  /* 子要素を水平方向(横方向)に中央揃えにします */
}
.container span {
    background-color: pink;   /* 左右に余白のない要素です */
}

CSSでテキストを垂直方向(縦方向)に中央揃えする方法

# 自分自身のテキストコンテンツを中央揃えにする方法:line-height

---- HTML ----
<h5>line-heightを使う</h5>

---- CSS ----
h5 {
    background-color: orange;
    line-height: 200px; /* この要素のテキストを基準に、上下方向で中央揃えにします */
}

# 自要素のテキストコンテンツを中央揃えにする方法:display: flexとalign-items: center

---- HTML ----
<h5>flexを使う</h5>

---- CSS ----
h5 {
    background-color: orange;
    height: 200px;
    display: flex;
    align-items: center; /* 子要素を垂直方向(縦方向)に中央揃えにします */
}

# 自要素のテキストコンテンツを中央揃えにする方法:display: table-cellとvertical-align: middle

---- HTML ----
<h5>table cellを使う</h5>

---- CSS ----
h5 {
    background-color: orange;
    height: 200px;
    display: table-cell;
    vertical-align: middle; /* テキストを垂直方向(縦方向)に中央揃えにします */
}

# 親要素に display: flexとalign-items: center

---- HTML ----
<div class="container">
    <span>子要素</span>
</div>

---- CSS ----
.container {
    background-color: orange;
    height: 200px;
    display: flex;
    align-items: center; /* 子要素を垂直方向(縦方向)に中央揃えにします */
}
.container span {
    background-color: pink;
}

# 親要素と子要素に positionとtransform プロパティを使う方法

---- HTML ----
<div class="container">
    <span>子要素</span>
</div>

---- CSS ----
.container {
    background-color: orange;
    height: 200px;
    position: relative;
}
.container span {
    background-color: pink;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

CSSでテキストを水平方向(横方向)および垂直方向(縦方向)に中央揃えする方法

# 自要素のテキストコンテンツを中央揃えにする方法:line-heightとtext-align: center

---- HTML ----
<h5>line-heightを使う</h5>

---- CSS ----
h5 {
    background-color: orange;
    text-align: center;   /* この要素の横幅を基準にテキストを中央揃えにします */
    line-height: 200px;   /* この要素のテキストを基準に上下方向で中央揃えにします */
}

# 左右に余白のある親要素に対して、display: flex、justify-content: center、およびalign-items: center

---- HTML ----
<div class="container">
    <span>子要素</span>
</div>

---- CSS ----
.container {
    background-color: orange;
    height: 200px;
    justify-content: center; /* 子要素を水平方向(横方向)に中央揃えにします */
    align-items: center;     /* 子要素を垂直方向(縦方向)に中央揃えにします */
}
.container span {
    background-color: pink;
}

参考資料

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?