maximum height within a flexbox container

In my flex container, I have set the flex direction to columns and placed 3 divs inside it. The first and third div adjust their height based on content, while the second div (referred to as A) should occupy the remaining space with an unknown height.

Within A, there will be a random number of images with unspecified dimensions. These images need to form a single row arrangement, similar to a carousel, meeting the following criteria:

  • Take up the maximum possible area within A,
  • Maintain aspect ratio,
  • Each image should not exceed one-third of the visible width within A,
  • No gaps between the images.

While setting max-width: 33% easily fulfills the second condition, achieving the first one is proving challenging as max-height: 100% seems to not work effectively.

To view a demonstration, click here: http://codepen.io/alexandernst/pen/oxqBPv

This is the anticipated outcome:

|-------------------------------------------------|\
|                                                 | \
| |---------------------------------------------| |  \
| | up                                          | |   \
| |---------------------------------------------| |    \
|                                                 |     |
| |---------------------------------------------| |     |
| |                  @@@@@@@@@                  | |     |
| |                  @@@@@@@@@                  | |     |
| | @@@@@@@@@@@@@@@@ @@@@@@@@@ @@@@@@@@@@@@@@@@ | |     |
| | @@@@ 33 % w. @@@ @@@@@@@@@ @@@@ 33 % w. @@@ | |     |
| | @@@@@@@@@@@@@@@@ @@@@@@@@@ @@@@@@@@@@@@@@@@ | |     400px
| |                  @@@@@@@@@                  | |     |
| |                  @@@@@@@@@                  | |     |
| |---------------------------------------------| |     |
|                                                 |     |
| |---------------------------------------------| |    /
| | down                                        | |   /
| |---------------------------------------------| |  /
|                                                 | /
|-------------------------------------------------|/

Answer №1

I believe this is now aligned with your desired outcome: http://codepen.io/kbkb/pen/jqzBKJ

You were lacking a specified height for the parent div. Without a defined height, the max-height could not interpret what 100% meant as there was no reference point. (I have set the height to 88% based on fitting needs, but ideally you should calculate top and bottom heights and subtract them from 100%, such as `calc(100% - 20px)).

By using text-align:center, I have successfully centered the images.

In addition, I have included box-sizing:border-box to address the issue where adding a border of 1 px to an element would increase its height by 2 px. With border-box, the border is contained within the element's dimensions rather than added on top of the height.

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

Having difficulty retrieving a nested child element with querySelector in JavaScript

Encountering an issue with the querySelector method in JavaScript when accessing nested child elements. The goal is to retrieve the first child of a selected element and then obtain the first child of that inner element. Attempted code: let selected = doc ...

Positioning elements on the web page using CSS tiles

Greetings! I am currently working on a website that utilizes tiles similar to this: https://i.sstatic.net/OzKQv.png Everything seems to be in place, except for one tile that is not aligning with the rest. I am struggling to figure out how to position it c ...

AngularJS is designed to provide alternating colors specifically for the objects that are currently visible within the complete set

Within my angularjs application, I am working with the following JSON data: $scope.itemsList = { "busName": "ABC", "needsToHide": true, "num": 123 }, { "busName": "xyz", "needsToHide": false, "num": 567 ...

Using a dynamic image source in an Ionic 3 background

I am using ngFor to display a list of posts, each of which should have a unique background image. The getBackgroundStyle function is responsible for extracting the URL of the image from the post array. <div class="singlePost" *ngFor="let post of da ...

Changing the color of a specific span using Angular

I am working with a dynamic mat-table where columns are added and populated on the fly. The table headers are styled using divs and spans. My goal is to change the color of a header to black when clicked, but also un-toggle any previously selected header. ...

Video is not visible in safari browser initially, but becomes visible only after scrolling for a little while

Having issues with a video not displaying correctly in Safari? The problem is that the video only becomes visible after scrolling the browser window. Want to see an example of this issue in action? Click here and select the red bag. Check out the code sni ...

Accessing deeply nested JSON objects in AngularJS

I've been working on an AngularJS single page application and I have successfully fetched files from a JSON. var app = angular.module("MyApp", []); app.controller("TodoCtrl", function($scope, $http) { $http.get('todos.json'). success ...

Interactive phone photo gallery

I am looking to create a mobile webpage that allows me to swipe my finger left or right in the middle of the page to scroll between images. Here is an example photo: I want to be able to swipe through the images with my finger. Any suggestions on how I ...

Is the combination of elements by CSS Minifiers harmful?

Recently, I came across an interesting CSS minifier tool at . On that webpage, there is a section titled "What does the CSS Compressor purposely NOT do?" which highlights four specific things, two of which caught my attention due to their potential impact: ...

Tips for preventing the appearance of a horizontal scroll bar when utilizing the FlexLayoutModule in Angular

import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-property-feed', templateUrl: './property-feed.component.html', styleUrls: ['./property-feed.component.scss'] }) export class Prope ...

What is the best way to implement autoplay sound on a JavaScript webpage?

I'm currently working on a web system aimed at monitoring values from a database, and I have the requirement to trigger a sound alert when a specific range of values is received. Despite trying various examples found online, none of them have been suc ...

Is there a way to automatically load a page?

Is there a way to automatically load another page without clicking on a link? I have a website with PayPal options available, and I want to know if there is a way to load the PayPal code without the need for a click. Can anyone help with this issue? < ...

I need assistance organizing a set of cards on a webpage using Bootstrap 4. Can you help me with this task?

Looking for assistance in organizing cards on a web page using Bootstrap 4. Can anyone lend a hand? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4I ...

How do I modify the sidebar width with CSS code?

I am trying to adjust the width of the right sidebar on my website. The current layout leaves too much empty space on the right side and makes it appear crowded next to the main content and images in the center. Despite exploring various solutions provide ...

Interacting with the Dropdown feature on the page causes the body of the page to shift

I am encountering an issue with 2 dropdowns in Datatables used to filter content. The problem arises when a dropdown is positioned on the right side of the page, causing a shift to the left by approximately 10px. Conversely, if the dropdown is placed on th ...

parsing HTML code to find the Facebook page link using regular expressions

I am attempting to extract the Facebook page address from website HTML using a regular expression search. Typically, the link will appear like this: <a href="http://www.facebook.com/googlechrome">Facebook</a> However, sometimes the address ...

CSS - apply unique styles to specific nodes based on the class they share

Struggling to apply different styles to the same class when it appears for the second time in the markup using the Subsequent-sibling combinator "~". It seems like there may be a detail I'm missing with the use of "~". Unfortunately, the HTML cannot b ...

Modify the background color of a particular TD element when clicking a button using JavaScript and AJAX

When I click on the "Active account" button inside the designated td with the <tr id="tr_color" >, I want the value "1" to be added to the 'active_account' column in the database. I have been successful with this functionality. However, I a ...

The flex box container has an overflowing issue despite the implementation of overflow:hidden and setting a min-width of

Struggling to make my flex box container fit in the div Despite setting min-width: 0 and overflow: hidden as a last resort the flex box simply won't shrink when the width changes Check out what happens when the width is adjusted: https://js ...

How can I find the last element that was selected using XPath in the browser console

Need help with XPath: $x(("//div[@class='ag-header-row']))[1] I'm working with an array of divs, but I want to select the last one. The [1] is necessary due to multiple rows with this class. I’ve heard about using [last()], but unsure w ...