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:

https://i.sstatic.net/ZBBqs.jpg

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

Steps on how to trigger an onmouseover event for entire blocks of text:

I'm currently in search of an onmouseover code that seems to be elusive on the vast internet. A CSS box format has been successfully created: .box { float: left; width: 740px; height: 300px; margin-left: 10px; margin-top: 10px; padding: 5px; border: ...

What is the best way to assign attributes to multiple HTML elements using an array?

Seeking assistance to hide various sections in my HTML file upon button click, with the exception of one specific section. Encountered an error message: Uncaught TypeError: arr[i].setAttribute is not a function Here's a snippet of my code: const hide ...

How can AJAX be utilized with both file and text inputs?

I have a basic form enclosed in a modal window, dynamically generated using jQuery: <form name="altEditor-form" id="altEditor-form" enctype="multipart/form-data" role="form"> <div class="form-group"> <div class="col-sm-5 col-md- ...

Only apply the CSS rule to all pages except for one

I'm a beginner when it comes to css. In my wordpress website editor (Onetone theme), I added the following rule to my 'custom css field': html, body {margin: 0; height: 100%; overflow: hidden} It's working well...the scrollbar is gone ...

Having difficulty accurately interpreting the input value

As someone new to JavaScript, I set out to create a basic calculator using JavaScript and HTML. My goal is to have two input fields, named Fld1 and Fld2, that would add up the values entered into them when submitting. However, despite parsing the input val ...

What is the best way to ensure that this header remains adaptable to different screen

Looking for guidance on creating a fluid and responsive header design similar to the curved images shown at this link or this link. How can I ensure that my design maintains its shape without distortion when viewed on different devices? Any suggestions a ...

What is the best way to use border radius on a canvas element similar to a div in order to create a

I'm having trouble making a canvas element appear as a circle like any other div. Here is my code, please take a look and let me know what I'm doing wrong with the border-radius property: <canvas id="myCanvas" width="200" height="200" style ...

Error: AngularJS: Invalid Argument Error. The argument 'ClientCtrl' is not defined as a function, it is currently undefined

As a newcomer to AngularJS, I am facing an issue while trying to add a controller to my website. Strangely, the other two controllers are functioning perfectly fine, but this particular one is not being recognized. Here is my app.js file: var app = angul ...

Assistance needed in restructuring code to eliminate white space between Divs

I am looking to combine two divs in the header section of my website. Both divs should have a black background with no white space between them. Can anyone provide advice on this? Additionally, I would appreciate any recommendations on how to structure my ...

Layered parallax scenery

I'm interested in creating a parallax effect similar to this example. https://medium.com/@PatrykZabielski/how-to-make-multi-layered-parallax-illustration-with-css-javascript-2b56883c3f27 However, I find the use of HAML and Coffeescript in the mentio ...

Tips for vertically aligning elements within the body while excluding the navbar from being centered

On my webpage, I have a navigation bar and a lot of content. The code structure looks something like this: .container { display: flex; justify-content: center; align-items: center; } <div class="navbar"> <!-- Various links for navigation ...

Having trouble getting the height to work properly on your Blogger Blogspot template?

Issue with embed height=100% not working <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <head> & ...

As a newcomer to Javascript, I am currently working on developing a CRUD-based application and could use some assistance with implementing the Edit functionality

I am currently in the process of developing a Javascript CRUD application that showcases data within an HTML Table. I have successfully implemented the Create, Read, and Delete functions. However, for the Edit function, I am looking to display the data in ...

Refinement of website footer alignment

As a web designer, I am experiencing difficulty in adjusting the footer on my website. I would like the footer to be fixed at a specific height and remain in the same position unless the content increases, in which case it should move down accordingly. Can ...

Designing an interactive HTML table that adapts to various screen

Currently utilizing Bootstrap to create a responsive HTML table on smaller devices like the iPad, but seeking a more polished and professional alternative. Searching for a JQuery/JavaScript or CSS solution without relying on plugins. Would appreciate any ...

How can I make my navbar visible once a specific section has been reached?

Is there a way to conditionally display my navbar in react only when a specific section is reached? Currently, I am monitoring scroll position but this method becomes unreliable on larger screens due to varying scroll positions. I need the navbar to beco ...

Issue with Angular 6: Animation with LESS is failing to work post deployment

Hello everyone, I'm facing an issue with my LESS animation. When I deploy my app on the server after using ng build --prod, the CSS animations are not visible in browsers. They work perfectly fine on localhost but fail to work after deployment and I c ...

Error encountered during GSTIN validation in the Polymer JS console

I'm currently working on a Polymer JS code that validates GSTIN: <dom-module id="gst"> <template> <div> <label for="gstid" class="gstlabel">GSTIN</label> <input type="te ...

Utilizing Conditional Styling for an Array of Objects within a Textarea in Angular 6

My array contains objects structured as follows: list =[ { name:"name1", value:true } { name:"name2", value:false } { name:"name3", value:true } { name:"name4", value:false ...

Setting objects in parallel in HTML involves using the display property along with the value

Creating two sign-up forms using HTML and CSS Bootstrap. Want them to be parallel, tried using the position tag but there is unwanted space created between the forms. View the output How can I prevent this circle of space from being generated? <main cl ...