Maintaining an even distribution of flex children on each line as they wrap to the next line

I am curious about finding a way to maintain an even distribution of elements on each line, including the last one, using flex-wrap or any other flexbox technique.

For example, let's say I have a flexbox with 6 elements. When it wraps, I would like to have 3 elements on the first line and 3 on the second, or for both lines' total width to be approximately equal.

I don't want to resize the children or compromise the functionality of flexbox.

Currently, I am only able to achieve 5 elements on the first line and one on the last line.

ul {
    display:flex;
    justify-content:space-between;
    flex-wrap:wrap;
}

You can view an example on jsfiddle here: https://jsfiddle.net/yfj2g7wx/

I believe that maintaining an even distribution across lines could enhance the visual appeal rather than wrapping elements individually.

Is there a solution to achieve this?

Answer №1

To control the size of flex items at different screen sizes, you can utilize media queries. For instance, at a specific breakpoint, you might want to set flex-basis: 33% to have three items on each line.

@media screen and ( max-width: 900px ) {
  li { flex-basis: 33%; }                    /* displaying three items per line */
}

@media screen and ( max-width: 600px ) {
  li { flex-basis: 50%; }                    /* showing two items per line */
}

@media screen and ( max-width: 300px ) {
  li { flex-basis: 100%; }                   /* one item per line */
}

Check out the jsFiddle example here

Answer №2

To ensure an equal number of elements per row, divide them into two sets of three elements each. This way, you will always have either a complete 6-element row or two rows with 3 elements each. This arrangement also maintains a nice symmetrical look with 'space-between' on both rows. I made some modifications to your Fiddle:

  • Separated the last 3 <li>s into a new <ul>.
  • Wrapped both <ul>s in a <section>.
  • Turned the <section> into a flex container.
  • Applies flex to both <ul>s.

    section {
      display: flex;
      justify-content: space-between;
      flex-flow: row wrap;
    }

Fiddle

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 can you center popup windows launched from the main window using jquery?

Within my web application, I frequently utilize popup windows that open at different locations on the screen. These popups are standard windows created with window.open() rather than using Jquery. Now, I am seeking a solution to automatically center all p ...

Is there a way to change an HTML document into a Word file?

I am looking for recommendations on libraries that can help me save HTML documents in memory as Word .DOC files. Any suggestions for both closed and open source options are welcome. Additionally, could someone provide links to these libraries based on the ...

Tips for optimizing Markdown to HTML conversion for improved SEO in Pagedown editor

Through the pagedown editor, I am transforming markdown into HTML using the method below: <div id="question_div" class="post_text"> </div> <script type="text/javascript"> $(document).ready(function() { var q_converter = new Markdo ...

Chrome on IOS: Experiencing screen freezing while scrolling

1) I'm working with Material UI in React JS. I have added a simple scrollable code, but when I reach the bottom of the screen/scroll, it freezes. 2) I also tried it with a simple HTML page and it worked correctly. <div style={{ ...

Utilizing Node.JS Nodemailer for sending emails with extensive HTML code

I am encountering an issue. I am working with Node.JS nodemailer. const url = `${url}`; var mailOptions = { from: 'stackoverflow', to: email, subject: 'test', html: 'Please click this email to confirm your email: &l ...

Why is my <a> element not functioning properly?

I'm encountering a small issue with my first HTML website. The hyperlinks in the "about me" section are not clickable, yet those in the drop down menu are functioning properly. Any thoughts on why this might be happening? If you'd like to take a ...

Angular 4 allows the addition of an image from right to left upon clicking

I've been working on angular 4 and I've successfully implemented the functionality of adding flags. However, the issue is that the flags are currently appearing from left to right. What I'm aiming for is to have the images appear from right ...

Saving HTML form field data through erb for persistent storage

Encountering an issue with erb while trying to populate the value of an HTML form field. The situation involves a scenario where the user has inputted an incorrect password and we want them to retain their name/email address information without having to r ...

Tips for enhancing webpage load times by optimizing CSS and jQuery file handling

As a beginner in HTML and CSS, I am facing slow loading times when testing my pages on a local server. This makes me concerned about the potential loading time on the internet. Currently, I have all the files included in the <head> tag at the top of ...

What steps can be taken to automatically set a button in a navigation bar to active when its corresponding route is clicked on?

I'm trying to implement a feature in my react app where an SVG button in the navbar changes color when its route is active or switches colors based on which button's route was selected. For example, clicking on the settings button would change it ...

Struggling with html code?

Struggling with some school work and facing a challenge. I need the menu to be centered, but it's not cooperating. My teacher was no help, so I'm turning to this website for assistance. This is what I have come up with: * { background-col ...

The waveform's bottom appears distorted when using Bootstrap

I'm currently experimenting with creating a header wave in Bootstrap CSS using SVG shape. However, I encountered an issue when implementing bootstrap into the HTML code - my design resembles the second image instead of the desired look of the first on ...

JQuery Mobile header style vanishing when applied to a page generated dynamically

My second page retrieves data from an Ajax call on the home page, but the header lacks JQuery styling and I suspect a connection between the two. Here is the HTML for the dynamically generated page: <div data-role="page" id="breakdownDialog" data-add-b ...

Having difficulty automating the selection of dropdown menu using Selenium in Python

I'm struggling to automate the selection of a specific element on this webpage using Selenium in Python. The elements I'm targeting from the webpage are as follows: Upon inspecting the page element, I found this portion of the HTML markup: < ...

disable caching for xmlhttp request

One issue I am facing is with a JavaScript function that retrieves JSON data via an Ajax request. The problem I'm encountering is that the browser starts caching the response to this request, which means I am not getting the most recent data from the ...

What could be causing the misalignment of these two divs?

I'm struggling to horizontally align these two divs using percentages for width. Even with padding and margin set to 0, they refuse to line up properly (the second one ends up below the first). This is the code I have been using: <div style="widt ...

Ajax facilitates the loading of multiple containers

My goal is to utilize Ajax to load dynamic content. The process starts with a list of countries. When a country is selected, the cities in that country are displayed, followed by suburbs, companies, branches, and eventually branch details: Countries --> ...

MySQL automatically inserts a null record if no value is provided

Currently, I am working on a project that involves inserting, viewing, and deleting records. Although everything functions smoothly, an issue persists where a null record is automatically added for some reason. The deletion feature works perfectly for othe ...

The event listener for jQuery's document.ready doesn't trigger, rendering all jQuery functions ineffective

After researching similar issues on various forums, I have attempted the following troubleshooting steps: replacing all $ with jQuery ensuring the correct src is used implementing jQuery.noConflict() My goal is to retrieve data from a SQLite3 database w ...

Which CSS preprocessor is the best choice for WordPress: SASS or

After comparing SASS and LESS, I found that SASS is the clear winner in my opinion. Unfortunately, setting up SASS on my server requires ruby, gems, and other tools that I don't have access to. On the other hand, it seems like adding LESS to my proj ...