What causes flex box to behave differently when transitioning from two columns to three compared to shifting from three columns to four?

I am currently developing an application that presents a variable number of cards to the user in a grid format. The divs defining these cards have both maximum and minimum widths set (375px and 250px, respectively) and are enclosed within a flex container.

.my-card-container {
    display: flex;
    flex-wrap: wrap;
}

.my-card {
    min-width: 250px;
    max-width: 375px;
    flex: 0 auto;
}

When there is only a single column, the cards expand until reaching 350px width and then stop growing. If the screen width exceeds 500px, the cards shrink to 250px each and two columns are displayed. The same resizing behavior occurs when transitioning from 2 columns to 3, with the columns expanding until the screen reaches 750px wide, at which point they drop down to 250px each forming a third column. However...

Once there are three columns, all the columns will resize to 350px width and leave excess white space on the right side. The cards do not decrease in size to accommodate additional cards. This arrangement remains with 3 columns until there is enough space for a fourth 350px column, repeating the pattern for a potential fifth column.

My objective is to replicate the behavior seen during the first two transitions, which prioritize maximizing the number of cards visible across the screen at any given width. I am struggling to comprehend why the transition from 3 columns to 4 is behaving differently.

Answer №1

When <strong>flex: 0 auto</strong> is applied, it gets parsed as:

{
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
}

Although the flex-shrink property allows an element to shrink below its normal width, it will not go below the value set by min-width. This causes the 4th item to be displayed on the first row when the parent's width is 1000px or larger, while the 3rd item does so at 750px.

This indicates that the parent's width is restricted (potentially by a rule defined on itself or any of its ancestors) to a range between 750px (for 2 to 3 items) and 1000px (for 3 to 4 items).

If you cannot isolate the factor limiting the parent's width below 1000px individually, you may need to create a mcve to demonstrate the issue, allowing for more precise identification.

Note: The desired behavior can be achieved with the provided code snippet below (additional rules are included to ensure child elements appear correctly):

.my-card-container {
    display: flex;
    flex-wrap: wrap;
}

.my-card {
    min-width: 250px;
    max-width: 375px;
    flex: 0 auto;
}

.my-card {
  min-height: 200px;
  border: 1px solid #eee;
  box-sizing: border-box;
}
<div class="my-card-container">
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
  <div class="my-card"></div>
</div>

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

Add a horizontal line between two div elements to create a visual division in the middle

Here is a snippet of my HTML/CSS/JS code: <div id="blockcart-wrapper"> <div class="blockcart cart-preview"> <div class="header"> <a rel="nofollow" href="#"> <img class="cart-icon" src="https://via.placehold ...

Angular JS: Automatically toggle the visibility of a div on a panel click event

There is a row, panel, and a closed div on load. Clicking on the panel should make the div appear while making the row and panel disappear. Clicking on X should make the panel and row appear again. <script src="https://ajax.googleapis.com/ajax/libs/ ...

Disregarding 'zIndex' directive within JavaScript function, an image stands apart

There is an issue with the z-index values of rows of images on my webpage. Normally, the z-index values increase as you scroll further down the page. However, I want certain items to have a lower z-index than the rest (except on hover). These items are i ...

Tips for ensuring that 3 divs fill the height of their parent container

I'm facing an issue with 3 dynamic content divs inside a parent container. I want them to all have equal heights and fill the parent container. To demonstrate my problem, I've set up a simple example on jsfiddle. .parent { overflow: hidden; ...

Even after setting a value to an HTML input field from the C# code behind, any modifications to the text are not being displayed

My form consists of input fields with the attributes type="text" and runat="server" to enable the code behind to access it. Users can input data and later edit the same data if selected from a list. However, after populating the form for editing and making ...

Is it better to use multiple external style sheets?

Hello! I am looking to implement a pop-up login screen by integrating downloaded code. The issue I am facing is that the CSS file that accompanies it clashes with my current one. Is there a way to have a specific style sheet only affect certain div tags, ...

Encountering a 504-loadbalancer error (Gateway Time-out) on PythonAnywhere after waiting for 5 minutes for a response, despite setting SQLALCHEMY_POOL_RECYCLE to 600

Currently, I am utilizing the SPOTIPY API to insert a user's song, album, artist, and user_info into a database. Approximately 4,000 records are being inserted in total. The database updates successfully, but after 5 minutes (response_time = 300 secon ...

Is there a way to display the month values in my dropdown menu using the date format of yyyy-mm-dd?

This is the code snippet I am working with: <form> <table> <tr> <td>Month</td> <td> <select name=""> <option value="">January</optio ...

Tips for managing a virtual URL within an img tag

I have a situation where I need to protect the src value of an img tag to ensure server security. One recommendation I received is to use a virtual URL approach: In this scheme, the img tag references a virtual URL like /uploads/myuser/mypict.jpg which isn ...

What is the best way to use ajax to load a particular div or class from a file?

In my main.html file, there are several divs each with 2 classes. The first is their specific class and the second is a class called menu-item, which determines if an event will be triggered when clicked. For example: <div id="load-here"> </div ...

Retrieve the data contained within a displayed embed tag that includes a src attribute

Can anyone provide guidance on how to retrieve the content of an embed tag using src attribute? I have an embed tag pointing to a custom page as src. I tried to use jquery to access the rendered content but I am not getting anything back. Any suggestions ...

Using a custom HTML file as a container in an Android WebView

I am trying to include each loaded page within my own custom HTML file. My app needs to first parse the loaded page, select the body tag, insert it into my custom HTML file, display it in a WebView, and continue this process for each subsequent page. I h ...

Is it necessary to capitalize the first letter only when it's not at the beginning of a sentence?

To separate a lengthy string, I followed the approach outlined in this post: .line { display: inline-block; } <p> <span class="line">This text should break</span> <span class="line">after the word "break"</span> </ ...

Creating dynamic button animations with Bootstrap 4 without impacting neighboring rows

I'm facing an issue with adding a simple animation to a button in one row of my webpage. When I change the margin-top of the button, all the rows below it also move along with the button. I tried using position-absolute on the button and position-rela ...

Is there a way to showcase a single HTML canvas across multiple div elements?

I'm currently developing a web application that includes a Google Maps feature in two different tabs using Twitter Bootstrap. I am exploring options to have one canvas element that can be displayed in both tabs with varying dimensions. One idea is to ...

Issue with Bootstrap Image Slider Carousel functionality not functioning as expected

I am facing an issue with my Django website where I am trying to display images using an image slider carousel. However, the code is not functioning as expected. Instead of a carousel, the images are appearing stacked on top of each other. https://i.sstat ...

Issue with Flexbox Layout - Flipping Card

As a newcomer to web development, my first project is a fan-page dedicated to Metallica. Here's the link to my project on Github. I'm currently facing an issue with the flex-box layout in the "Discography" section of the page. You can view the ...

What is causing some Font Awesome icons to not function properly with boostrapCDN?

I thought all Font Awesome icons would work by linking this bootstrap stylesheet, but I've only managed to get one of them (`.fa fa-deskto`) to display. The rest, like the second one for example (link below), simply don't show up: Both icons are ...

Unable to display HTML content on a page using the foreach callback in JavaScript

I have a function that iterates through each element of an array and generates HTML content on the page while updating some properties using elements from the array. I am utilizing forEach to iterate over the elements of the array and innerHTML to display ...

Tips for effectively passing a parameter in @url.Action

I stumbled upon this piece of code: <button type="button" class="btn btn-inverse btn-danger" onclick="@("window.location.href='" + @Url.Action("ActionName", "ControllerName") +"'");"> Link </button> for redirecting- it works gre ...