コンテンツ枠内のボタンを一番下に固定配置するCSS6パターン

はじめに

コンテンツ内に横並びのメニューを作った時、例えば「詳しくはコチラ」のようなボタンがテキスト量によってズレて表示されてしまっていたので、それを回避する方法を色々考えてみました。

メニュー枠を作るhtmlはいくつかありますが、ボタンの固定については、大きく「abusoluteで固定する方法」と「flexboxを使う方法」の2種類を紹介します。
どちらも見た目には大差無いと思います。

仕上がりイメージ

テスト画像

テキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト

詳しくはこちら

やりたい事

  • ボタンは枠の下部に固定
  • ボタンサイズにwidthを指定した上で、ボタンを左右中央に配置させる
  • テキスト量が変わっても、それぞれの枠の縦幅は同じ高さに揃うようにする
  • 枠はheight固定しない

メニュー枠はflexで横並びにしています。

align-items: stretch;を指定することで、テキスト量が変わっても枠の高さが揃うようになります。

【参考】flexのCSS

  1. .box {//実際のソースでは別のクラス名を使用しています
  2.  display:flex;
  3.  align-items: stretch;
  4.  justify-content: space-between;
  5. }

目次

absoluteで配置する(divを使用)

リンクボタンをpositon:absoluteで、枠の下部に固定させる方法です。

単純に下部に固定させるだけだと、下の右側の枠のようにテキストとボタンが重なってしまいます。

テスト画像

テキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト

詳しくはこちら

HTML

  1. <div class="inner_box">
  2. <div class="test">
  3. <img src="画像パス" alt="テスト画像">
  4. <p>テキストテキスト</p>
  5. <a href="#" class="button">詳しくはこちら</a>
  6. </div>
  7. <div class="test">
  8. <img src="画像パス" alt="テスト画像">
  9. <p>テキストテキストテキストテキストテキストテキスト</p>
  10. <a href="#" class="button">詳しくはこちら</a>
  11. </div>
  12. <div class="test">
  13. <img src="画像パス" alt="テスト画像">
  14. <p>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</p>
  15. <a href="#" class="button">詳しくはこちら</a>
  16. </div>
  17. </div>

CSS

  1. .inner_box {
  2. display:flex;
  3. align-items: stretch;
  4. justify-content: space-between;
  5. }
  6. .test {
  7. position: relative;
  8. width: 30%;
  9. background: #f5f5f5;
  10. border: 1px solid #ddd;
  11. padding: 10px;
  12. box-sizing:border-box;
  13. }
  14. a.button {
  15. position: absolute;
  16. bottom: 10px;
  17. display: block;
  18. width: 100px;
  19. background: #338DC9;
  20. color: #fff;
  21. text-decoration: none;
  22.  
  23. font-size: 12px;
  24. text-align: center;
  25. left: 50%;
  26. line-height: 30px;
  27. transform: translateX( -50%);
  28. }

対策

ボタンの高さと同じだけ、CSSにpadding-bottomを追加します。

今回の場合、ボタンのline-heightが30pxなので、CSSの.testにpadding-bottom:30pxを追記するか、padding: 10px 10px 30px 10px;と修正します。

padding-bottom:30pxと記述する場合は、必ずpadding: 10px;より下の行に追記して下さい。

なお、今回のパターンでは、リンクボタンを左右中央に配置するためにleft: 50%;と設定した上でtransform: translateX( -50% );と設定しています(ボタンが改行されてしまう場合はwhite-space:nowarp;を追記してみて下さい)。

CSS(.test部分のみ)

  1. .test {
  2.  position: relative;
  3.  width: 30%;
  4.  background: #f5f5f5;
  5.  border: 1px solid #ddd;
  6.  padding: 10px;
  7.  padding-bottom: 30px;
  8.  box-sizing:border-box;
  9. }
テスト画像

テキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト

詳しくはこちら

これで、ボタンとテキストが重ならなくなりますが、更新(メンテナンス)時に以下の注意点があります。

問題点

ボタンサイズを変更する場合、.testのpadding-bottomもサイズを変更する必要がある(更新時に面倒)。

