What is the best way to ensure a flexbox image item keeps its aspect ratio?

After exploring numerous questions and answers, it seems that most discussions on flexbox aspect ratio involve using <div> elements instead of <ul><li>. However, I am struggling to find a solution to my specific issue. When utilizing <div> tags, the images do not remain on the same line and instead drop down the screen, which is not my desired outcome.

I aim to have a row of 4 images that stay in a row regardless of screen size, without causing horizontal scrolling, so I have turned to flexbox for assistance.

Despite various attempts, when displayed on smaller screens, the images' width adjusts correctly but their height remains unchanged, resulting in distortion.

I have experimented with flex-shrink: 1; on all items, as well as employing min-height: 0;, min-height: 1px;, and align-items: stretch;, yet none of these adjustments seem to resolve the height issue alongside width reduction.

Here is an excerpt from my HTML code:

.flexbox-holder{
  padding: 0;
  margin: auto;
  list-style: none;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  min-height: 1px;
}
.flexbox-item{
  overflow: hidden;
  flex-basis: 100%;
  max-width: 240px;
  min-width: 1px;
  height: 100%;
  max-height: 150px;
  min-height: 1px;
}
<ul class="flexbox-holder">
    <li class="flexbox-item"><img src="images/service1.jpg" alt="service1"></li>
    <li class="flexbox-item"><img src="images/service2.jpg" alt="service1"></li>
    <li class="flexbox-item"><img src="images/service3.jpg" alt="service1"></li>
    <li class="flexbox-item"><img src="images/service4.jpg" alt="service1"></li>
</ul>

Answer №1

Acknowledgements

Credit and gratitude go to the author of this insightful answer on Stack Overflow. The solution provided is specifically tailored to address the issue discussed in the original post.

Proposed Solution

To tackle the problem, incorporate the object-fit CSS property into your image data, as illustrated in the code snippet below.

.flexbox-holder{
  padding: 0;
  margin: auto;
  list-style: none;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  min-height: 1px;
}
.flexbox-item{
  overflow: hidden;
  flex-basis: 100%;
  max-width: 240px;
  min-width: 1px;
  max-height: 150px;
  min-height: 1px;
}

.flexbox-item img {
  border: solid 1px red;
  max-height: 100%;
  max-width: 100%;
  object-fit: scale-down;
}
<ul class="flexbox-holder">
    <li class="flexbox-item"><img  src="http://collapsar.bplaced.net/samples/png/240x150.black.png" alt="service1"></li>
    <li class="flexbox-item"><img  src="http://collapsar.bplaced.net/samples/png/240x150.black.png" alt="service2"></li>
    <li class="flexbox-item"><img  src="http://collapsar.bplaced.net/samples/png/240x150.black.png" alt="service3"></li>
    <li class="flexbox-item"><img  src="http://collapsar.bplaced.net/samples/png/240x150.black.png" alt="service4"></li>
</ul>

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 to vertically center a card between a search bar and the bottom of the viewport using Bootstrap?

I'm struggling to center a card vertically between the search bar and the bottom of the viewport. Even after setting the body height to 100%, the content extends beyond the viewport, leaving white space that users can scroll into. I've attempted ...

Adjusting input height with CSS in Google Chrome

When I define an input element with the following CSS properties: input { border: none; font-family: arial, sans-serif; font-size: 16px; line-height: 16px; padding: 0; } I anticipate the height of the input to be 16px, however, upon inspecting ...

Determine whether the Bootstrap data-style attribute is enabled or disabled

I have a bootstrap checkbox that I would like to trigger an alert dialog when it is switched off. <div class="row"> <link href="css/bootstrap-toggle.min.css" rel="stylesheet"> <script src="javascript/bootstrap-toggle.min.js ...

Having trouble inserting a hyperlink using the <a> element into a list item (<li>) tag

I have developed a partial view that displays a menu using an unordered list. I am trying to add links to each list item but regardless of whether I use relative paths or absolute paths, the linked pages are not opening and I keep seeing a 404 error in my ...

Issue with word-wrapping in a CSS Grid element

