Using a grid layout with columns that dynamically adjust in size between a set maximum and minimum pixel size

How can I create grid columns that have a minimum width of 200px and a maximum width of 300px? Currently, when using the code

grid-template-columns: repeat(auto-fit, minmax(200px, 300px));
, the columns always render at 300px.

To make columns shrink proportionally, I tried using: 
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

However, this caused the columns to become too large. After reviewing the documentation for minmax(), I couldn't find a suitable example to address this issue.

Is there a workaround solution available for this problem? See The Fiddle for reference.

The Answer

An important aspect of resolving this issue is modifying the code to: minmax(300px, auto)); and adding box-sizing: border-box;.

Failure to include these adjustments may result in the grid not being properly centered when collapsing into one column layout. Thank you!

Answer №1

To prevent elements from growing larger than 300px, you can use either minmax(200px,1fr) or minmax(200px,auto) along with setting max-width on the child elements.

Here are some examples:

* {
  box-sizing: border-box
}

body {
  padding: 20px;
  font-family: Helvetica;
}
/* sizing */
.wrapper {
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.wrapper.bis {
  grid-template-columns: repeat(auto-fit, minmax(200px, auto));
}

.box {
  max-width: 300px
}
/* --- */
.wrapper {
  display: grid;
  grid-gap: 10px;
}

.box {
  background-color: #20262e;
  color: #fff;
  border-radius: 3px;
  padding: 20px;
  font-size: 14px;
  max-width: 300px
}

.box {
  /* Demo see average width by chunks of 50px bg-gradient-color */
  background-image: repeating-linear-gradient(to right, red 0 50px, green 50px 100px);
  background-position: 0 0;
  background-repeat: repeat-x;
  background-size: auto 5px;
}
<div class="wrapper">
  <div class="box a">A - 200px,1FR</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
  <div class="box e">E</div>
  <div class="box f">F</div>
</div>
<hr>
<div class="wrapper bis">
  <div class="box a">A - 200px,AUTO</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
  <div class="box e">E</div>
  <div class="box f">F</div>
</div>

Answer №2

After testing out your code and examining the elements in my browser, I am confident that everything is correct. Your utilization of padding within each box seems to be spot on.

Here is the result when using the maximum value: https://i.sstatic.net/9e7ka.png

And this is what it looks like with the minimum value: https://i.sstatic.net/jAwqt.png

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

Issue of Modal not hiding persists despite attempted solutions using JS, jQuery, and CSS

I am attempting to display a modal exclusively on mobile devices. Despite my confidence in the correctness of my JavaScript, it is not functioning as expected. Would anyone be willing to review and provide assistance? <div class="col-5 col-sm-2 ml-au ...

Tips for adding an icon to an image using PHP and CSS

My goal is to create a responsive image gallery where each image has an icon or text that allows me to delete it if necessary. Here is my HTML and PHP code: <?php $sqlimage = "SELECT * FROM images where `id_user` = '".$customer_id."'"; ...

Customizing the width of the JQuery $ui.progressbar by overriding its properties

After spending some time, I successfully overrode the function that controls the width of the progress bar element in the jQuery UI widget version 1.12.1 from September 14, 2016. Initially, I needed the progress bar to completely fill a column in a table. ...

I am encountering an issue with the jquery.min.js file not loading when I try to utilize the Google CDN for jQuery

Currently in the process of learning about jQuery and testing its functionality, however, I am facing an issue where changes are not being reflected. The error message states that it is unable to load. I have confirmed that I am using Google CDN and also h ...

Problem with JQUERY Galleria CSS positioning alignment specifically in Firefox, Chrome works without issues

I recently incorporated jquery Galleria into my website and I am currently facing an alignment issue with the div element gallery-container. Interestingly, it appears correctly aligned in Chrome but is shifted to the right in Firefox. You can view the webs ...

What is the best way to incorporate white-space:normal within the text of a jQuery Mobile grid button?

Below is the code for my jQuery Mobile Grid buttons: <div class="ui-grid-b my-breakpoint"> <div class="ui-block-a"><button type="button" data-theme="c"> <img src="res/icon/android/business.png" height="45"><br>Business</ ...

Is it possible for an ul to be displayed beneath a white section within an li

I am working on a JQuery carousel that is displaying correctly, but I want to make a small adjustment to the content: <li class="jcarousel-item jcarousel-item-horizontal jcarousel-item-1 jcarousel-item-1-horizontal" style="float: left; list-style: none ...

A guide to resizing images to fit the page in React using Bootstrap

Currently, I am utilizing a function to iterate over each item in the props array and generate an image tag. My goal is to have every set of 3 images enclosed within a row div using Bootstrap to ensure proper page layout. However, I am struggling to implem ...

Altering the source of an HTML image

I am faced with a situation where I need to change the font color of text under a specific condition using code like this: newEntryRow.find("p").find("span").css('color','red'); Now, my goal is to achieve something similar but by chan ...

Tips for placing the html <a> tag above an image and centered

Here's the HTML code I'm struggling with, which is inside an ASP.NET Repeater: <div class="team_member"> <a class="teamMemberLink" href='<%# "Profile.aspx?uID=" + Eval("ID") %>' target="_blank"><%# Eval("Name") ...

Navigating with just a single instance of identical pseudo elements

I am attempting to replicate a design similar to this: https://i.sstatic.net/pJPBj.png In this design, the ribbon in the middle is an image, while the gradients surrounding it are created using CSS. My objective is to have an element act as the title on ...

Ways to prevent the navbar from disappearing when the Chrome browser is resized

I am looking to align the navbar lists, which are currently on the right side, below the header icon placed on the left side. Rather than toggling the navbar with a button, I want it to simply expand under the header icon. It seems that the disappearance ...

Page will break once it is filled vertically

My webpage is filled with Bootstrap 4 cards lined up in a row. I am looking to add a line break when the vertical scroll bar appears. How can I achieve this? Snippet: <?php //THIS FILE BELONGS TO CROPSATH, LICENSED UNDER GPLv3. //CREATED BY MORICZGER ...

Is it considered a bad practice to apply overflow: auto to all elements with the exception of html?

My approach to beginning a new design always involves overriding the default padding and margin set by the browser on all elements: * { margin: 0; padding: 0; } After coming across j08691's response regarding a margin collapse issue, I discovered th ...

Experiencing problems with z-index and the usage of :before and :after pseudo elements on Internet Explorer

Encountering a compatibility issue on IE8 (surprise, surprise) and here's what it looks like: The current setup involves products as list items with image and details: <li class="product ships-free on-sale">stuff</li> With corresponding ...

Issue with cdk-virtual-scroll-viewport: Scroll function malfunctioning when using navigation up/down button

While utilizing cdk-virtual-scroll-viewport within my mat-autocomplete, I have noticed that scrolling works perfectly when using the mouse but does not function properly when pressing the keydown button. Expected Behavior: The list should automatically sc ...

Looking to achieve a scroll effect with mix-blend-mode using JavaScript?

I am seeking a method to adjust the background color of a specific element based on the background itself. While CSS offers the mix-blend-mode property, I am looking to specify the color manually. Ideally, I would like to achieve the same effect using an ...

Image overlay on a hovering background

I'm currently facing a challenge with creating a hover effect for a background image while keeping the image in front unchanged. I would like the background to darken on hover, but I want the image in front of it to remain the same. Essentially, I ne ...

HTML animation effect on subpage with a URL modification [LATEST UPDATE]

Can you smoothly load a new subpage in the background, update the URL, and fade it in while an animation is playing on the previous page? Check out this example (jsFiddle) to see a simple animation: $(document).ready(function() { 'use strict&apo ...

Issue with Font-Awesome icons failing to display properly when using the BootstrapCDN

Trying to integrate Font-Awesome icon fonts using the BootstrapCDN link with what seems like the latest version: <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> The link has been added to the <hea ...