Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1132727 of minmax()

  • Revision slug: Web/CSS/minmax
  • Revision title: minmax()
  • Revision id: 1132727
  • Created:
  • Creator: Sebastianz
  • Is current revision? Yes
  • Comment Added 'Values' section and line highlighting within the example code and changed the code to avoid scrollbars within the live sample

Revision Content

{{CSSRef}}{{SeeCompatTable}}

The minmax() CSS function defines a size range greater than or equal to min and less than or equal to max. If max < min, then max is ignored and minmax(min,max) is treated as min. As a maximum, a {{cssxref("<flex>")}} value sets the flex factor of a grid track; it is invalid as a minimum.

Syntax

/* <inflexible-breadth>, <track-breadth> values */
minmax(200px, 1fr)
minmax(400px, 50%)
minmax(30%, 300px)
minmax(100px, max-content)
minmax(min-content, 400px)
minmax(max-content, auto)
minmax(auto, 300px)
minmax(min-content, auto)

/* <fixed-breadth>, <track-breadth> values */
minmax(200px, 1fr)
minmax(30%, 300px)
minmax(400px, 50%)
minmax(50%, min-content)
minmax(300px, max-content)
minmax(200px, auto)

/* <inflexible-breadth>, <fixed-breadth> values */
minmax(400px, 50%)
minmax(30%, 300px)
minmax(min-content, 200px)
minmax(max-content, 200px)
minmax(auto, 300px)

Values

<length>
A non-negative length.
<percentage>
A non-negative percentage relative to the inline size of the grid container in column grid tracks, and the block size of the grid container in row grid tracks. If the size of the grid container depends on the size of its tracks, then the <percentage> must be treated as auto. The UA may adjust the intrinsic size contributions of the track to the size of the grid container and increase the final size of the track by the minimum amount that would result in honoring the percentage.
<flex>
A non-negative dimension with the unit fr specifying the track’s flex factor. Each <flex>-sized track takes a share of the remaining space in proportion to its flex factor.
max-content
Represents the largest max-content contribution of the grid items occupying the grid track.
min-content
Represents the largest min-content contribution of the grid items occupying the grid track.
auto
As a maximum, identical to max-content. As a minimum it represents the largest minimum size (as specified by {{cssxref("min-width")}}/{{cssxref("min-height")}}) of the grid items occupying the grid track.

Formal syntax

{{csssyntax}}

Example

CSS content

#container {
  display: grid;
  grid-template-columns: minmax(max-content, 300px) minmax(200px, 1fr) 150px;
  grid-gap: 5px;
  box-sizing: border-box;
  height: 200px;
  width: 100%;
  background-color: #8cffa0;
  padding: 10px;
}

#container > div {
  background-color: #8ca0ff;
  padding: 5px;
}

HTML content

<div id="container">
  <div>
    Item as wide as the content,<br/>
    but at most 300 pixels.
  </div>
  <div>
    Item with flexible width but a minimum of 200 pixels.
  </div>
  <div>
    Inflexible item of 150 pixels width.
  </div>
</div>

Result

{{EmbedLiveSample("Example", "100%", 200)}}

Specifications

Specification Status Comment
{{SpecName("CSS Grid", "#valdef-grid-template-columns-minmax", "minmax()")}} {{Spec2("CSS Grid")}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}}[1] {{CompatNo}} {{CompatNo}}[2] {{CompatNo}}[3] {{CompatNo}}[1] {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}}[1] {{CompatNo}}[2] {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] Implemented behind the experimental Web Platform features flag in chrome://flags.

[2] Implemented behind the preference layout.css.grid.enabled, defaulting to false.

Revision Source

<div>{{CSSRef}}{{SeeCompatTable}}</div>

<p>The <code><strong>minmax</strong></code><strong><code>()</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> function defines a size range greater than or equal to <var>min</var> and less than or equal to <var>max</var>. If <var>max</var> &lt; <var>min</var>, then <var>max</var> is ignored and <code>minmax(min,max)</code> is treated as <var>min</var>. As a maximum, a {{cssxref("&lt;flex&gt;")}} value sets the flex factor of a grid track; it is invalid as a minimum.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="brush: css">
/* &lt;inflexible-breadth&gt;, &lt;track-breadth&gt; values */
minmax(200px, 1fr)
minmax(400px, 50%)
minmax(30%, 300px)
minmax(100px, max-content)
minmax(min-content, 400px)
minmax(max-content, auto)
minmax(auto, 300px)
minmax(min-content, auto)

