Ensure that the images in the final row of the flexbox gallery are consistent in size with the rest

I'm working on creating a CSS flex image gallery that adjusts the width of the images based on the container size:

HTML:

<div class="grid-container">
    <div class="grid-item">
        <img src="small-thumb-dpi.jpg" />
        <div>this is the item description</div>
    </div>

    ...(more items here)

</div>

CSS:

.grid-container {
  display: flex;
  flex-flow: row wrap;
}

The issue arises because I'm using "flex: 1 1 auto;" for the gallery items, like this:

.grid-item {
    flex: 1 1 auto;
    width: 236px;
    margin: .35vw;        
}

The current code creates the desired flex grid layout, but the images on the last row expand to fill the available space as shown in the example below:

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

My code snippet:

body {
  margin: 0;
  padding: 10px;
}
.grid-container {
  display: flex;
  flex-flow: row wrap;
}
.grid-item {
  background-color: lightgray;
  flex: 1 1 auto;
  width: 300px;
  margin: .45vw;
  padding: 10px;
  box-sizing: border-box;
}
.grid-container img {
  width: 100%;
  height: auto;
}
<div class="grid-container">
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
    <div classs=item-description>This is the item description</div>
  </div>
</div>

Codepen

In summary, I am looking to keep the images on the last row the same size as the others while maintaining the flex properties. How can I achieve this?

Answer №1

Change the size of the .grid-item and include spacing for gutters if desired.

.grid-container {
    display: flex;
    flex-flow: row wrap;
    width: 100%;
}

.grid-item {
  width: 30%;
  padding: 15px;
  box-sizing: border-box;
  background-color: #f0f0f0;
}

Answer №2

Why not spice things up with a sneaky ninja donkey?

  • Give each donkey a flex-basis value of 25% instead of auto.

    flex:1 1 25%
    
  • Then, throw in an extra donkey and make the ninja donkey invisible by adding visibility: hidden.

    .grid-item:last-of-type {
       visibility: hidden;
    }
    

By the way, I upgraded the divs to <figure> and <figcaption> tags for each donkey.

CODEPEN

SNIPPET

<!doctype html>
<html>

<head>
  <meta charset='utf-8'>
  <style>
    body {
      margin: 0;
      padding: 10px;
      box-sizing: border-box;
    }
    .grid-container {
      display: flex;
      flex-flow: row wrap;
    }
    .grid-item {
      background-color: lightgray;
      flex: 1 1 25%;
      width: 300px;
      margin: .45vw;
      padding: 10px;
    }
    .grid-item:last-of-type {
      visibility: hidden;
    }
    .grid-item img {
      width: 100%;
      height: auto;
    }
    figcaption {
      text-align: center;
    }
  </style>
</head>

<body>
  <div class="grid-container">
    <figure class="grid-item">
      <img src="https://pbs.twimg.com/media/CZsY7k0WAAA_sk5.jpg" />
      <figcaption>This is the item description</figcaption>
    </figure>
    ...
  </div>
</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

Phonegap enables iOS keyboard to dynamically adjust screen size during use

Currently, I am developing an iOS app using phonegap 3.0 and have encountered a particular issue. In my app, there is a window where users are required to enter a promo code. The problem arises when I click on the input area (see this example) and then pr ...

Align the asp.net content generated towards the left side

I am looking to optimize the layout of my asp.net code by shifting the content on all pages to the left side of the screen for better efficiency. Despite my efforts, I have been unable to find a suitable CSS solution that works universally across all view ...

How to utilize PHP to create a multiple select form for filtering results in a MySQL

I have been attempting to create a query using the results from a search form. In my form, I have a field called "state". My goal is to allow users to select multiple states and receive a list of results that include all the selected states. Currently, I o ...

Is the 404 error a result of the ajax code?

I'm currently working on a form that utilizes AJAX to validate and interconnect various form elements. Below is a simplified version of my code: <?php if( isset( $_POST['create_transaction'] ) ) { // do something } ?> <div> ...

