Achieving consistent image column heights that decrease evenly

I am trying to create a grid-like layout where each column in a row has the same height and contains an image while maintaining its proportions.

Currently, I am using the flex-grow property to adjust the ratio of the images. Although this works, it often results in overly large images which is not ideal for my project.

https://i.stack.imgur.com/EEb0U.png

I want the images to automatically resize to fit within a single row when there is leftover space.

https://i.stack.imgur.com/6OSyl.png

The flex-shrink property functions similarly to flex-grow but distributes negative space based on the set values.

However, there is a downside as items will never shrink below their original content size, preventing columns from resizing beyond the dimensions of the contained images.

To overcome this issue, I have experimented with adjusting the flex-basis property but faced challenges with wrapping and maintaining equal heights.

My desired outcome is as follows:

Consider the following code snippet:

.container {
  display: flex;
  flex-wrap: wrap;
  width: 600px;
}

.img-container {
  padding: 5px;
  box-sizing: border-box;
  flex-basis: auto;
}

.img {
  width: 100%;
  height: auto;
}
<div class="container">
  <div style="flex-grow: 1; flex-shrink: 1;" class="img-container">
    <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
  </div>
  
  <div style="flex-grow: 1; flex-shrink: 1;" class="img-container">
    <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
  </div>

  <div style="flex-grow: 2; flex-shrink: 2;" class="img-container">
    <img class="img" src="https://via.placeholder.com/400x200/" alt=""> 
  </div>
</div>

The total width sums up to 200+200+400=800px, leaving a negative space of 200px (800-600=200). By applying the flex-shrink values, we allocate parts of this negative space accordingly.

This calculation results in:

  • Width reduction for item one: 1/4*200=50
  • Width reduction for item two: 1/4*200=50
  • Width reduction for item three: 2/4*200=100

Although these calculations ensure correct proportions, achieving this effect proves challenging due to the limitations imposed by the natural widths of the image elements as determined by flex-basis.

If you have any suggestions on how to address this issue, I would greatly appreciate your input.

Answer №1

To ensure that your images are displayed properly, make sure to set the width of your .container and the height of your .img-container.

Next, determine how you want to space your images using the following CSS:

.container { justify-content: space-evenly/space-between/space-around}

.container {
  display: flex;
  flex-wrap: wrap;
  width: 600px;
  justify-content: space-evenly;
}

.img-container {
  padding: 5px;
  box-sizing: border-box;
  flex-basis: auto;
  height: 150px;
}

.img {
  width: auto;
  height: 100%;
}
<div class="container">
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/400x200/" alt=""> 
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/400x200/" alt=""> 
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/400x200/" alt=""> 
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
   </div>
   <div class="img-container">
      <img class="img" src="https://via.placeholder.com/200x200/" alt="">     
   </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

CSS not aligning email header correctly

I have been given the task of coding an HTML email for mailing campaigns. I have managed to get everything working, except for a particular piece of code. What I am trying to accomplish is having an image with other elements on top such as titles, a button ...

Develop a novel function

I am working on a new Order page where users can select a category, then a service, and fill out the quantity field for their order. I have an idea to create a function using @if and @else statements. In this function, I want to display the quantity fiel ...

bootstrap 5 with uneven spacing

I am working on some HTML/CSS code that utilizes Bootstrap 5.2 and the justify-content-between class in order to move two buttons to the edges of a container. <div class="container"> <div class="row"> <div cla ...

Instructions on how to save HTML "innerHTML" along with attributes to a text document

I'm currently developing an HTML export feature from a DIV tag that includes various elements and attributes. HTML: <div id="master"><span class="classname">content goes here</span></div> <span class="download" onclick="ca ...

Utilizing Google Maps within a responsive layout with 2 columns

Seeking guidance on integrating a map into the second column of a responsive website. Acknowledging that my code may not be optimal, as this is my first attempt at such a task. After a day of searching for solutions, I'm feeling quite desperate. HTML ...