/* &lt;fixed-breadth&gt;, &lt;track-breadth&gt; values */
minmax(200px, 1fr)
minmax(30%, 300px)
minmax(400px, 50%)
minmax(50%, min-content)
minmax(300px, max-content)
minmax(200px, auto)

/* &lt;inflexible-breadth&gt;, &lt;fixed-breadth&gt; values */
minmax(400px, 50%)
minmax(30%, 300px)
minmax(min-content, 200px)
minmax(max-content, 200px)
minmax(auto, 300px)
</pre>

<h3>Values</h3>

<dl>
 <dt>&lt;length&gt;</dt>
 <dd>A non-negative length.</dd>
 <dt>&lt;percentage&gt;</dt>
 <dd>A non-negative percentage relative to the inline size of the grid container in column grid tracks, and the block size of the grid container in row grid tracks. If the size of the grid container depends on the size of its tracks, then the <code>&lt;percentage&gt;</code> must be treated as <code>auto</code>. The UA may adjust the intrinsic size contributions of the track to the size of the grid container and increase the final size of the track by the minimum amount that would result in honoring the percentage.</dd>
 <dt>&lt;flex&gt;</dt>
 <dd>A non-negative dimension with the unit <code>fr</code> specifying the track’s flex factor. Each <code>&lt;flex&gt;</code>-sized track takes a share of the remaining space in proportion to its flex factor.</dd>
 <dt>max-content</dt>
 <dd>Represents the largest max-content contribution of the grid items occupying the grid track.</dd>
 <dt>min-content</dt>
 <dd>Represents the largest min-content contribution of the grid items occupying the grid track.</dd>
 <dt>auto</dt>
 <dd>As a maximum, identical to <code>max-content</code>. As a minimum it represents the largest minimum size (as specified by {{cssxref("min-width")}}/{{cssxref("min-height")}}) of the grid items occupying the grid track.</dd>
</dl>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">
{{csssyntax}}</pre>

<h2 id="Example">Example</h2>

<h3 id="CSS_content">CSS content</h3>

<pre class="brush: css; highlight[3]">
#container {
  display: grid;
  grid-template-columns: minmax(max-content, 300px) minmax(200px, 1fr) 150px;
  grid-gap: 5px;
  box-sizing: border-box;
  height: 200px;
  width: 100%;
  background-color: #8cffa0;
  padding: 10px;
}

#container &gt; div {
  background-color: #8ca0ff;
  padding: 5px;
}
</pre>

<h3 id="HTML_content">HTML content</h3>

<pre class="brush: html">
&lt;div id="container"&gt;
  &lt;div&gt;
    Item as wide as the content,&lt;br/&gt;
    but at most 300 pixels.
  &lt;/div&gt;
  &lt;div&gt;
    Item with flexible width but a minimum of 200 pixels.
  &lt;/div&gt;
  &lt;div&gt;
    Inflexible item of 150 pixels width.
  &lt;/div&gt;
&lt;/div&gt;</pre>

<h3 id="Result">Result</h3>

<p>{{EmbedLiveSample("Example", "100%", 200)}}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th>Specification</th>
   <th>Status</th>
   <th>Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS Grid", "#valdef-grid-template-columns-minmax", "minmax()")}}</td>
   <td>{{Spec2("CSS Grid")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}<sup>[2]</sup></td>
   <td>{{CompatNo}}<sup>[3]</sup></td>
   <td>{{CompatNo}}<sup>[1]</sup></td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}<sup>[1]</sup></td>
   <td>{{CompatNo}}<sup>[2]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Implemented behind the <em>experimental Web Platform features</em> flag in <code>chrome://flags</code>.</p>

<p>[2] Implemented behind the preference <code>layout.css.grid.enabled</code>, defaulting to <code>false</code>.</p>
Revert to this revision