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

Why do I see my footer displayed twice on the page?

<div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li> @Ajax.ActionLink("Imagenes List", "Images", "Home", new { page = 0 }, new AjaxOptions() ...

Displaying a collapsible table directly centered within the table header

I am having trouble centering my table header in the web browser page. When I click the "+" button, the data is displayed beneath the table header, but I want the collapsible table to be centered directly below the header. I have tried making changes in CS ...

JavaScript/DOM - What sets apart a "CSS Selector" from an attribute?

When it comes to excluding declarative event handlers: <a href='#' onclick=<handler> ... /> Is there a significant difference between an Attribute and a CSS Selector? For example, if I define my own attribute: <a href='#&a ...

Dropdown menu is not positioning correctly over other elements on the webpage

After researching multiple stack overflow questions and attempting various solutions, I still haven't been able to resolve my issue. Please refrain from marking this question as a duplicate. The problem I am facing is that my dropdown menu is not ap ...

Guide on fetching data from a different php file using jQuery's .load() method?

I am trying to use a basic .load() function from jQuery to load a div element with an id from another file in the same directory when changing the selected option in a selector. However, I am having trouble getting it to work. Nothing happens when I change ...

Update location when visibility changes

Looking for a way to automatically refresh an HTML page when a specific div becomes visible, but seems like I am missing something. There is a JavaScript code, divslide.js, that changes the display style of the div from none to inline at predetermined time ...

The <form> element is giving me headaches in my JavaScript code

Trying to troubleshoot why my HTML pages render twice when I add a form with JavaScript. It seems like the page displays once with the script and again without it. Below is the basic HTML code: <form action="test.php" method="post"> <div class=" ...

Dynamic Bootstrap Table with Fixed Header

I am customizing a Bootstrap table by adding CSS for a sticky header. My code snippet includes the following: .table-sticky th { background: #fff; position: sticky; top: -1px; z-index: 990; } <div class ...

Issues with scaling background videos in HTML5

I'm having trouble making a video scale properly with the browser window while still covering the entire area. Does anyone know why my current approach isn't working? HTML: <div class="bgVideoWrap"> <video id="bgVideo" loop="true" aut ...

The white background of the .jpg image is conflicting with the white background of the HTML

My website has a top-of-page .jpg image that was created with a white background. However, when using the img src="topOfEveryPageImage.jpg" tag, the white background of the image appears visually different from the rest of the solid white background on the ...

Utilizing the power of JQuery and KineticJS in HTML5 for Drawing and Interactive Drag-and-Drop

Creating a page with mouse-drawn lines and drag-and-drop functionality has been challenging. The primary issue I'm facing is the inability to delete more than one item. Additionally, cloning an item on drag does not work as intended, resulting in dup ...

Ways to adjust the ngx-pagination color scheme?

I am looking to customize the background color of ngx-pagination Here is my current code: <pagination-controls class="custom-pagination" id="indicadorPaginationResults" (pageChange)="p=$event" maxSize="9" directionLinks="true" autoHide="true" previ ...

Do flexible layouts require more effort and time to create and upkeep compared to fixed width layouts?

Are flexible layouts more challenging to create and maintain than fixed width layouts, and do they take longer to design? Does a flexible layout only involve setting the width, height, and font size in em or %? What are the main advantages of using a fle ...

Is it possible to reset the text within the text box when the form is being submitted using the load() ajax function?

I am working on implementing a comment feature where the data entered in the form is appended and displayed after submission. Here is my HTML form: <table> <tr><td>Name :</td><td> <input type="text" id="name"/></td&g ...

Is it possible to leverage React/Next.js for dynamically adding HTML content based on element IDs when integrating with Netlify forms?

Sorry if this question has already been addressed somewhere. I've spent hours searching and troubleshooting without success. I'm still in the process of learning React, as it's a new concept to me. I just need to implement it for a contact ...

Can we limit the number of items to just two per row in a grid with two columns?

I'm currently facing issues with aligning items within a two-column grid. When looking at this image, you'll notice that the alignment is off. Specifically, 'Example 3' does not align properly with 'Example 4'. This seems to b ...

Equal vertical alignment in the center using flexbox

My flexbox layout is set up as shown in the code snippet below: html { height:100%; width:100%; } body { display:flex; flex-direction:column; align-items:stretch; height:100%; width:100%; margin:0; background-color:grey; } header { b ...

Tips for stopping automatic scrolling (universal solution)

Issue or Inquiry I am seeking a way to disable actual scrolling on an element while still utilizing a scrollbar for convenience (avoiding the need for manual JavaScript implementations instead of relying on browser functions). If anyone has an improved s ...

What causes the disappearance of CSS styles when attempting to modify the className in react js?

I am currently working on a basic react application, and I am trying to dynamically change the name of a div element using the following code snippet - <div className={'changeTab ' + (page.login ? 'leftSide' : 'rightSide')} ...

The issue of stuttering arises in Safari when using a combination of size and translate transitions

When applying transitions to an element and adjusting the width and/or height along with -webkit-transform:translate3d, there is a noticeable stutter in the transition animation. It seems like the width/height change is animated first, then partially trans ...