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?

More than 3 years have passed since last update.

カード型の中身が増減してもボタンの位置を揃えたい

Last updated at Posted at 2022-01-23

やりたいこと

WordPressでよく出てくるカード型の一覧画面。
投稿する内容によって高さが変わったりしても、ボタンの位置や高さが揃うようにしたい

良くない例

悪い例.png

<!-- ulは端折る -->

<li class="bl_card">
<a href="">

<figure class="bl_card_imgWrapper"></figure>

<div class="bl_card_body">
<h2 class="bl_card_ttl">タイトルが入ります。</h2>
<p class="bl_card_txt">
本文が入ります。本文が入ります。<br>
本文が入ります。本文が入ります。<br>
本文が入ります。本文が入ります。
</p>
<p class="el_btn">詳細はこちら</p>
</div>
<!-- /.bl_card_body -->

</a>
</li>
.bl_card {
  width: 320px;
  min-height: 420px;
}
.bl_card > a {
  display: block;
  height: 100%;
}
.bl_card_imgWrapper {
  width: 100%;
  height: 240px;
  background: #999;
}
.bl_card_body {
  padding: 20px;
  background: #fff;
}
.bl_card_ttl {
  margin-bottom: 16px;
}
.el_btn {
  width: 200px;
  height: 46px;
  background: #999;
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 20px auto 0;
}

修正後

HTMLは同じなので、CSSだけ変更

.bl_card {
  width: 320px;
  min-height: 420px;
}
.bl_card > a {
  display: block;
  height: 100%;
  
  /* ①ここから追加 */
  background: #fff;
  padding-bottom: 64px;
  position: relative;
}
.bl_card_imgWrapper {
  width: 100%;
  height: 240px;
  background: #999;
}
.bl_card_body {
  /* padding: 20px; */ 
  padding: 20px 20px 8px;  /*←padding-bottomを調整*/
  background: #fff;
}
.bl_card_ttl {
  margin-bottom: 16px;
}
.el_btn {
  width: 200px;
  height: 46px;
  background: #999;
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  /* margin: 20px auto 0;  ←削除 */
  
  /* ②ここから追加 */
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
}

●結果
修正後.png

考え方

  • 背景の色をカード全体につける
  • ボタンを配置するための余白を取る
  • その余白の部分にposition: absolute;でボタンを配置する
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?