Skip to content Skip to sidebar Skip to footer

Align Link To Bottom Of Card In Bootstrap 4

I would like to align links to the bottom center of cards. I tried adding the classes d-flex and flex-column to .card-body and mt-auto to the link, as suggested elsewhere. Note I'm

Solution 1:

The link is placed on the end with d-flex and flex-column on card-body and mt-auto on the link. On your CodePen you are missing some of these classes (example with classes)!

Note: You can find this example also on the official documentation of Bootstrap 4.

To center the links you can use the text-center class on the card-link.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<div class="row portfolio">
  <div class="col-sm-12">
    <h2>Portfolio</h2>
  </div>
  <div class="col-lg-4 col-sm-6 portfolio-item">
    <div class="card h-100">
      <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
      <div class="card-body d-flex flex-column">
        <h4 class="card-title">Project One</h4>
        <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet numquam aspernatur eum quasi sapiente nesciunt? Voluptatibus sit, repellat sequi itaque deserunt, dolores in, nesciunt, illum tempora ex quae? Nihil, dolorem!</p>
        <a href="#" class="card-link text-info mt-auto text-center">View CodePen</a>
      </div>
    </div>
  </div>
  <div class="col-lg-4 col-sm-6 portfolio-item">
    <div class="card h-100">
      <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
      <div class="card-body d-flex flex-column">
        <h4 class="card-title">Project Two</h4>
        <p class="card-text ">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
        <a href="#" class="card-link text-info mt-auto text-center">View CodePen</a>
      </div>
    </div>
  </div>
  <div class="col-lg-4 col-sm-6 portfolio-item">
    <div class="card h-100">
      <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
      <div class="card-body d-flex flex-column">
        <h4 class="card-title">Project Three</h4>
        <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos quisquam, error quod sed cumque, odio distinctio velit nostrum temporibus necessitatibus et facere atque iure perspiciatis mollitia recusandae vero vel quam!</p>
        <a href="#" class="card-link text-info mt-auto text-center">View CodePen</a>
      </div>
    </div>
  </div>
  <div class="col-lg-4 col-sm-6 portfolio-item">
    <div class="card h-100">
      <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
      <div class="card-body d-flex flex-column">
        <h4 class="card-title">Project Four</h4>
        <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
        <a href="#" class="card-link text-info mt-auto text-center">View CodePen</a>
      </div>
    </div>
  </div>
  <div class="col-lg-4 col-sm-6 portfolio-item">
    <div class="card h-100">
      <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
      <div class="card-body d-flex flex-column">
        <h4 class="card-title">Project Five</h4>
        <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
        <a href="#" class="card-link text-info mt-auto text-center">View CodePen</a>
      </div>
    </div>
  </div>
  <div class="col-lg-4 col-sm-6 portfolio-item">
    <div class="card h-100">
      <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
      <div class="card-body d-flex flex-column">
        <h4 class="card-title">Project Six</h4>
        <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque earum nostrum suscipit ducimus nihil provident, perferendis rem illo, voluptate atque, sit eius in voluptates, nemo repellat fugiat excepturi! Nemo, esse.</p>
        <a href="#" class="card-link text-info mt-auto text-center">View CodePen</a>
      </div>
    </div>
  </div>
</div>

Solution 2:

Add this css, it will work

.card-body {
  display: flex;
  flex-direction: column;
}
.card-link {
   margin-top: auto;
}

codepen link


Solution 3:

An alternative solution

You can align the link at the bottom of the card-body by following these three steps:

  1. Apply the d-flex and flex-column classes to card-body
  2. Wrap all the content of card-body in a div.
  3. Use flex: 1 1 auto on this new div

Doing so, the newly created div always takes all the available space. Hence, the link is always at the bottom of the card-body.

.flex-grow {
  flex: 1 1 auto;
}
<div class="card-body d-flex flex-column">
  <div class="flex-grow">
    <h4 class="card-title">Project One</h4>
    <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet numquam aspernatur eum quasi sapiente nesciunt? Voluptatibus sit, repellat sequi itaque deserunt, dolores in, nesciunt, illum tempora ex quae? Nihil, dolorem!</p>
  </div>
  <a href="#" class="card-link text-info mt-auto">View CodePen</a>
</div>

Check this pen on CodePen

FYI, You can make an element occupy all the available space using this method.


Post a Comment for "Align Link To Bottom Of Card In Bootstrap 4"