What is the mechanism behind `minmax` generating multiple columns in CSS Grid when it is given a value of `1fr`?

Consider the code snippet provided. According to my understanding, the columns in this grid are set to be a minimum of 150 pixels wide and maximum of "1 fraction" of the container width.

My question is, how does the grid determine when to add more columns? Following this logic, wouldn't it always result in a single column that occupies 100% of the container width?

main {
    display: grid;
    grid-gap: 20px;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}

.item {
    background: #07c;
    height: 150px;
}
<main>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</main>

Answer №1

Is it possible that following this method will always result in a grid with one column occupying 100% of the container's width?

Not necessarily, as the grid is designed to prevent this scenario when using a maximum constraint defined in terms of fr. Instead, the grid will aim to generate multiple columns that meet the minimum requirement of 150 pixels within the available width of the container. For instance — disregarding any specified gutter width for a moment — if the container is 300 pixels wide, two columns of 150 pixels each will be established. As the width extends to 450 pixels, a third column will be introduced.

The use of 1fr ensures that any extra space (in cases where the container's width falls between 300 and 450 pixels excluding these values) is evenly distributed among the columns, post their arrangement.

This behavior aligns with section 7.2.2.2 of the specification, which, though detailed, illustrates an example closely resembling your situation:

Consider the code snippet below, generating columns of 25 characters fitting into the window width. Any remaining space will be divided among the 25-character columns.

body {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(25ch, 1fr));
}

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

What could be causing my linear background to behave in this unusual manner?

Just when I thought styling my background with a simple line of code would be easy, I encountered an unexpected issue. Instead of a smooth linear-gradient from the top left to the bottom right, my background is showing rows of red and blue in between each ...

In what location is it possible to define this CSS property?

After inheriting a website, I am encountering CSS issues that are driving me crazy. There is a mysterious div element with a height of 185px that is causing content truncation problems. The strange part is that this height is not defined anywhere in the st ...

Prevent images from loading and CSS from rendering on Firefox using Selenium WebDriver with Python

Currently, I am utilizing Selenium 2 with Python bindings to retrieve data from our partner's website. However, the operation is taking an average of 13 seconds. I have been exploring ways to disable images, CSS, and Flash content in order to improve ...

Issue with login form in IONIC: Form only functions after page is refreshed

Encountering an issue with my Ionic login form where the submit button gets disabled due to invalid form even when it's not, or sometimes displays a console error stating form is invalid along with null inputs. This problem seems to have surfaced afte ...

Neglecting the focus on click events in jQuery

I recently created a webpage with multiple elements that have the contenteditable property enabled. To enhance user experience, I implemented a feature where the text in each element is automatically selected when tabbing to it, thanks to a helpful plug-in ...

The parent element is concealed when the content inside the expanding span grows

I created an HTML and CSS design with the following styles: .sectionTitle { color: #fff; background-color: #e8eaed; padding: 0; height: auto; } .contentAlignment { margin-left: 10px; line-height: 23px; max-width: 100%; cursor: text; font ...

Python Script to Extract Hyperlinks from HTML Documents

I am in the process of developing a Python script to extract the iframe src from a given set of websites. For example, if my input includes A.com, B.com, and C.com, and each of these sites has iframes linking to D.com, E.com, F.com (or 'None' if ...

Issues with loading a 3D model on my hosting platform

After successfully testing my web page locally, I uploaded it to my server and encountered an issue with my 3D model not loading. What could be causing this problem? The code snippet responsible for loading the 3D model is as follows: textureLoader = new ...

Inline CSS not appearing correctly upon initial page load

Is your page rendering incorrectly on the first load but then correct after a refresh? It seems to be related to the "display:inline-block" style. Despite clearing cache before loading, the issue persists and you're unable to find a solution. First L ...

Unable to extract content enclosed within the span tags

I am struggling to extract the text from the constantly changing span tag (568,789,073,292). The HTML snippet is provided below: Despite trying the following xpath code, I still can't retrieve the text. xpath=(//div[@id='RxValidPackets']/sp ...

Show unique language text in the <noscript> element based on the user's browser language

I am just starting out with HTML, and I would like to show a message if JavaScript is disabled. To do this, I have placed the message within the <noscript> tag, which is working perfectly. <noscript> Need to enable Javascript. </noscript> ...

Is there a proper method for populating an HTML text with values from a form?

As a newcomer, I could really use some guidance here :) I'm trying to populate text with various words, numbers, and calculation results from a form. This is my initial attempt for just one field/word, and it appears to be functioning correctly. Do y ...

Struggling to place image behind Bootstrap navbar due to z-index issues

This layout features a navigation bar: <header id="navigation-bar" class="nav-header"> <nav class="navbar navbar-expand-lg"> <a class="navbar-brand" href="#"> <img src="images/logo.svg" alt="logo-icon"> ...

Do specific elements of typography adjust according to the size of the window?

I'm always amazed by the creative solutions that come out of this community. So I'm reaching out to see if anyone knows a way to achieve something unique with CSS! Specifically, I'm curious if it's possible to make certain horizontal p ...

Stop all other players except the one that is currently active. Utilize a jQuery audio plugin designed for the <audio> HTML element

I am currently using a plugin that styles the HTML5 <audio> element and provides flash fallback for browsers that do not support the .mp3 file format. However, I have run into an issue where if you start playing one song and then click play on anothe ...

What is the best way to eliminate blank space within a table cell?

The code snippet below includes a set max-width for table cells to ensure they have equal width regardless of their content. The vertical-align is set to top. However, the first cell appears longer than the second and third cells, leaving a significant gap ...

Unable to retrieve the WebDriver value in Java

Could you please locate the HTML code? <input type="password" maxlength="20" id="txtRegPassword" data-bind="value: Password, valueUpdate: 'afterkeydown',qtipValMessage:Password" class="input-txt ErrorControl" data-orig- title="" data-hasqt ...

Please rewrite the following sentence: "I am going to the store to buy some groceries."

As I navigate through my styled HTML page, an interesting sequence of events unfolds. When the Create New List button is clicked, a pink div emerges, contrasting with the orange hue of the All Lists div. At this moment, a new user has joined but hasn' ...

Rearrange the entire div container by simply dragging and dropping it. (Shift the Pop-up Modal dialog box)

How can I make a Modal pop-up draggable and change the color of the "Ok" and "Cancel" buttons on hover using a single CSS class? .hidModal{ position: fixed; font-family: Arial, Helvetica, sans-serif; top: 0; right: 0; bottom: 0; ...

Creating a clickable link that dynamically updates based on the span and id values, and opens in a new browser tab

I am attempting to create a clickable URL that opens in a new window. The URL is being displayed and updated on my HTML page in the following manner: Python Code: def data_cb(): return jsonify(waiting=await_take_pic()) Javascript Code: jQuery(d ...