02-cssTermsLevel_06minmax() Function

minmax() Function

Level 6 — Layouts — CSS Grid A CSS function used within grid track definitions to set a size range (minimum and maximum bounds) for columns or rows, ensuring layouts scale responsively without breaking.


1. Prerequisites


2. Term Category

CSS Function (Universal Modern Standard .): minmax() Function is a fundamental concept in this technology stack. Level 6 — Layouts — CSS Grid


3. Explanation

(1) Design Motivation — "Why did we design this?"

When designing web layouts, columns must adapt to various screens.

If you set a column to a fixed width like 200px, it looks fine on desktop. But on a mobile phone screen that is only 320px wide, that column takes up almost the whole screen, leaving no room for content.

Conversely, if you make a column completely fluid using 1fr or percentages, it can shrink to become tiny on mobile (e.g., 40px wide). This squishes text and icons, making the site completely unreadable.

To solve this, the W3C created the minmax() function.

It defines a safe size range for columns and rows. You tell the browser: "This column must never shrink below 200px (so the content is always readable), but if there is extra room on the screen, let it grow to fill the remaining space (1fr)."


(2) Function Syntax

The function takes two parameters:

minmax([minimum-size], [maximum-size]);

  • minimum-size: The lower bound constraint. Can be a fixed unit (like 200px), a percentage, or content keywords (like min-content or auto).
  • maximum-size: The upper bound constraint. Can be a fixed unit, a percentage, or the flexible fr unit.

(3) The Dynamic Auto Grid

When combined with auto-fit and repeat(), minmax() creates the modern, media-query-free responsive grid:

.grid {
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

If the parent container is 800px wide, the browser fits 4 columns (each 200px wide). If the container shrinks to 500px, it drops to 2 columns (each stretching to 250px because of the 1fr maximum).


(4) Code Examples

Short Snippet

Setting a constraint on a sidebar:

.layout {
  display: grid;
  /* Sidebar is between 150px and 300px; Content fills the rest */
  grid-template-columns: minmax(150px, 300px) 1fr;
  gap: 20px;
}

Fuller Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Minmax Function Demo</title>
  <style>
    .grid-container {
      display: grid;
      /* Column 1: Min 150px, Max 1fr.
         Column 2: Min 300px, Max 2fr. */
      grid-template-columns: minmax(150px, 1fr) minmax(300px, 2fr);
      gap: 15px;
      background-color: #eee;
      padding: 10px;
    }

    .column {
      background-color: white;
      border: 2px solid black;
      padding: 20px;
      font-weight: bold;
      text-align: center;
    }
  </style>
</head>
<body>

  <!-- Try resizing your browser. 
       Column 1 will never shrink below 150px.
       Column 2 will never shrink below 300px. -->
  <div class="grid-container">
    <div class="column">Min: 150px / Max: 1fr</div>
    <div class="column">Min: 300px / Max: 2fr</div>
  </div>

</body>
</html>

4. Common Mistakes & Pitfalls

Mistake 1: Placing 1fr as the minimum size parameter

The mistake: Declaring minmax(1fr, 300px):

/* BAD: Invalid syntax! Browser will ignore this track setup */
.grid {
  grid-template-columns: minmax(1fr, 300px) 1fr; 
}

Why it's wrong: The fr unit represents flexible remaining space. A minimum size constraint must be a static or content-defined size. The browser cannot calculate a dynamic fraction as a minimum baseline.

Fix: Only use fr units as the maximum (second) parameter in minmax().


Mistake 2: Reversing Min and Max Arguments in minmax(max, min)

The mistake: Writing grid-template-columns: repeat(3, minmax(1fr, 200px));.

Why it's wrong: The 1st argument of minmax() is the MINIMUM bound, and the 2nd argument is the MAXIMUM bound. If min > max, the rule is invalidated.

Incorrect:

/* ❌ Min 1fr > Max 200px! Invalid minmax declaration! */
grid-template-columns: repeat(3, minmax(1fr, 200px));

Fix:

grid-template-columns: repeat(3, minmax(200px, 1fr)); /* Min: 200px, Max: 1fr */

Mistake 3: Using minmax() Without responsive auto-fit or auto-fill

The mistake: Writing grid-template-columns: minmax(300px, 1fr); without repeat().

Why it's wrong: Using minmax() on a single column track simply sets a minimum bound for that column. Combine repeat(auto-fit, minmax(...)) for responsive multi-column wrapping.

Incorrect:

/* Single column minmax without repeat wrapping */

Fix:

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

5. Practice Exercises

Exercise 1: Constraining Responsive Column Bounds with minmax

Scenario: An author constrains responsive grid columns to a minimum width of 16rem and maximum of 1fr using minmax().

Requirements:

  1. Apply grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)).
  2. Verify min/max boundary enforcement.
Answer

Implementation

.constrained-grid {
  display: grid;
  /* minmax(MIN, MAX): Min 16rem (~256px), Max 1fr (fractional share) */
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1.5rem;
}

Technical Explanation

  1. The minmax() Function: Defines a size range for grid tracks with a minimum bound (16rem) and maximum bound (1fr).
  2. Preventing Squishing: Guarantees columns never shrink below 16rem, ensuring content readability on mobile devices.
  3. Fluid Stretch: Allows columns to stretch up to 1fr when extra screen width is available.

Exercise 2: Dynamic Content Sizing with minmax(max-content, 1fr)

Scenario: Configures a sidebar column that scales dynamically between its content width and 1fr.

Requirements:

  1. Apply grid-template-columns: minmax(max-content, 20rem) 1fr.
Answer

Implementation

.sidebar-layout {
  display: grid;
  grid-template-columns: minmax(max-content, 20rem) 1fr;
  gap: 2rem;
}

Technical Explanation

  1. max-content Keyword: Sizes the track to fit the largest text content inside without line wrapping.
  2. Content-Driven Boundaries: minmax(max-content, 20rem) ensures the sidebar expands to fit content up to a max cap of 20rem.
  3. Flexible Layout Adaptation: Ideal for navigation sidebars with variable text length links.

Exercise 3: Preventing Text Ellipsis Truncation Failures inside Grid Track Boundaries

Scenario: Fixes broken text-overflow: ellipsis truncation inside grid cells using minmax(0, 1fr).

Requirements:

  1. Apply grid-template-columns: minmax(0, 1fr) to unblock truncation.
Answer

Implementation

.truncation-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* Forces grid cell to respect 0px min width */
}

.truncation-grid .title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Technical Explanation

  1. Grid Track Minimum Default Trap: Grid tracks default to minmax(auto, 1fr), which calculates minimum width based on content size, preventing text truncation!
  2. The minmax(0, 1fr) Fix: Replacing auto with 0 allows the grid track to shrink below content size, enabling text-overflow: ellipsis.
  3. Essential Data Grid Rule: Mandatory pattern when building table-like data grids with long text strings.

7. Key Takeaways

  • minmax() defines a bounding scale range (min-size and max-size) for grid tracks.
  • The browser guarantees the track never shrinks below the minimum and never exceeds the maximum.
  • Pair repeat(auto-fit, minmax(size, 1fr)) to create fluid, media-query-free responsive column wraps.
  • You can never use the fr unit as the first (minimum) parameter in minmax().
Built with LogoFlowershow