Overflowing CSS grid issue

I've been working on this for a couple of hours, but I haven't been able to get it right. I want to add labels in a grid layout with a maximum of 3 columns. If the items don't fit, they should wrap to the next row. It would be great if the items could reorder themselves to fit as many as possible in a row.

Here is the code snippet I have so far:

body {
  background-color: #555;
  margin: 0;
  padding: 0;
}

.main-wrapper {
  background-color: #fff;
  margin: auto;
  max-width: 280px;
}

label {
  white-space: nowrap;
}

.wrapper {
  padding-bottom: 50px;
  display: grid; */
  grid-gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(max-content, 30%));
  grid-auto-rows: minmax(min-content, max-content);
}
<div class="main-wrapper">
  <div class="wrapper">
    <label><input type="checkbox"/>Bafang M500</label>
    <label><input type="checkbox"/>Bafang MaxDrive</label>
    <label><input type="checkbox"/>Bosch Active Plus</label>
    <label><input type="checkbox"/>Bosch Performance</label>
    <label><input type="checkbox"/>Bosch Performance CX</label>
    <label><input type="checkbox"/>OLI SPORT</label>
    <label><input type="checkbox"/>Shimano StePS E6000</label>
    <label><input type="checkbox"/>Shimano STePS E6100</label>
    <label><input type="checkbox"/>Yamaha PW-ST</label>
  </div>

  <div class="wrapper">
    <label><input type="checkbox"/>500<</label>
      <label><input type="checkbox"/>500</label>
      <label><input type="checkbox"/>400</label>
      <label><input type="checkbox"/>300</label>
      <label><input type="checkbox"/>200</label>
      <label><input type="checkbox"/>100</label>
      <label><input type="checkbox"/><100</label>
        </div>
      </div>

Answer №1

the minmax function adjusts to various sizes.

  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));

Answer №2

In my opinion, the solution provided by @gregory-broyer is suitable for the upper wrapper, but may not be completely applicable to the lower one. To address this, I have created a second class specifically for the second wrapper, where I have defined the grid-template-columns property to match the original settings.

You can view the demo here:

body {
  background-color: #555;
  margin: 0;
  padding: 0;
}

.main-wrapper {
  background-color: #fff;
  margin: auto;
  max-width: 280px;
}

label {
  white-space: nowrap;
}

.wrapper {
  padding-bottom: 50px;
  display: grid; */
  grid-gap: 10px;
  //grid-template-columns: repeat(auto-fit, minmax(max-content, 30%));
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  grid-auto-rows: minmax(min-content, max-content);
}
.wrapper-h{
  grid-template-columns: repeat(auto-fit, minmax(max-content, 30%));
}
<div class="main-wrapper">
  <div class="wrapper">
    <label><input type="checkbox"/>Bafang M500</label>
    <label><input type="checkbox"/>Bafang MaxDrive</label>
    <label><input type="checkbox"/>Bosch Active Plus</label>
    <label><input type="checkbox"/>Bosch Performance</label>
    <label><input type="checkbox"/>Bosch Performance CX</label>
    <label><input type="checkbox"/>OLI SPORT</label>
    <label><input type="checkbox"/>Shimano StePS E6000</label>
    <label><input type="checkbox"/>Shimano STePS E6100</label>
    <label><input type="checkbox"/>Yamaha PW-ST</label>
  </div>

  <div class="wrapper wrapper-h">
    <label><input type="checkbox"/>500<</label>
      <label><input type="checkbox"/>500</label>
      <label><input type="checkbox"/>400</label>
      <label><input type="checkbox"/>300</label>
      <label><input type="checkbox"/>200</label>
      <label><input type="checkbox"/>100</label>
      <label><input type="checkbox"/>&lt100</label>
  </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

How to achieve smooth transitions in CSS3 with dynamic height changes?

Currently, I am in the process of designing a unique layout. The main challenge lies in toggling between two classes for a div element, one of which has a height of 0px. To achieve this, I have successfully utilized CSS3 animations that effectively scale t ...

Determine the overall price of the items in the shopping cart and automatically show it at the bottom without the need for manual entry

Creating a checkout form that displays the total cost of products, subtotal, GST cost, delivery cost, and overall price. The goal is to calculate the total by adding together the costs of all products with the "price" class (may need ids) at a 15% increase ...

Modifying the Background Color of a Header in React Native

