2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

親が display:flex; だと子要素のdisplayをtable-cellにできない

Posted at

#前提

これは自分用のメモに近しいものです。一瞬戸惑ったので次すぐできるように。

#現象

単純な話です。

<div class="wrapper">
  <div class="btn">BUTTON</div>
</div>
.wrapper {
  display: flex;
  flex-direction: column;
}


.btn {
  width: 7rem;
  height: 2.6rem;
  border: solid 0.2rem #bd4a4f;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  margin: auto 1rem;
}

.btnの中身のテキストを中央揃えにするためにdisplay:table-cellを適用したいんですが、親要素がflexだと子要素のtable-cellが解除されるのか、うまく縦方向の中央揃えができなくなります。

対処法

ボタン自体に最低限のラッパーをつけて保護してあげるだけ。

<div class="wrapper">
  <div class="btn-wrapper">
    <div class="btn">BUTTON</div>
  </div>
</div>
.btn-wrapper {
  width: 7rem;
  height: 2.6rem; /*必要ないけど、btnにmargin autoを適用していた場合はこっちに移す。それに応じて高さと幅も必要になる。*/
  margin: auto 1rem; 
}
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?