Images of varying sizes aligned at the bottom of cards, organized in rows with pure CSS styling

I am working on a layout that involves a container with "cards": images positioned above with related text below. Here's an example:

<div class = "cards">

  <div class = "card">
    <div class = "image-wrap">
      <img src= "image1.jpg" />
    </div>
    <div class = "text">
      Lorem ipsum
    </div>
  </div>

  <div class = "card">
    <div class = "image-wrap">
      <img src= "image2.jpg" />
    </div>
    <div class = "text">
      Lorem ipsum dolor sit
    </div>
  </div>

  <!-- snip -->

</div>

My challenge is to align the bottom edges of these images within each row as they have variable height. The number of "cards" in a row will change based on responsiveness, so tables are not suitable here.

A jsfiddle (link provided) comes close to achieving this but I aim for the bottoms of the varying-height images to align perfectly within each row. Each card should adjust its height to match the tallest one in that row, ensuring alignment at the bottom.

The outcome desired is illustrated in this image: ideal layout

Separating the images and text into different containers won't work due to the responsive nature and variability in the number of cards per row based on breakpoints. A purely CSS solution is preferred to prevent layout shifts after JavaScript loads, although achieving this solely with CSS might be challenging.

.cards {
   grid-template-columns: calc(1/3*100% - 1/3*10px) calc(1/3*100% - 1/3*10px) calc(1/3*100% - 1/3*10px);
  grid-gap: 5px;
  margin: 0 auto;
  display: grid;
  width: 100%;
  background-color: #f1f1f1;
}

.card {
  border: 1px solid #ccc;
  padding: 5px;
}

.image-wrap,
.text {
  width: 100%;
}

.image-wrap {
  text-align: center;
}
<div class = "cards">

  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x40/fff.jpg"> 
    </div>
     <div class = "text">
      Lorem ipsum dolor sit amet
    </div>    
  </div>
  
  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x80/fff.jpg">
     </div>
    <div class = "text">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </div>    
  </div>

  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x50/fff.jpg">
    </div>
    <div class = "text">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>    
  </div>
  
  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x50/fff.jpg">
    </div>
    <div class = "text">
      Lorem ipsum dolor sit amet.
    </div>    
  </div>


</div>

Answer №1

Keep in mind that

the height will adjust to the tallest image's height. If you have images of varying heights, consider using jquery to set this CSS property.

.image-wrap,.text { height:80px; }

the left margin will be set at 50% (in pixels) of the individual image size. If your images have differing widths, utilize jquery to inline-set this CSS property.

.image-wrap img { position:absolute; margin-left:-45px; bottom:0;}

.cards {
   grid-template-columns: calc(1/3*100% - 1/3*10px) calc(1/3*100% - 1/3*10px) calc(1/3*100% - 1/3*10px);
  grid-gap: 5px;
  margin: 0 auto;
  display: grid;
  width: 100%;
  background-color: #f1f1f1;
}

.card {
  border: 1px solid #ccc;
  padding: 5px;
}

.image-wrap,
.text {
  width: 100%;
  height:80px;
}

.image-wrap {
  position:relative;
  text-align: center;
}

.image-wrap img {
  position:absolute;
  margin-left:-45px;
  bottom:0;
}
<div class = "cards">

  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x40/fff.jpg"> 
    </div>
     <div class = "text">
      Lorem ipsum dolor sit amet
    </div>    
  </div>
  
  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x80/fff.jpg">
     </div>
    <div class = "text">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </div>    
  </div>

  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x50/fff.jpg">
    </div>
    <div class = "text">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>    
  </div>
  
  <div class = "card">
    <div class = "image-wrap">
      <img src ="https://via.placeholder.com/90x50/fff.jpg">
    </div>
    <div class = "text">
      Lorem ipsum dolor sit amet.
    </div>    
  </div>


</div>

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

Could someone explain how CSRF protection works with empty Flask-WTForms forms?

I have a query regarding csrf Cross-site Request Forgery Attacks in flask. After finding a helpful youtube video, here is what I learned: There was an incident where an email was changed by someone who was logged in through a specific path that allows up ...

Exploring the World of Html

I'm struggling with an HTML problem related to a web programming class I'm taking. The assignment involves creating a video game using HTML and JavaScript, where an image moves randomly on the screen and the player must click on it as many times ...

Tips for properly formatting quotes in JSON to activate a link