My paragraph text lacks line breaks and needs to wrap onto a new line when the width exceeds that of the grid container. It works fine without using a grid. I attempted the method suggested in Prevent content from expanding grid items but had no luck. Ano ...

Problematic: BS4 Cards cannot be accommodated within the parent div.Improved: The

Within a dynamic-height parent div, I have two cards stacked on top of each other. However, the issue arises when these two cards do not completely fit inside the parent div. The problem likely stems from the table within the lower card, which is not respo ...

What steps can be taken to repair the footer area within this CSS document?

As a beginner ASP.NET developer, I am encountering CSS for the first time. The CSS file I have is fairly simple, but I am facing difficulties with the footer section. The current appearance of the footer is as follows: I am looking to make the following m ...

enigmatic margin surrounding navigation dots

I have added a straightforward dot navigation to the banner of my website. You can check it out HERE. The HTML code I used is shown below: <ul class="carousel-dots"> <li> <a href=""></a> </li> <li> ...

Header Text unexpectedly breaking out of Div in Mozilla Firefox, but not in Chrome or IE

Need some help with getting the h5 text to show up inside the div with id="text". Any advice would be appreciated! Thanks for taking the time to read this! <div id="footer"> <div id="instagram"> <div id="text"> &l ...

Verify the presence of a specific select option using text in jQuery version 1.7

Looking at the following snippet of HTML: <select id="sel"> <option value="0">Option1</option> <option value="1">Option2</option> <option value="2">Option3</option> <option value="3">Option4</option> & ...

Input field with ruled lines beneath each line of text

Can you assist in creating a textarea that displays lines under each row of text similar to the example shown in the image below? ...

Steps for adding a Bootstrap Navbar Dropdown1. Begin by creating

Looking for tips on how to incorporate a dropdown navbar using Bootstrap 4. I've utilized the .navbar-bottom class to achieve a horizontal layout.** The .navbar-bottom class is used to include a scrollbar in the navbar** .navbar-bottom { overflow ...

Utilizing Jquery to Load JSON Arrays as Tooltip Messages

I have successfully loaded the descriptions of my JSON variable into my script as tooltips. However, I am encountering two issues: When hovering over one sentence in my table, tooltips for all four sentences appear. It seems like I am unable to revert my ...

Jquery unresponsive in AJAX form output presentation

I recently delved into the world of jquery and AJAX, and while I grasp most concepts, I'm struggling with a small code snippet. On my webpage, there is a summary of articles. Clicking on an article name triggers a popup window with detailed informati ...

Summing numbers inputted into text boxes generated by a looping mechanism

Currently, I have a function that uses a for loop to generate multiple text boxes dynamically. Each row consists of three text boxes created using the same ID, all within an HTML table structure. var txtBox = "<input type='text' id='txtN ...

The complexities of stopPropagation() causing further confusion

I have encountered an issue with my code. It currently allows a dropdown functionality for a <ul> element, but when a child <li> is clicked, it also closes other menus of the same type. To fix this, I used an if clause to check if the clicked ...

Angular 2 is all about managing validation messages within controls

I'm currently facing challenges with getting validation messages to display properly in Angular 2. The frequent updates to Angular 2 have made it difficult to find a simple and effective solution online, so please refrain from marking this as a duplic ...

Issue with aligning items vertically using CSS and Javascript

I must admit, I am not well-versed in CSS. My goal is to create a performance bar using CSS and Javascript. So far, I have managed to generate a background bar with another bar inside that fills up to a specific percentage. However, my issue lies in the fa ...

Using Jquery to encase an element in a div while scrolling down and removing it while scrolling up

After some experimentation, I've managed to wrap an element inside a div using jQuery. My next challenge is to wrap it as you scroll down and unwrap it as you scroll up. Do you think this is achievable? Although I have succeeded in wrapping it while ...

"Troubleshooting: IE compatibility issue with jQuery's .each and .children functions

I have created an Instagram image slider feed that works well in Chrome, Safari, Firefox, and Opera. However, it fails to function in all versions of Internet Explorer without showing any error messages. To accurately determine the height of images for th ...