What is the best way to ensure that a css grid using grid-template-columns: repeat(auto-fit, minmax(x,y)) distributes items as evenly as possible across each row?

I'm in the process of creating a CSS grid. Here is a snippet of the CSS I am using:

div.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    }

Within my HTML code, there are exactly 6 items:

<div class=stats>
    <div>Item 1</div>
    <div>Item 2</div>
    <div>Item 3</div>
    <div>Item 4</div>
    <div>Item 5</div>
    <div>Item 6</div>
</div>

When viewed on a phone, it displays as a neat 2 columns layout:

| Item1 | Item2 |
| Item3 | Item4 |
| Item5 | Item6 |

On a widescreen device (PC or horizontal tablet), it transforms into a sleek 6 columns layout:

| Item1 | Item2 | Item3 | Item4 | Item5 | Item6 |

However, when viewed horizontally on a phone, it shows 5 items in the first row and 1 item in the second row:

| Item1 | Item2 | Item3 | Item4 | Item5 |
| Item6 |

And when viewed vertically on a large screen (like a tablet), it arranges as 4 items in the first row and 2 items in the second row:

| Item1 | Item2 | Item3 | Item4 |
| Item5 | Item6 |

Is there a way to instruct the CSS to consistently maintain an equal number of items in each row, such as having 3 items in both the first and second rows for "middle ground" screens?

| Item1 | Item2 | Item3 |
| Item4 | Item5 | Item6 |

I have experimented with various options like auto-fill, minmax(3fr, 1fr), percentage values, different grid-template-rows values, and even tried masonry layouts, but haven't found a solution yet.

Answer №1

If you find yourself in a situation where you need to cater to both desktop and mobile screens, you can implement the following CSS rules.

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

For smaller screens like mobile devices, you can adjust the layout with this CSS code.

@media only screen and (max-width: 767px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

Your code should look something like this:

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
@media only screen and (max-width: 767px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
<div class=stats>
    <div>Item 1</div>
    <div>Item 2</div>
    <div>Item 3</div>
    <div>Item 4</div>
    <div>Item 5</div>
    <div>Item 6</div>
</div>

You can now test the layout on different screen sizes, add more items in your HTML, and modify the column count in the CSS as needed.

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

There seems to be an issue with the item display page when clicking on the 'view details' button within the Bootstrap tab

Encountering a problem with displaying the product details page. There are four bootstrap tabs, each containing some Product Items with a Quick View link button. The issue arises when clicking on the link button, as all tab items with the same ID are displ ...

What is the reason for Internet Explorer making a POST request upon clicking <a><button/></a>?

Here's the structure of my form: <form method="post"> {% csrf_token %} <table> {% for field in form %} {% partial "partials/field.html" field=field %} {% endfor %} <tr> <td& ...

Guide to setting up a custom js file in Laravel admin template

Currently working with Laravel 5.8 and utilizing the laravel-admin Template for administrative purposes. There are times when I require custom JavaScript and CSS files specifically for certain admin controllers. How can I include these JS and CSS in lara ...

Using JQuery to Execute Matching Based on Text Within <A> Elements

After reviewing the resources on Jquery Extract URL from Text and jquery match() variable interpolation - complex regexes, I am still a bit confused. The issue at hand is that I have a webpage with a dropdown menu at the top. Inside the dropdown, there ...

Combine several elements in a single jQuery scrollreveal function

I am currently working on a webpage that utilizes the jQuery library plugin scrollreveal to gradually reveal different html elements when the page loads. The functionality of the code is working correctly at the moment, but I have noticed that there is a d ...

Get the Bootstrap download that includes CSS files rather than the LESS files

What is the reason behind bootstrap switching to only having LESS files instead of the traditional bootstrap.css file? Is there a way to download the bootstrap framework that includes the CSS files instead of just the LESS files? ...

Insert a new <tr> element into a dynamic table using PHP and jQuery without the need to refresh the page

I am attempting to dynamically insert a row into an existing table when a button is clicked. The rows in the table are created dynamically based on data retrieved from a PHP script. My approach involves making an ajax call to the insert_tr.php script, whi ...

Learning the ropes of off-canvas design with Bootstrap 3

I'm really struggling to understand how to implement off-canvas functionality in Bootstrap 3. I've searched for examples, but I just can't seem to grasp it. My navigation is set up, but I'm unsure how to make the off-canvas feature work ...

Can the arrangement of icons/buttons on the JW Player control bar be customized?

I am trying to customize the skin of my JWplayer like this: This is my current progress: I have been researching how to rearrange the order of icon / button on the controlbar. According to the jwplayer documentation, the buttons on the controlbar are div ...

Designing a dynamic hamburger menu featuring multiple levels of buttons that open up additional layers of options

I'm trying to implement an animated hamburger menu with expandable buttons in the next level. While I know my code is far from achieving that, here's my progress so far: https://jsfiddle.net/TheBB23/hnsjym9u/ This code was borrowed from a webs ...

Switch up the color of the following-mouse-div in real-time to perfectly complement the color that lies underneath it

I am trying to create a div that changes color based on the complementary color of whatever is underneath the mouse pointer. I want it to follow the mouse and dynamically adjust its color. This functionality is similar to what Gpick does: https://www.you ...

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</ ...

Tips for eliminating the horizontal scroll bar on a responsive background image

I'm having an issue with a full-width div containing a background image on my responsive website. Everything looks fine on desktop, but when I switch to responsive mode using Chrome Dev Tools, a horizontal scroll bar appears that I can't remove w ...

My CSS horizontal drop-down menu is causing the sub-navigation items to overlap each other

I'm having an issue where my sub navigation menu items are stacking on top of each other instead of displaying properly. Here is the code snippet causing the problem: /* STYLING FOR NAVIGATION MENU */ .nav-bar { width: 100%; height: 80px; ...

Tips for running multiple JavaScript functions in ASP.NET using Button's OnClientClick property

Is there a way to execute multiple JavaScript functions in ASP.NET to perform various tasks such as inserting a desired text in a TextBox, changing the TextBox background color and font color, and disabling or locking a Button for a specific duration? I ha ...

Converting data from Random.org into an integer using JavaScript

I have been working on a small web application to experiment with CSS animations. Although it's functioning, I'm seeking more genuine randomness. To achieve this, I am exploring the use of Random.org. How can I import the output from Random.org i ...

Looking to implement a condition that prevents the echoing of HTML code when a certain template ID is selected?

Struggling with inherited PHP MVC code for a website, I encounter an issue on a specific page where unwanted code is echoed out. To prevent this, I aim to skip the echoing when a specific templateID (which I set as default) is selected. However, I'm u ...

I need to create a login form using Angular - what steps should I follow?

I'm currently in the process of developing a login form for my website. Utilizing angular, express, and mongoDB. Below is the login function controller I have implemented: loginUser: (req, res) => { User.findOne({ username: req.bo ...

Stop iPhone body scrolling when fullscreen overlay is opened

My goal is to prevent the body from scrolling when my fullscreen overlay navigation is open. I have added a class show-nav to the body with the CSS attribute overflow: hidden, which works perfectly on desktop but not on iPhones. After researching similar ...

Calculating available balance by subtracting the entered amount from the existing balance in Python Django

In the user profile model within my Django project, there are fields for available balance and current balance. I am looking to deduct an input amount from the current balance in order to display the updated available balance on the user's web page. ...