absoluteで配置する(ul liを使用)

基本的には上記と同じです。

HTMLにdivを使わずulliを使うので、divが入れ子にならずにソースが分かりやすい(かもしれない)と思っています。

一方で、liで画像、テキスト、リンクボタンを区切るので、ul(この場合.test)にlist-style:noneが必要になります。

また、ボタンの入るli(この場合li:last-of-type)にpadding-bottom:30pxを記入します。

仕上がりイメージ

  • テスト画像
  • テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト
  • 詳しくはこちら

HTML

  1. <div class="inner_box">
  2.  <ul class="test">
  3.   <li><img src="画像パス" alt="テスト画像"></li>
  4.   <li>テキストテキスト</li>
  5.   <li><a href="#" class="button">詳しくはこちら</a></li>
  6.  </ul>
  7.  <ul class="test">
  8.   <li><img src="画像パス" alt="テスト画像"></li>
  9.   <li>テキストテキストテキストテキストテキストテキスト</li>
  10.   <li><a href="#" class="button">詳しくはこちら</a></li>
  11.  </ul>
  12.  <ul class="test">
  13.   <li><img src="画像パス" alt="テスト画像"></li>
  14.   <li>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</li>
  15.   <li><a href="#" class="button">詳しくはこちら</a></li>
  16.  </ul>
  17. </div>

CSS

  1. .inner_box {
  2.  display:flex;
  3.  align-items: stretch;
  4.  justify-content: space-between;
  5. }
  6. .test {
  7.  position: relative;
  8.  width: 30%;
  9.  background: #f5f5f5;
  10.  border: 1px solid #ddd;
  11.  padding: 10px;
  12.  padding-bottom: 30px;
  13.  box-sizing:border-box;
  14.  list-style:none;
  15. }
  16. a.button {
  17.  position: absolute;
  18.  bottom: 10px;
  19.  display: block;
  20.  width: 100px;
  21.  background: #338DC9;
  22.  color: #fff;
  23.  text-decoration: none;
  24.  font-size: 12px;
  25.  text-align: center;
  26.  left: 50%;
  27.  line-height: 30px;
  28.  transform: translateX( -50% );
  29. }

余談ですが、line-heightの関係でテキストとボタンが詰まっている等の問題があれば、CSSを以下のようにしても良いかと思います(実際の私のソースでは以下のように設定しています)。

CSS(.test関連部分のみ)

  1. .test {
  2.  position: relative;
  3.  width: 30%;
  4.  background: #f5f5f5;
  5.  border: 1px solid #ddd;
  6.  padding: 10px;
  7.  box-sizing:border-box;
  8.  list-style:none;
  9. }
  10. .test li:last-of-type {
  11.  padding-bottom: 30px;
  12. }

absoluteで配置する(dl dt dd を使用)

基本的な構造は上記と同じです。

ulliを使用したパターンより、CSSが若干シンプルになる印象です。

dtddは1:1でなくて良いので、dt1つに対してddを2つ設定しています。

仕上がりイメージ

テスト画像
テキストテキスト
詳しくはこちら
テスト画像
テキストテキストテキストテキストテキストテキスト
詳しくはこちら
テスト画像
テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト
詳しくはこちら

HTML

  1. <div class="inner_box">
  2.  <dl class="test">
  3.   <dt><img src="画像パス" alt="テスト画像"></dt>
  4.   <dd>テキストテキスト</dd>
  5.   <dd><a href="#" class="button01">詳しくはこちら</a></dd>
  6.  </dl>
  7.  <dl class="test03">
  8.   <dt><img src="画像パス" alt="テスト画像"></dt>
  9.   <dd>テキストテキストテキストテキストテキストテキスト</dd>
  10.   <dd><a href="#" class="button01">詳しくはこちら</a></dd>
  11.  </dl>
  12.  <dl class="test03">
  13.   <dt><img src="画像パス" alt="テスト画像"></dt>
  14.   <dd>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</dd>
  15.   <dd><a href="#" class="button01">詳しくはこちら</a></dd>
  16.  </dl>
  17. </div>