I am working with a JSON configuration file that looks like this: "type": "script", "label": "coreapps.archivesRoom.printSelected", "script": "emr.fragmentActionLink(${project.parent.artifactId},\"downloadPatientHistory\", &bs ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

What is the best way to retrieve JSON data using JavaScript within a loop?

I'm struggling to retrieve data from a JSON object. Here's the JSON structure I'm working with: var data = [ {"mes":{ "January":{ "Inversion":"1000","Fans":"1020"} ...

Table design with fixed left columns and header that adjust to different screen sizes

After reading various articles on this issue, I have been unable to find a solution. Most of the examples I've come across feature tables with a single row for the header and rows that consist of just one row, similar to a calendar. My table, on the o ...

In Firefox, there is a peculiar issue where one of the text boxes in an HTML form does not display

In my HTML form, users can input their first name, last name, and phone number to create an account ID. The textboxes for first name, last name, and account ID work smoothly on all browsers except Firefox. Surprisingly, the phone number textbox doesn' ...

Adaptive Design - Content Fragmentation

I am currently working on a webpage layout that has the following HTML structure: <div class="row"> <div class="col3">test 1</div> <div class="col3">test 2</div> <div class="col3">test 3</div> </div ...

How do I simulate a checkbox click with jQuery based on the text included in its ID?

Below is a sample table with checkboxes: <table> <tr> <td><input id="aaa_1" type="checkbox" /></td> <td>1</td> </tr> <tr> <td><input id="aaa_2" type="checkbox" /></td> ...

What steps can I take to troubleshoot the 'Uncaught DOMException: failed to execute add on DOMTokenList' error?

I am looking to create media icons <div class="contact"> <span> <i class="fa fa-phone"></i> </span> <span> <i class="fa fa-facebook"></i> </spa ...

Vertical alignment is somewhat functioning but not completely

Could someone help me figure out why my green DIV is not vertically aligned in the middle of the red DIV and suggest a solution? Thank you! Link to JSFiddle .wrapper-alt { margin-right: auto; margin-left: auto; width: 980px; display:tab ...

Tips for enhancing the color saturations of cells that overlap in an HTML table

My goal is to enhance the color of overlapping cells in my HTML tables. For instance, when I click on cell 2, the .nextAll(':lt(5)') method will change the class of the next 4 cells. https://i.stack.imgur.com/mwv8x.png Next, clicking on cell 3 ...

The problem with clearing floats in IE7 (as well as 6)

Currently, I am facing an issue where I have a list that I want to split into three columns by using the float left property and then clearing the first column. Everything seems to be working fine in IE8 and FF, however, IE7 is causing the second column t ...

What could be causing my AngularJs routing and animation to bypass the second redirection?

Recently started working with Angular and experimenting with routing and animations to manage my page transitions. I followed a helpful guide that helped me set everything up. I encountered a few issues: When trying to link back to the landing page (home ...

Having trouble submitting a date input form generated with vuejs on Safari browser

I am a huge fan of Vuejs and it's my go-to at work. The other day, I came across a rather perplexing scenario. If you have any insights into this, please do share. Here is the code in question: <script setup lang="ts"> import { ref ...

Looking to retrieve the value of an input element within an ng-select in Angular 6?

Currently, I am working on a project where I aim to develop a customized feature in ng-select. This feature will enable the text entered in ng-select to be appended to the binding item and included as part of the multiselect function. If you want to see a ...

A guide on adding two fields together in AngularJS and displaying the output in a label

I am facing a unique issue on my webpage. Including two inputs and a label in the page, I want the label to display the sum of the values entered into these two inputs. My initial attempt was as follows: Sub-Total <input type="text" ng-model="Propert ...

Creating an animated CSS growth effect using inline SVG

I'm trying to make these circuits look like they are running downwards. I have the right movement, but I can't seem to start them at their actual size, causing them to appear skewed at the beginning of the animation and then scaling to full size ...

I am having trouble deactivating an HTML button using my JavaScript function

I have been working on a feature where a button in HTML is supposed to be disabled until a specific textbox is filled out. Once the textbox has content, the button should become enabled and save the user's name along with their score from a previous g ...

Questions from beginners regarding the use of canvas elements with imported images, including concerns about caching and altering colors

I have a couple of beginner questions regarding the canvas element in HTML. Firstly, I was wondering if images imported into a canvas element are cached? Is this consistent across different browsers? Secondly, is it possible to import a black and white PN ...