IE8 is not properly handling nested HTML elements that have the display:none property set

I am currently using jQuery to create a smooth fade-in effect for an <article> element that contains a <section> element. The outer element has CSS properties of display:none, position:fixed, and z-index:5. Meanwhile, the inner element is styl ...

Unable to produce scrolling animation using JavaScript

I'm trying to implement a feature on my website where the page scrolls with a sliding effect when the user presses the "Scroll" button. However, I've encountered issues as it doesn't seem to work despite adding the necessary tags to my HTML ...

Is it possible to insert an image directly into the <img> tag without having to first send it to the server?

I've created an upload form that allows users to submit images: <form> <input accept="image/jpeg, image/gif, image/png" type="file" name="image" id="image" class="UploadInput" onchange="submitImageUploaderForm()"/> </form> Once t ...

Utilize import and export statements to transfer an HTML tag between two JavaScript files

I have two HTML files linked to two JS files. I want to save an HTML tag from HTML1 with JS1 in a Variable and export it. Then import it in the JS2 file and use it in HTML2 I have tried many ways but nothing seems to work, something as simple as this Exp ...

Using jQuery to access specific elements within the DOM by navigating through the document structure

I'm currently facing a challenge with using jquery's parents/siblings functions to locate specific input elements. Despite my efforts, I can't seem to get it right. Here is the HTML structure I am working with: <div id="ExtrasOptions"&g ...

Error with displaying uib-datepicker within a table row

I need assistance with integrating a uib-datepicker into a table: <div class="table-responsive"> <table class="table table-striped" > <thead> <tr> <th class="col-md-2"><span>Date ...

The sole focus is on the animation within the div

Is it possible to have a circle animation within a button without overlapping it? Check out my code on this fiddle. $(document).ready(function(){ $('button').on("mouseup",function(){ $('#mousemark').removeClass("c ...

The application is experiencing issues with loading Django's CSS files properly

When working on my Django application, I've noticed that the CSS code being loaded differs from what is written in the files. What could be causing this discrepancy and how can it be fixed? ...

What is the best way to ensure that only one div is toggled at a time while using the toggle class function?

$(document).ready(function(){ $("#items").children().click(function(){ $(this).toggleClass('clicked'); }); }); .clicked { background-color:red; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></s ...

CSS animation: Final position once the vertical text has finished scrolling

I have designed a CSS-animated headline with a leading word and three vertically-scrolling/fading statements to complete the sentence, inspired by the style used on careers.google.com. The animation runs through three cycles and stops, leaving the third st ...

Steps for adding a background image in ASP.NET MVC4

I recently removed the reference to the master page from the head section @{ Layout = null } After making changes in my CSS: html { background-image:url('Images\BGP.jpg'); } However, I didn't see any changes. Any suggesti ...

Tips for streaming an mp3 file on the internet

Is there a way to play an MP3 on a webpage without allowing users to download it? ...

Switching to a dropdown navigation option

Sorry for the repetition, but I haven't been able to find a solution to my problem yet, so here I am asking again. I am still getting the hang of html5 and css3, and while I've managed so far, I'm stuck now. I want to turn one of the option ...

My Ajax script is not recognizing the select tag value?

I am struggling with an ajax script that is supposed to send data from a contact form to a PHP script. The main issue I'm facing is that I can't seem to retrieve the value from the "select" tag. My knowledge of JavaScript/ajax is limited, so plea ...

The absence of button.onclick=func in the HTML code is notable

I am facing an issue where adding an onclick attribute to an input element via JavaScript does not display in the HTML, but the function gets executed. On the other hand, when I add the input element directly in the HTML code, the onclick attribute shows u ...

Extract ID for Bootstrap modal display

In my project, I am using a bootstrap modal that displays various strings. The challenge I am facing involves a loop of cards, each with a distinct 'id'. When triggering the modal, I want to show the corresponding id inside the modal itself, whic ...