CSS

  1.  .inner_box {
  2.  display:flex;
  3.  align-items: stretch;
  4.  justify-content: space-between;
  5. }
  6. .test {
  7.  position: relative;
  8.  width: 30%;
  9.  background: #f5f5f5;
  10.  border: 1px solid #ddd;
  11.  padding: 10px;
  12.  padding-bottom: 30px;
  13.  box-sizing:border-box;
  14. }
  15. a.button {
  16.  position: absolute;
  17.  bottom: 10px;
  18.  display: block;
  19.  width: 100px;
  20.  background: #338DC9;
  21.  color: #fff;
  22.  text-decoration: none;
  23.  font-size: 12px;
  24.  text-align: center;
  25.  left: 50%;
  26.  line-height: 30px;
  27.  transform: translateX( -50%);
  28. }

flexboxで配置する(divを使用)

2つ目の大きな方法はflexboxでリンクボタンを下部固定する方法です。

posision:abusoluteの場合と異なり、padding-bottomで調整する必要がなくなります。

具体的には、inner_box内の子要素.testにもflexを設定します(詳細は下記ソースをご覧ください)。

CSS(.testのみ)

  1. .test{
  2.  display:flex;
  3.  align-items: flex-start;
  4.  justify-content: flex-start;
  5.  flex-direction: column;
  6. }

flexboxを使いつつ、flex-direction: column;を設定する事でコンテンツ内の画像、テキスト、リンクボタンを縦並びにします。

こちらの方法もdivul lidl dt ddを使用した方法を紹介します。

こちらの方がメンテナンス性は良さそうですが、下記3パターンそれぞれでCSSの記述が若干異なるので若干汎用性に欠けるかなぁ、というのが個人的見解です。

HTML

  1. <div class="inner_box">
  2.  <div class="test">
  3.   <img src="画像パス" alt="テスト画像">
  4.   <p>テキストテキスト</p>
  5.   <a href="#" class="button02">詳しくはこちら</a>
  6.  </div>
  7.  <div class="test04_box">
  8.   <img src="画像パス" alt="テスト画像">
  9.   <p>テキストテキストテキストテキストテキストテキスト</p>
  10.   <a href="#" class="button02">詳しくはこちら</a>
  11.  </div>
  12.  <div class="test04_box">
  13.   <img src="画像パス" alt="テスト画像">
  14.   <p>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</p>
  15.   <a href="#" class="button02">詳しくはこちら</a>
  16.  </div>
  17. </div>

CSS

  1. .inner_box {
  2.  display:flex;
  3.  align-items: stretch;
  4.  justify-content: space-between;
  5. }
  6. .test {
  7.  position: relative;
  8.  width: 30%;
  9.  background: #f5f5f5;
  10.  border: 1px solid #ddd;
  11.  padding: 10px;
  12.  box-sizing:border-box;
  13.  display:flex;
  14.  align-items: flex-start;
  15.  justify-content: flex-start;
  16.  flex-direction: column;
  17. }
  18. a.button {
  19.  display: block;
  20.  margin: auto auto 0 auto;
  21.  width: 100px;
  22.  background: #338DC9;
  23.  color: #fff;
  24.  text-decoration: none;
  25.  font-size: 12px;
  26.  text-align: center;
  27.  line-height: 30px;
  28. }

今回のパターンでボタンを下部固定するにはdisplay:blockを設定した上で、margin-topをautoに設定します。

また、左右中央に配置するにはmargin-leftmargin-rightをautoに設定する必要があります。

そのため、a.buttonの箇所はmargin: auto auto 0 auto;としています。

できあがったのもの

テスト画像

テキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキスト

詳しくはこちら
テスト画像

テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト

詳しくはこちら

flexboxで配置する(ul liを使用)

