Ensure that the flex items are utilizing the full height of the parent when the flex-direction property is set to row

I'm currently working on a layout with 3 flex items in a flex container, each having equal height and width. While I've managed to achieve equal width for all 3 items, there's an issue with the first item. It contains a div (.images) that serves as another flex container with more children than the other two items' .images div. This causes the first item to have a larger height compared to the other two. Is there a way to make the height of the remaining two flex items match the height of the first item, even though they don't have the same number of children? Most solutions I found address this problem when the flex-direction is set to column, but in my case, it's set to row within the flex container.

body {margin: 0}

.flex-container {
  display: flex;
  flex-direction: row;
  width: 100%;
}

.flex-items {
  width: 33.333%;
  height: 100%;
}

.images {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  height: 100%;
  justify-content: center;
}
<div class="flex-container">
  <div class="flex-items">
    <h1>Some Title</h1>
    <div class="images">
      <img src="image1.jpg" alt="">
      <img src="image2.jpg" alt="">
      <img src="image3.jpg" alt="">
      <img src="image4.jpg" alt="">
      <img src="image5.jpg" alt="">
    </div>
  </div>
  <div class="flex-items">
    <h1>Some Title</h1>
    <div class="images">
      <img src="image6.jpg" alt="">
      <img src="image7.jpg" alt="">
    </div>
  </div>
  <div class="flex-items">
    <h1>Some Title</h1>
    <div class="images">
      <img src="image8.jpg" alt="">
      <img src="image9.jpg" alt="">
    </div>
  </div>
</div>

Answer №1

To ensure that the images within the .images container scale proportionately, set their height to 100% and width to auto:

.images {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    height: 100%; (the .images container has 100% height, but its children are not instructed to adjust their height)
    justify-content: center;
}

.images img { (targeting the child elements)
  height: 100%;
  width: auto;
}

Answer №2

When images are not given a specific width and height, they will default to auto and scale accordingly within the flex box based on the browser being used.

Images can be adjusted either inline or through CSS by setting the height or width properties. Here are examples of both methods:

Inline Example 1:

<img src="#.png" width="auto" height="100%" />

CSS Example 2:

div.images > img {  
 height: 100%;  
 width: auto;  
}   

Optimizing images for a web page is recommended instead of solely relying on auto scaling for optimal results. Ultimately, you have insight into what the final outcome should look like.

:)

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

Is there a way to determine the origin of a CSV file shared through a GitHub link?

I'm a beginner on Github and I need help figuring out how to trace back the origin of this CSV file: https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv Can someone guide me on how to locate where it was uploaded? ...

Generate a bill based on items in a virtual shopping cart

I am having trouble with formatting the output of a shopping cart to an invoice. The data is displaying, but it's not showing up correctly. https://i.sstatic.net/eCKs3.jpg Here's the code snippet: <table class="table table-striped"> ...

Shifting the Bootstrap sidebar

Here is a layout defined using Bootstrap: <div class="container-fluid"> <div onclick="smallSize()">Click</div> <div onclick="largeSize()">Click</div> <div class="row"> ...

Positioning HTML elements using CSS and avoiding the use of tables

I'm struggling with my clear CSS declarations. This is how I'd like it to appear: View the Fiddle demo HTML: <div id="timesheeteditor"> <div id="weekselector"> <div>Week 1</div> <div>Week 2</div> ...

Steps for updating a text section beneath a carousel

I'm looking to enhance my webpage with a bootstrap carousel, which is pretty standard. However, I want the content under each slide to change dynamically, but I'm struggling to make it work. As a newbie, I could use some guidance. I've atte ...

What is the best way to target CSS only to elements that do not have a parent with a specific class?

Striving to preserve the existing CSS in the codebase. .my-new-class { .existing-class { ...implementing new styles } } .existing-class { ...maintaining old styles } I attempted a technique that I know won't work and understand why: ...

AngularJS - Changing Views with Templates

At the moment, I am changing my directives in the following manner. Within my directive: (function(){ 'use strict'; var controller = ['$scope', function ($scope) { }]; angular .module('moduleName' ...

Switching the navbar image with HTML and JavaScript when clicked

Looking to change the image upon clicking on any of the navbar items. Emulating the navigation bar behavior from this website : This is my current progress : The HTML file : <html lang="en"> <head> <meta charset="utf-8" /> ...

Preserve the HTML file with all its source code intact

Looking to access a specific parameter on this webpage: . For instance, if I need to retrieve the "Topological Polar Surface Area" value. If I manually save the page using Internet Explorer, I can locate the value with these commands: cat file.html | gr ...

Changing the click event using jQuery

My JavaScript snippet is displaying a widget on my page, but the links it generates are causing some issues. The links look like this: <a href="#" onclick="somefunction()">Link</a> When these links are clicked, the browser jumps to the top of ...

How to Arrange Information Harvested from While and For Loops?

I am attempting to display the posts of a user's friends, similar to the layout of Facebook's main page. Currently, to echo out a user's friend's posts, the database table for that user contains a reference to a text file. In my case, t ...

Issue with thymeleaf causing malfunction in top-bar navigation on foundation framework

Looking for advice on creating a navigable menu using Foundation's "top-bar" component in an HTML template file with Spring MVC + thymeleaf. The menu bar appears but the sub-menus are not displaying when hovering over them. Sub-menus related to main ...

Shrinking the image within a card row in Laravel/Bootstrap 5

My issue involves creating a row of Bootstrap cards using Laravel. When I add the "row" class, the images in the cards shrink. However, if I remove the "row" class, the images display perfectly. How can I fix this problem? <h1>Vehicles</ ...

What is the best way to create a functional menu placed behind a PNG image using CSS?

Looking to add a PNG image to a menu, similar to the one showcased below: https://i.sstatic.net/YOXpM.jpg My current approach involves using position: absolute and z-index, but I'm encountering issues when the menu ends up behind the PNG image. Is t ...

Validation of multiple textboxes can be achieved by utilizing either JavaScript or Angular 1

How can I implement a validation in Angular 1 or JavaScript that will enable the submit button only when there is text in any of the 3 textboxes? If no text is present, the button should be disabled. Below is the code snippet: First name: <br> < ...

The importance of precision in a written text

When it comes to being specific, I usually pride myself on my skills. However, there's one particular challenge that I can't seem to crack. Here's the scenario: Imagine you have a list structured like this: <ul class="dates"> < ...

Incorporating a hyperlink into an HTML submit button

Recently, I started working with MySQL and decided to create a basic registration page. However, upon clicking the submit button, I would like to be redirected to another page, possibly a login page as seen on many websites. I am struggling with figuring ...

JavaScript is currently not executing properly. According to Firebug, the message states to "reload the page to obtain the source for: ..."

I'm currently working on creating a bookmarklet that adds some JavaScript elements to the head of a webpage. However, I've noticed that one of them is not loading properly. Is there a solution to this issue? Is it possible to reload the page wit ...

Can a variable containing HTML code be iterated through with a loop?

I am currently working on setting up a select button that will receive options from an ajax call. The ajax call is functioning properly. However, I am facing a challenge when trying to create a variable containing the HTML. I am unsure of how to iterate a ...

Switching the file format from HTML to .ASP resulted in the loss of the website's design elements and images

After initially creating an HTML file, I decided to rename it with a .asp extension for uploading online. To do this, I copied the original HTML file and moved it into a folder containing .asp files. However, after replacing the existing file, my website l ...