2

How do I make images take up the entire width of a card-img in a Bootstrap 4 card? What I want to do is set the height of the card-img and have the width be automatically filled and the image vertically centered in the card. I need to do this because I have multiple cards with different images of different sizes in a row. Right now, my code looks like this:

<div class="row">
  <div class="col-6">
    <div class="card h-100 border-0 shadow">
      <div class="card-img-top overflow-hidden">
        <img src="..." alt="" class="img-fluid" />
      </div>
      <div class="card-body">
        <p>Card content goes here</p>
      </div>
    </div>
  </div>

  <div class="col-6">
    <div class="card h-100 border-0 shadow">
      <div class="card-img-top overflow-hidden">
        <img src="..." alt="" class="img-fluid" />
      </div>
      <div class="card-body">
        <p>Card content goes here</p>
      </div>
    </div>
  </div>
</div>

So in this scenario, the two imgs are different sizes. I want the two cards to be the same height, and I want the images to fully take up the card-img whether they are longer than they are wide, or wider than they are long.

1 Answer 1

1

By far the simplest form of image equality in size is using aspect ratio's. Bootstrap has an embed utility that you can utilise for such a case. It's shipped with 16x9 and 4x3 aspect ratio but you can easily add your own.

In this demonstration I've provided two images, one landscape and the other portrait, both of which appear the same and fill the available aspect ratio.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-6">
      <div class="card h-100 border-0 shadow">
        <div class="card-img-top">
          <div class="embed-responsive embed-responsive-4by3">
            <div class="embed-responsive-item">
              <img src="https://placeimg.com/640/480/any" alt="" class="img-fluid w-100" />
            </div>
          </div>
        </div>
        <div class="card-body">
          <p>Card content goes here</p>
        </div>
      </div>
    </div>
    <div class="col-6">
      <div class="card h-100 border-0 shadow">
        <div class="card-img-top">
          <div class="embed-responsive embed-responsive-4by3">
            <div class="embed-responsive-item">
              <img src="https://placeimg.com/480/640/any" alt="" class="img-fluid w-100" />
            </div>
          </div>
        </div>
        <div class="card-body">
          <p>Card content goes here</p>
        </div>
      </div>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

Only problem with this is that I want the image to maintain its original aspect ratio. This distorts the image.
I don't follow, if two images have different aspect ratios, how can they equal in height without knowing which ones larger than the other? Images in the code shouldn't distort now, I should have wrapped the images with .embed-responsive-item rather than apply directly to the image. This will retain the original aspect rations but appear as 4x3 as explained in the answer.
So the thing is, the images have different aspect ratios, and I want them to fit into a space that’s like 400px wide and 300px tall. But I don’t want to distort the image. So if the image is 400px wide but taller than 300px I want the image to be aligned vertically in the center and have its sides on the sides of the container, but if the image is 300px tall but slimmer than 400px I want it to have the top and bottom touching the top and bottom sides of the div and only show part of the image from a horizontal standpoint.
So what you're saying is you want the images to be center aligned vertically and horizontally, while filling the width of the container which has a fixed height/width?
Exactly. Any way to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.