HTML

  1. <div class="inner_box">
  2.  <ul class="test">
  3.   <li><img src="画像パス" alt="テスト画像"></li>
  4.   <li>テキストテキスト</li>
  5.   <li class="button"><a href="#">詳しくはこちら</a></li>
  6.  </ul>
  7.  <ul class="test">
  8.   <li><img src="画像パス" alt="テスト画像"></li>
  9.   <li>テキストテキストテキストテキストテキストテキスト</li>
  10.   <li class="button"><a href="#">詳しくはこちら</a></li>
  11.  </ul>
  12.  <ul class="test">
  13.   <li><img src="画像パス" alt="テスト画像"></li>
  14.   <li>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</li>
  15.   <li class="button"><a href="#" >詳しくはこちら</a></li>
  16.  </ul>
  17. </div>

CSS

  1. .inner_box {
  2.  display:flex;
  3.  align-items: stretch;
  4.  justify-content: space-between;
  5. }
  6. .test {
  7.  position: relative;
  8.  width: 30%;
  9.  background: #f5f5f5;
  10.  border: 1px solid #ddd;
  11.  padding: 10px;
  12.  box-sizing:border-box;
  13.  list-style:none;
  14.  display:flex;
  15.  align-items: flex-start;
  16.  justify-content: flex-start;
  17.  flex-direction: column;
  18. }
  19. .button {
  20.  display: block;
  21.  margin: auto auto 0 auto;
  22. }
  23. .button a {
  24.  display: block;
  25.  width: 100px;
  26.  background: #338DC9;
  27.  color: #fff;
  28.  text-decoration: none;
  29.  font-size: 12px;
  30.  text-align: center;
  31.  line-height: 30px;
  32. }

ulliを使用するので、abusoluteのパターン同様、.testにlist-style:none; を設定します。

また、今回のパターンの場合、リンクボタンを下部固定および左右中央に配置させるためには、aではなく、ボタンの入るli<li class="button">と設定する必要があります。

できあがったもの

  • テスト画像
  • テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト
  • 詳しくはこちら

flexboxで配置する(dl dt dd を使用)

HTML

  1. <div class="inner_box">
  2.  <dl class="test">
  3.   <dt><img src="画像パス" alt="テスト画像"></dt>
  4.   <dd>テキストテキスト</dd>
  5.   <dd class="button"><a href="#">詳しくはこちら</a></dd>
  6.  </dl>
  7.  <dl class="test">
  8.   <dt><img src="画像パス" alt="テスト画像"></dt>
  9.   <dd>テキストテキストテキストテキストテキストテキスト</dd>
  10.   <dd class="button"><a href="#">詳しくはこちら</a></dd>
  11.  </dl>
  12.  <dl class="test">
  13.   <dt><img src="画像パス" alt="テスト画像"></dt>
  14.   <dd>テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト</dd>
  15.   <dd class="button"><a href="#">詳しくはこちら</a></dd>
  16.  </dl>
  17. </div>

CSS

  1. .inner_box {
  2.  display:flex;
  3.  align-items: stretch;
  4.  justify-content: space-between;
  5. }
  6. .test {
  7.  position: relative;
  8.  width: 30%;
  9.  background: #f5f5f5;
  10.  border: 1px solid #ddd;
  11.  padding: 10px;
  12.  box-sizing:border-box;
  13.  display:flex;
  14.  align-items: flex-start;
  15.  justify-content: flex-start;
  16.  flex-direction: column;
  17. }
  18. .button {
  19.  display: block;
  20.  margin: auto auto 0 auto;
  21. }
  22. .button a {
  23.  display: block;
  24.  width: 100px;
  25.  background: #338DC9;
  26.  color: #fff;
  27.  text-decoration: none;
  28.  font-size: 12px;
  29.  text-align: center;
  30.  line-height: 30px;
  31. }

基本的には上記ulliを使用したパターンと同じです。

dtddは1:1でなくて良いらしいので、ddを2つ作ってリンクボタンが入るものに<dd class="button">と設定しています。

ulliのパターンと同様に<dd class="button">にリンクボタンが下部固定および左右中央配置させる設定をしています。

できあがったもの

テスト画像
テキストテキスト
詳しくはこちら
テスト画像
テキストテキストテキストテキストテキストテキスト
詳しくはこちら
テスト画像
テキストテキストテキストテキストテキストテキストテキストテキストテキストテキスト
詳しくはこちら