As a newcomer to React Native, I am attempting to change the background color of the header bar (Navigation Bar). Here are some approaches that I have tried: return ( <View style={styles.container}> <NavigationBar titl ...

angular use ngFor directive to restrict the number of rows displayed in a table

In my angular 6 project, I am trying to limit the display to just five rows in a table using ngFor. My current code snippet is as follows: <tbody> <tr *ngFor="let item of routines; let i = index"> <td style="display: none"> ...

Press the button using the spacebar

I am facing an issue where I have a button with an anchor element that I need to trigger with the spacebar key for accessibility purposes. However, instead of triggering the button, pressing the spacebar causes the page to jump down when the button is in f ...

Tips for changing the color of an underline in an <a> tag when it is hovered over

Is it possible to change the color of the underline in an <a> tag when hovering over it? After some investigation, it seems that direct color changes are not feasible. Instead, you can use the border-bottom option. However, my attempt did not yield ...

`When utilizing $routeParams, the CSS fails to load`

Whenever I use parameters in ngRoute and go directly to the URL (without clicking a link on the site), the CSS fails to load. All my routes are functioning properly except for /chef/:id. I utilized yeoman's angular generator, and I am running everythi ...

Creating responsive images using Bootstrap CSS framework

I have been focusing on optimizing the banner below the navigation menu on this website. My goal is to make it responsive so that the player in the banner remains visible even when scaling down to iPhone size portrait. You can find the code for this secti ...

Bootstrap4 lacks responsiveness

html, body { background-color: #E3E3E3; width: 100%; height: 100%; margin: 0; padding: 0; } /* Custom Styling */ .section1 { background: url("../images/laptop-table1920-gray.jpg") no-repeat center center fixed; -webkit-background-size: cover ...

Leverage AJAX variables directly within the HTML page

In my application, I have incorporated an Input type="date" field to capture date values from users. However, I need to send the value of the date as "sdate" in the URL used in the action method, as illustrated below. My goal is to post the value of the In ...

view multiple HTML documents simultaneously in a single browser tab

Currently, I am in the process of building a wiki and have included tables in various sections. I want to showcase these tables on the main page as well. Rather than constantly copying and pasting them, I'm looking for a way to have the main page auto ...

PHP - The form page freezes up every time I try to submit the form

Hey there! I've encountered an issue with my form submission. Everything works perfectly fine, except when the Title field is left blank. Here's a snippet of my HTML code: Title: <input type="text" name="title"><br /> Description:&l ...

What is the best way to utilize Django forms with a <select> value that is frequently used?

I am currently developing an application that enables users to interact with a database of information, including searching and saving entries. Within the search feature of the application, there are multiple fields where users should specify comparison o ...

Learn how to align elements in the Toolbar component using the package "@mui/material" version "^5.11.3"

Recently, I came across this example on their website: https://codesandbox.io/s/0f50jf?file=/demo.js I've been attempting to align all toolbar elements in the center using: sx= {{ justifyContent: 'center', alignItems: 'center&apo ...

Using a pseudo-element after to center CSS content

Although there are numerous posts discussing this topic, my situation is unique. In my case, I have a collection of colors represented by circles. When I click on a circle, I add content in the form of a check mark by including a class selector with an af ...

Slowly revealing sticky navigation with slideDown animation using JQuery

Here is the code for a .JS file: $(document).scroll(function (){ var menuheight= $('header').height(); var y = $(this).scrollTop(); if (y>(menuheight)) { $('.menu_nav_features').addClass ...

What is the best way to align text in the middle of a card using bootstrap?

I crafted a code to display cards using Bootstrap 4. My goal now is to center all the text inside the card, but I'm facing challenges in achieving this. I attempted to apply "text-center" to the entire section. I also tried using "d-flex" and justify ...

It's a mystery unraveling the source of CSS margins and padding

Could someone please help me analyze this webpage? I am trying to make the center of the page white up to the navigation bar, at the bottom of the page, and horizontally up to the shadows on both sides. I'm not sure where this extra padding is coming ...

Step by step guide to showcasing images dynamically in user interface

My current project involves displaying a screen with an HTML table and an image. The HTML table is fully dynamic. The Code Working Process When the user loads a page (with a URL), I render an HTML table in different parts as the page loads. I retrieve al ...

Safari's Web Audio API suffering from subpar performance and various shortcomings

For my University project, I am developing an HTML and JavaScript-based mp3 player using the Web Audio API. You can check out the progress of this project by visiting this link: While everything is running smoothly on Firefox and Chrome, Safari is posing ...