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

横幅を最低●●pxはほしいけど文字数によって幅が広がるボタン

Posted at

例えば少ない文字数でも最低240pxの幅は確保したいけど、文字数が多くなったときには横幅を変動させるようにしたいといった場合

ボタンのテキストをカスタムフィールドとかで出力するときとかに使えそうなのでメモ。

<a href="" class="btn">詳細はこちら</a>
.btn {
  min-width: 240px; /* 最低240pxを確保 */
  height: 40px;
  background: #999;
  border-radius: 4px;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  padding: 0 8px; /* テキストがキツキツにならないように */
/* ここから文字の中央寄せ */
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

幅可変のボタン.png
2番めのようにテキストが長くなっても幅が広がります。

●ボタンを中央寄せにしたいとき

divタグとかで囲んで、text-align: center;

<div class="btn-wrapper">
  <a href="" class="btn">詳細はこちら</a>
</div>
.btn-wrapper {
  text-align: center;
}
.btn {
上の.btn同じ
}
0
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
0
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?