What is the best way to ensure that the heights of Bootstrap's .card-img-top remain consistent across various screen sizes?

I have put together a collection of Bootstrap 4 cards in the following format...

<div class="row">

  <!-- column -->
  <div class="d-flex align-items-stretch col-xs-12 col-sm-6 col-md-4 col-lg-2 p-2">
      <!-- card -->
      <div class="card rounded-0 w-100 bg-white">
          <a href="http://www.example.com/destination.html">
            <img src="http://www.www.example.com/images/photo.jpg" class="card-img-top img-responsive w-100">
          </a>
          <div class="card-body">
            <h6 class="card-title">Bookish Blaenavon opens new chapter</h6>
          </div>
      </div>
      <!-- end card -->
  </div>
  <!-- end column -->

  <! -- more of the same cards here +++ -->

</div>

The images vary in size, but with the help of the .card-img-top class, they all appear uniform in dimensions.

Width:

To ensure consistent width, I have included .w-100 to both .card and the img, although it may not be necessary for the latter.

Additionally, my stylesheet specifies object-fit: cover; for .card-img-top.

So far, the setup seems effective, eliminating the need for a card deck to maintain consistency. I am satisfied if this method is appropriate.

Card Height:

To enforce equal-height cards regardless of content length, I utilize d-flex align-items-stretch within the column. This approach has been successful.

Image Height:

However, an issue arises here.

In addressing this concern, I have assigned a CSS attribute height to .card-img-top...

/* Equal-height card images, cf. https://stackoverflow.com/a/47698201/1375163*/
.card-img-top {
    height: 11vw;
    object-fit: cover;
} 

The problem lies in the fact that the img maintains the same height at every browser width, resulting in undesirable shortness at sm breakpoint, as illustrated below...

https://i.sstatic.net/EJmzP.gif

Is there a way to make the image height truly responsive, adjusting its height across different breakpoints?

Can this be achieved using Bootstrap 4 functionality instead of creating custom media queries to match its predefined widths?

Could any aspects related to width affect the outcome in terms of height?

Answer №1

To create a range for the growth and shrinkage of your images, you can apply an absolute min and max height to the img element. Here is an example:

