惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
A Deep Dive Into CSS Grid minmax()
Ahmad Shadeed · 2020-11-15 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

There are a lot of tutorials and guides out there that teach CSS grid in general, and I wrote about it multiple times. However, I noticed that there is a misunderstanding of the minmax() function as most of the articles are generic or don’t provide enough explanation and real-world use-cases. The minmax() is very powerful and useful. For that specific reason, I thought that writing a complete guide about minmax() will help to close that gap.

In this article, we will deep dive into the minmax(), learn how, when, and why to use it. At the end of the article, I expect that you will have a complete understanding of it. Let’s dive in!

Introduction to CSS Grid minmax()

As per the CSS spec, the definition of minmax(min, max) is as the following:

Defines a size range greater than or equal to min and less than or equal to max.

We can use minmax() as a value for a grid column or row. Let’s take a basic example:

.o-grid {
  display: grid;
  grid-template-columns: minmax(200px, 500px) 1fr 1fr;
  grid-gap: 1rem;
}

Let’s analyze the above:

  1. We have three grid columns.
  2. The first column width is minmax(200px, 500px). The minimum value is 200px, and the maximum value is 500px.
  3. The other two columns are 1fr each. That means, they will take the available remaining space.

Moving on with the example, we should think about different viewport sizes. What happens when the viewport size is too small? The short answer is horizontal scrollbar. I think no one likes to see a scrollbar on a web page, until it was done on purpose, of course.

That means, minmax() can’t handle responsive design by itself. We need to handle that by ourselves, and I will come to this later in the use-cases section.

CSS Grid minmax() Validation

If the min value in minmax(min, max) is larger than the max, then the max will be ignored. The minmax(min, max) will be treated as a min value.

Also, it’s important to keep in mind that using 1fr for the min value will be invalid. It only works for the max value. This is worse than the min being larger than the max! The whole declaration will be ignored.

Using a zero for the minimum value in minmax()

What will happen if we use minmax(0, 500px)? You guises it! The width will be at least zero, and it won’t go above 500px. That means, if the viewport width is not enough, the column width will resize.

A Simple Grid

Let’s suppose that we want to create a 3-columns grid. We will do the following:

.o-grid {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(200px, 1fr) minmax(200px, 1fr);
  grid-gap: 1rem;
}

A column width will have a minimum width of 200px. Here is how the items will look:

Note that we can use the repeat() function to avoid repeating the minmax() three times.

.o-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(200px, 1fr));
  grid-gap: 1rem;
}

Even though the above works, but it’s not recommended. Let’s explore why.

Changing the number of columns manually

If the number of columns needed to be different, then we need to manually change the number of columns. For me, that doesn’t make sense. CSS Grid is capable of more of that!

It will cause a horizontal scrollbar

Since we are explicitly defining the number of columns, the browser can’t make them wrap when the viewport width is small. As a result, this will cause a horizontal scrollbar.

How to fix the horizontal scrollbar? Well, we need to let the browser know that the number of columns should reduce if the viewport width isn’t enough.

In flexbox, we do that by adding flex-wrap: wrap to the flexbox parent.

.parent {
  display: flex;
  flex-wrap: wrap;
}

In CSS grid, we can use either the auto-fill or auto-fit keywords.

Using auto-fit or auto-fill keywords

To avoid tweaking the issues mentioned above, we can get the benefit of the auto-fit or auto-fill keywords. The difference between auto-fit and auto-fill is tricky.

In short, auto-fit will expand the grid items to fill the available space. While auto-fill won’t expand the items. Instead, auto-fill will keep the available space reserved without altering the grid items width.

When using auto-fill/fit, you might get the same result depending on the viewport size. However, you won’t notice that similarity until you resize the viewport. See the following video:

How collapse works for auto-fit

In the video below, the dotted rectangle represents the additional space. When the viewport is bigger, the browser collapse the rectangle and distribute its width between the other grid items.

For auto-fill, the browser deals with that differently. When the viewport size is bigger, the grid items width won’t expand, and an empty grid item will be there (The dotted one).

Now that I explained how minmax() works, let’s explore some examples and use-cases.

Use Cases and Examples

Cards Grid

I think this is the most common use-case for the minmax(), which is creating a responsive cards wrapper.

.wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-gap: 1rem;
}

When I started learning CSS grid, reading the value of grid-template-columns in this use-case was scary. It’s important to pay attention to viewports below 250px, as there will be a horizontal scrollbar.

To solve this, we have two solutions. The first one is using CSS media queries. The idea is to set the grid-template-columns to 1fr, and when the viewport width is big enough, we will apply the minmax().

.wrapper {
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 1rem;
}

@media (min-width: 300px) {
  .wrapper {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}

The second solution is to use CSS comparison functions. This solution is modern, and you need to check for browser support before using it.

.wrapper {
  display: grid;
  grid-template-columns: repeat(
    auto-fill,
    minmax(min(100%, 250px), 1fr)
  );
  grid-gap: 1rem;
}

I used the min() comparison function as the first value for minmax(). Here is what’s happening:

  • If the viewport width is less than 250px, then the first value of the minmax() function will be 100% of the parent width.
  • If the viewport is larger than 250px, then the first value of the minmax() will be 250px.

We achieved a better solution, with less CSS. If you want to learn more about CSS comparison functions, I wrote a detailed article about them.

Usage of ch unit for a content wrapper

An interesting use case for minmax() is using it to create an article layout. For this example, the content is horizontally centered.

.wrapper {
  display: grid;
  grid-template-columns: minmax(1rem, 1fr) minmax(auto, 70ch) minmax(1rem, 1fr);
  grid-gap: 1rem;
}

The first and last columns act as a gutter. The middle one is our focus. Notice that it has minmax(auto, 70ch). That means, the maximum width for this column is 70 characters per line. This is an ideal character length for better readability.

This can work great for mobile, as the first and last columns will collapse with a minimum width of 1rem. See the figure below:

The downside of using auto-fit irresponsibly

It might be tempting to use auto-fit. The downside is that when the content is dynamic and you don’t have control, things might break.

.wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  grid-gap: 1rem;
}

A grid of cards that looks perfect when we have four columns. However, when there is only one card only, it will expand to fill the available remaining space.

This is not good. The reason is that if the card component has an image with text, for example, it will expand the image to fill the full width, which makes the image look extremely bad. Here is a visual example that illustrates the issue.

Do you see the issue now? Please use auto-fit responsively. It might be okay to use it when you don’t have an image. However, if the component you’re working on has an image, I don’t recommend using it.

I wrote an ebook

I’m excited to let you know that I wrote an ebook about Debugging CSS.

If you’re interested, head over to debuggingcss.com for a free preview.