Creating uniform image heights using Flexbox

My webpage features a library of books, with each book displayed in rows within a flexbox container. Each book is represented as a separate block consisting of a heading (the book's name) and an image of the book cover directly below it. I'm currently facing an issue where the images don't maintain equal height and fail to fill up the entire block. Here's a visual representation of my current setup:

I did some research on Stack Overflow regarding similar queries, but the solutions offered didn't seem to work for me. Despite setting the image element inside the container to 100% height, why are the images varying in heights initially? How can I resolve this issue?

You can view the complete HTML/CSS code on JSFiddle.

HTML:

<div class="flexbox">
    <div class="item">
      <h2 class="catalogue">
        <a href="items/pyramid_texts.html" target="_blank">
           Тексты пирамид
        </a>
      </h2>
      <img class="catalogue"
           src="images/pyramid_texts.png" alt="Тексты пирамид">
    </div>

    <div class="item">
      <h2 class="catalogue">
        <a href="items/coffin_texts.html" target="_blank">
           Тексты саркофагов
        </a>
      </h2>
      <img class="catalogue"
           src="images/coffin_texts.jpg" alt="Тексты саркофагов">
    </div>

    <div class="item">
      <h2 class="catalogue">
        <a href="items/book_of_the_dead.html" target="_blank">
           Египетская книга мертвых
        </a>
      </h2>
      <img class="catalogue"
           src="images/book_of_the_dead.png" alt="Книга мертвых">
    </div>
  </div>

CSS:

  a
  {
    color: black;
    text-decoration: none;
  }

  a:hover
  {
    color: red;
  }

  div.flexbox
  {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  div.item
  {
    height: 20%;
    width: 20%;
  }

  h2
  {
    background-color: #FFF3D9;
    color: black;
    font-size: 18px;
    font-weight: 400px;
    style: block;
    text-align: center;
  }

  img
  {
    height: 20%;
    outline: 2px solid red;
    width: 20%;
  }

  img.catalogue
  {
    height: 100%;
    object-fit: contain;
    width: 100%;
  }

UPDATE: Even after trying out different CSS styles like background-size: cover, I still haven't been able to fix the issue. Could it be related to the structure of my HTML? Currently, I have a parent flexbox containing "item" blocks with a header displaying the book title and an <img>. Should I consider restructuring by removing the header from the item block?

Answer №1

Feel free to utilize the following code snippet

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <title>Demo</title>
  <style type="text/css">
    body {
      padding: 0;
      margin: 0;
    }
    
    a {
      color: black;
      text-decoration: none;
    }
    
    a:hover {
      color: red;
    }
    
    h2 {
      background-color: #FFF3D9;
      color: black;
      font-size: 18px;
      font-weight: 400;
      style: block;
      text-align: center;
    }
    
    img {
      height: 20%;
      outline: 2px solid red;
      width: 100%;
    }
    
    img.catalogue {
      height: 100%;
      object-fit: contain;
      width: 100%;
      display: block;
      margin: 0 auto;
      text-align: center;
    }
    
    .flexbox {
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      overflow: hidden;
    }
    
    .flexbox .col {
      flex: 1;
      padding: 7px;
    }
    
    .flexbox .col:nth-child(1) {
      background: #ccc;
      -webkit-order: 1;
      -ms-flex-order: 1;
      order: 1;
    }
    
    .flexbox .col:nth-child(2) {
      background: #eee;
      -webkit-order: 0;
      -ms-flex-order: 0;
      order: 0;
    }
    
    .flexbox .col:nth-child(3) {
      background: #eee;
      -webkit-order: 2;
      -ms-flex-order: 2;
      order: 2;
    }
  </style>
</head>

<body>
  <div class="flexbox">
    <div class="col">
      <div class="item">
        <h2 class="catalogue">
          <a href="items/pyramid_texts.html" target="_blank">
                   Texts of Pyramids
                </a>
        </h2>
        <img class="catalogue" src="https://www.w3schools.com/images/picture.jpg" alt="Texts of Pyramids">
      </div>
    </div>
    <div class="col">
      <div class="item">
        <h2 class="catalogue">
          <a href="items/coffin_texts.html" target="_blank">
                   Coffin Texts
                </a>
        </h2>
        <img class="catalogue" src="https://www.w3schools.com/images/picture.jpg" alt="Coffin Texts">
      </div>
    </div>
    <div class="col">
      <div class="item">
        <h2 class="catalogue">
          <a href="items/book_of_the_dead.html" target="_blank">
                   Egyptian Book of the Dead
                </a>
        </h2>
        <img class="catalogue" src="https://www.w3schools.com/images/picture.jpg" alt="Book of the Dead">
      </div>
    </div>
  </div>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

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

How can the seating arrangement be optimized for a seating chart and what is the most effective way to transition away from a traditional table structure?

Currently, I am attempting to create a dynamic seating chart on a webpage within an asp.net-mvc site using data retrieved from a database. Initially, I used a table to organize the grid layout of rows and columns, which resembled this structure: The datab ...

How about a CSS one-by-one black pixel that's not opaque?

I've been attempting to achieve a 70% transparent black background on an element, but unfortunately, I had to use a pixel to address browser compatibility problems. Using CSS alone would make the entire content of the element transparent as well. Her ...

Issues encountered when trying to implement a Navbar drop-down menu using Angular in conjunction with Bootstrap 4

I'm currently working on a web application using Angular 5 and Bootstrap 4, and I've encountered issues with the navigation menu bar dropdowns. Despite following the guidelines provided in the official documentation here, I can't seem to get ...

Is it necessary to mark all checkboxes in the initial column of the gridview?

I have encountered an issue with my code that checks all checkboxes in the first column of a gridview. It seems to work only for Internet Explorer versions 5.0 to 8.0, but gives a Javascript error when running on IE 9 and above stating "Function Expected ...

AngularJS - Sending configuration values to a directive

I'm trying to figure out how to pass parameters (values and functions) to an Angular directive. It seems like there should be a way to do this in Angular, but I haven't been able to locate the necessary information. Perhaps I'm not using th ...

Content from PHP's unlink() function continues to persistently display

I run an Angular front end along with a PHP back end for my website. I utilize PHP's unlink function to remove specific images from Angular's assets folder: $fileToDelete = "../src/assets/images/".$name; unlink($fileToDelete) or die("Error delet ...

Struggling with implementing a materialize modal?

I am encountering a problem with Materialize. This time, I am trying to create a modal div, but it doesn't seem to be working. The button is created, but when I click on it, nothing happens. I have made sure to link all the necessary Materialize files ...

What could be causing the background color not to change on the HTML page with Bootstrap?

Learning html, bootstrap, and css styling can be a confusing endeavor. Sometimes things don't work as expected, like changing the background color. Despite following what seems like a simple solution after googling, the background color remains unchan ...

Tips for ensuring text remains within a div container and wraps to the next line when reaching the edge

Currently, I am working on a flash card application using Angular. Users can input text in a text box, and the text will be displayed on a flash card below it. However, I have encountered an issue where if the user types a lot of text, it overflows and mov ...

What is the procedure to reduce my final search result by 1?

When the search is triggered, I want to filter the images by name. However, the issue arises when trying to make everything disappear on filter addition. <ul id="list2"> <li class="in2"> An extra number is added ...

php code to show data in a single column of a table

I am working with a database record that includes an id and name, and I need to display it in a table where records are distributed in the pattern 1-2-1-2-4 across columns. Below is my current code that is generating the incorrect output: <table borde ...

The CSS background cannot be blurred

After constructing a basic HTML page, I decided to incorporate the following CSS code: html { background: url("photourl") no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover ...

Steps for Creating an Animation Tool based on the Prototype:

One question that recently came up was: What is the best approach to tackling this issue? Developing a tool that enables designers to customize animations. To make this process easier, it is essential to create an AnimationSequence using JavaScript that ca ...

Creating Image Overlays with Hover Effects using CSS and PHP Conditions

Looking for some assistance with a Minecraft server listing page I am working on for a client. I have never done this before and have encountered a roadblock. Take a look at this image (apologies for using Paint, I was in a hurry!) The goal is to have the ...

Avoid page refreshing when retrieving data using PHP and AJAX

In my current program, I am able to add data and insert it into the database. However, I am looking for a way to automatically send this data to another page or browser without refreshing. I have two browsers open, so how can I submit the data to the other ...

Utilize the power of MYSQL and PHP in conjunction with an HTML form to

Having trouble with a basic PHP/SQL search bar for my database. The search results are not showing up even though the search bar appears on the page. When I type something, it doesn't reflect in the URL. Below is the code snippet. I am connecting to t ...

Creating CSS style rules for aligning table column headers in JavaFXML

Is it possible to style columns individually in a tableview? I have been attempting to make some column headers left-aligned and others right-aligned, as shown below: | hdr1 | hdr2 | hdr3 | hdr4 | In my css stylesheet, I experimented with the following ...

Avoid Scrolling within an iFrame in HTML with the Use of #

I have a menu loading in an iframe with links using # like http://<location>/page.html#part1. When I click inside the iframe, the entire page outside of the iframe scrolls to the location of #. How can I stop this from happening? I only want the me ...

Are there any instances where CSS is overlooking certain HTML elements?

In the following HTML code snippet, the presence of the element with class ChildBar is optional: <div class="Parent"> <div class="ChildFoo"></div> <div class="ChildBar"></div> <!-- May ...

CSS and HTML modal not closing

I've been working on creating a custom popup using HTML, CSS, and some jQuery functions. My idea was that when I click on the main div, it should expand in size and display a cancel button, which it does successfully. However, the issue arises when tr ...