tips for linking my webpage with the database

While working on a registration page, I encountered an issue where the entered information was not being stored in the database. Instead, an error message appeared: Notice: Undefined variable: sql in C:\xampp\htdocs\Bawazir\Untitled-1. ...

mobile-responsive vertical alignment using Bootstrap

Here is a jsbin that demonstrates the issue and here is the markup: <div class="row"> <div class="col-sm-4 xs-12"> <strong>UK Postcode</strong> </div> <div class="col-sm-4 xs-12"> <strong>Other Fie ...

What causes jquery height and javascript height to both be returned as 0?

I'm facing an issue with a visible div on my screen - despite being visible, its height always returns as 0. I've attempted various jQuery and JavaScript methods to retrieve the height, but it consistently shows as 0. Here's the structure of ...

Unable to display divs in a stacked format using Bootstrap 5

I need help aligning div's vertically on the page. Currently, all my elements are displaying next to each other instead of stacking one below the other. body { height: 100vh; } <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi ...

The surprising way Next.js disabled the production build and transformed my rgba color into hsla

I created CSS code to display grid patterns like this: background-image { repeating-linear-gradient( 0deg, rgba(255, 255, 255, 0.43) 0px 1px, transparent 1px 20px ), repeating-linear-gradient( 90deg, rgba(255, 255, 255, 0.43) 0px 1px, transparent 1px 20 ...

Switching out text when hovering with Jquery or JavaScript

Is there a way to use jQuery or JS to make the text and icon disappear when hovering over a div, and replace it with "Read More"? I've looked at some guides but they only remove one line of text instead of clearing the entire div and displaying "Read ...

What is the method for configuring body attributes in CSS with AngularJS?

Setting the background image of a page using CSS: body { background: url(http://momentumbooks.com.au/wp-content/uploads/2013/06/space.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-backg ...

Establish a connection to a regular socket by utilizing WebSocket technology

I am attempting to connect a client interface to a hardware device using WebSocket in a browser like Chrome. The code snippet I have been using for connection is as follows: var ws = new WebSocket("ws://127.0.0.1:13854"); ws.onopen = function() ... Howev ...

Copy the style of a chosen element and apply it to another element

One of the challenges I'm facing involves a dynamic span element that changes based on color selection: <span class="color checked" style="background-color: #ff0000">Red</span> In addition, I have an image element wit ...

Incorporating Material-UI themes within styled components

Trying to incorporate theme spacing value in a styled component but facing issues. Initially, the style was applied to the button, but now it is not working as expected. You can view the problem here: sandbox. import React from "react"; import ...

How can I implement horizontal scrolling on a material-ui table containing numerous columns?

As I work with the React Material-UI framework, I am using this table example as a guide. However, I am facing an issue where my table becomes overcrowded when I have numerous columns, causing the content to be cut off. I recently came across the material ...

Interactive input field designed for modifying, adding, and removing data in MySQL

I am working on a project where I have a simple form. However, I need to dynamically change the form action from insert to update within the same page. Additionally, I also want to display the values on the same page. Within my code, I have set up the up ...

What is the best way to align the menu to the center

Hey there, I'm trying to center a dropdown menu in a 970px wide div. No matter what I do, I can't seem to get it to work correctly. <div id="menuwrapper"> <ul class="menu" id="menu"> <li><a href="#" >Home</ ...

The text that follows the cursor seems to stand out taller than the rest

When I type words in a textarea input field, the cursor seems to be taller than it should be (as shown below). What is causing this issue and is there a way to make it as tall as the words with a font size of 38px? Here is the code snippet: <textarea n ...

Issues with centering background-position while using clip-path functionality

Struggling to center a background image? I initially suspected conflicting CSS rules, so I created a simplified demo to troubleshoot. After reviewing examples on SO and beyond, it seems like my approach is correct. Can anyone help identify what I might be ...