.card-img-top {
    max-height: 200px;
    min-height: 150px;
    object-fit: cover;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">

  <!-- column -->
  <div class="d-flex align-items-stretch col-xs-12 col-sm-6 col-md-4 col-lg-2 p-2">
      <!-- card -->
      <div class="card rounded-0 w-100 bg-white">
          <a href="http://www.example.com/destination.html">
            <img src="https://via.placeholder.com/350x150" class="card-img-top img-responsive w-100">
          </a>
          <div class="card-body">
            <h6 class="card-title">Bookish Blaenavon opens new chapter</h6>
          </div>
      </div>
      <!-- end card -->
  </div>
  <!-- end column -->

  <! -- more of the same cards here +++ -->

</div>

In addition, there is no need to apply the .w-100 class to the image as the .card-img-top already sets its width to 100% of its containing element.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensuring PHP input fields are validated upon leaving the field

I am in the process of developing a system that is capable of calculating all input provided for invoice-related calculations. My goal is to have multiple text fields where, upon clicking out of a field, it will validate in real-time whether the input is ...

Updating Identification in Bootstrap 4 Data Tables

Currently, I am utilizing the Bootstrap 4 datatables features available at https://datatables.net/examples/styling/bootstrap4. In their example, they have assigned the id "example" to the table: <table id="example" class="table table-striped table-bord ...

Modify the transparency of a form's backdrop without affecting the input fields

I'm attempting to achieve a similar effect as shown in this example on my website form. I've tried using opacity to make the form field background only 20% visible, but it ends up affecting the text inside and when typing. Is there a way to apply ...

Switching back to the original CSS file after making changes with jQuery's CSS functions

Within another element's click event, I am using JQuery to toggle the display of a delete icon element from none to block and back again. However, in the CSS file for the corresponding class, I have defined: .pageDeleteIcon { display:none; } .pag ...

The clash between jQuery UI and Bootstrap themes

I am encountering an issue where the text on my Bootstrap-styled buttons is not appearing on my webpage. After some investigation, I discovered that certain lines in the jQuery UI theme file are causing this problem: .ui-widget input, .ui-widget select, . ...

Struggling to align rotated text within the Bootstrap 4 Grid system

I have been working on a grid layout using Bootstrap 4's Grid system, and I am aiming for a design similar to the following: https://i.sstatic.net/sU7kV.png While I have made significant progress and got almost 100% of it complete, I am facing an is ...

Creating a dropdown menu with HTML and CSS is causing me a major migraine

<div class="fbtop"> <img src="https://static.solidshops.com/1441/files/Logo-site.png" title="Pieke Wieke" alt="Pieke Wieke"> <h2 class="title">Handcrafted with love</h2> <ul class="dropdown"> <li> <a hre ...

Trouble with search box images on Internet Explorer causing confusion

Here is the searchbox image displayed: With this code and css styling: #searchcontainer { margin: 40px 0 0 0; } .search { float: right; margin: 0; padding: 0; height: 21px; width: 310px; } .search input, .search button { mar ...

Enhance User Experience with the Tooltip Feature and Customized

Here is the jQuery code I am using to style my tooltips: $(function() { // select all input fields within #tooltips and attach tooltips to them $("#tooltips :input").tooltip({ // place tooltip on the right edge position: "cen ...

Steps for removing all inline styles within an element:1. Access the element's

I have a group of span elements containing texts generated by tinymce. <span class="ArticleSummary"> This content is produced using a text editor and may include various inline styles. </span> Typically, this text includes numerous inline s ...

Concerns with the background image

I have been attempting to include a background image in my project, but for some reason it is not showing up when I check it in the browser. I am compiling the .scss files into a CSS directory. Below is an image of how my folders are structured: https://i ...

Numerous Divs Expanding to Fit Window Size

I successfully managed to make one div expand to the size of the window, filling the screen completely. However, I am facing an issue where the other divs are overlapping each other and only displaying thing 3. How can I ensure that they do not overlap and ...

What is the best way to position a Button at the bottom of a div using jQuery?

Within my HTML, I have divided into two col-md-6 sections. On the left side, there is a picture and on the right side, there is text. I am considering using jQuery .height() for this layout. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7 ...

Arranging text within a td cell

This is what I currently have: <tr> <td colspan="4" style="font-size:7pt; font-color:red; font-weight: bold;"> PLEASE NOTE: SPECIFY THE ARTICLE IN AS MUCH DETAIL AS POSSIBLE </td> </tr> However, the text does not a ...

What is the best way to ensure a font-face is displayed consistently across different browsers?

Having some issues with Helvetica Neue - when I set the font-size to 15px it looks fine, but if it goes above 16px, it starts to look a bit strange. Check out how it should look here: And here is the problem I am facing: Any suggestions on how to fix th ...

Issues with loading an external CSS file in an HTML document

I've been attempting to implement a loading animation using jQuery, but I'm encountering issues with loading the CSS file. I have tried various paths for the css file such as: <link href="css/animation.css" type="text/css" rel="stylesheet"> ...

How to Programmatically Assign Bootstrap Class to an Element Using Angular 2

I am working on a page with several input boxes that need to be flagged for certain criteria. <div class="form-group"> <label class="d-block">Allowance Type</label> <div class="input-group"> ...

Floating menu links are positioned to the left and right in WordPress

I've been utilizing the following code snippet to create a custom menu within Wordpress: $defaults_topmenu = array( 'theme_location' => '', 'menu' => 'Top Menu', 'container&ap ...

Header Logo centered at the top

I am using a Bootstrap 4 navbar where the nav-brand is centered on a computer window, but shifts to a dropdown when expanded on mobile. Is there a way to keep the brand fixed at the top and center? Here is my navbar code in HTML: <nav class="navba ...

I'm having trouble getting the Codesandbox preview to work, but I can't seem to figure out what the issue is. What could I

Currently, I am practicing by copying random web pages to enhance my skills. Right now, I am in the process of replicating this page. When viewed in VS Code, everything looks fine and even when opened in Chrome. However, after uploading it to Codesandbox, ...