<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Evan Minto</title>
    <description>The latest articles on Forem by Evan Minto (@evanminto).</description>
    <link>https://forem.com/evanminto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F194053%2F7084d9f4-a36c-4f3b-9f45-9c85ca572c4f.jpeg</url>
      <title>Forem: Evan Minto</title>
      <link>https://forem.com/evanminto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/evanminto"/>
    <language>en</language>
    <item>
      <title>Intrinsically Responsive CSS Grid with minmax() and min()</title>
      <dc:creator>Evan Minto</dc:creator>
      <pubDate>Fri, 12 Jul 2019 18:08:21 +0000</pubDate>
      <link>https://forem.com/evanminto/intrinsically-responsive-css-grid-with-minmax-and-min-1n55</link>
      <guid>https://forem.com/evanminto/intrinsically-responsive-css-grid-with-minmax-and-min-1n55</guid>
      <description>&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout" rel="noopener noreferrer"&gt;CSS Grid&lt;/a&gt; is now widely supported across modern browsers, and there are lots of folks doing great work with it! But unfortunately, one of the most useful features of the specification doesn’t quite work as advertised. Specifically, it’s not possible to create an “intrinsically responsive grid” — that is, &lt;strong&gt;a grid that is responsive based on the size of its container&lt;/strong&gt; — without the use of media queries. But thanks to some standards that are now available in some browsers and on their way to others, we can fix that!&lt;/p&gt;

&lt;p&gt;Before I explain the solution, let’s review the problem. With CSS Grid, we can create a grid of items arranged in equally sized columns and tell those column sizes to adjust based on the amount of available space:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vamptvo/embed/dBawwg?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This technique uses a number of features in the Grid specification:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;repeat()&lt;/code&gt; tells Grid to create multiple tracks with the same size parameters (not necessarily the same size, though in this case they all end up being the same).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;auto-fill&lt;/code&gt; tells it to create as many tracks as necessary to fill the space.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;minmax(10rem, 1fr)&lt;/code&gt; tells it to determine each track’s size by finding a value between a minium of &lt;code&gt;10rem&lt;/code&gt; and a maximum of &lt;code&gt;1fr&lt;/code&gt;. The &lt;code&gt;fr&lt;/code&gt; unit is what makes sure that the tracks are allowed to grow to fill any remaining space.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks to all this, the container adds new columns as it grows. A key feature of this solution is that the columns change based on the &lt;em&gt;container&lt;/em&gt; size, not the viewport size. That means we can apply this to a reusable component and be confident that it will always look right, without the need for media queries overriding the default behavior on specific pages. So what’s the problem?&lt;/p&gt;

&lt;h2&gt;
  
  
  Responsive... Sorta
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe1zbgd3t1hgse2m4cljk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe1zbgd3t1hgse2m4cljk.jpg" alt="Single column of black rectangles. They are overflowing from the right side of the light gray background." width="364" height="794"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since each grid track has a &lt;em&gt;minimum&lt;/em&gt; size of &lt;code&gt;10rem&lt;/code&gt;, they can’t shrink below that size. That means when the container is smaller than &lt;code&gt;10rem&lt;/code&gt;, the grid items overflow the container! To fix this, we’d have to wrap the &lt;code&gt;grid-template-columns&lt;/code&gt; delcaration in a media query, like so:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vamptvo/embed/qzvLar?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;But now that we’re in media query-land, our grid only switches when the &lt;em&gt;viewport&lt;/em&gt; is small. We can’t put it inside a small &lt;em&gt;container&lt;/em&gt;, or we run into the overflow problem all over again. This is why &lt;a href="https://twitter.com/heydonworks" rel="noopener noreferrer"&gt;Heydon Pickering&lt;/a&gt; and &lt;a href="https://twitter.com/andybelldesign" rel="noopener noreferrer"&gt;Andy Bell&lt;/a&gt;’s &lt;a href="https://every-layout.dev" rel="noopener noreferrer"&gt;Every Layout&lt;/a&gt; site generally avoids media queries and CSS Grid, instead achieving intrinsic responsiveness through Flexbox.&lt;/p&gt;

&lt;p&gt;Thankfully, with some new additions to the CSS spec that have started landing in browsers, we can fix this issue, allowing CSS Grid to live up to its full potential as a tool for &lt;a href="http://www.zeldman.com/2018/05/02/transcript-intrinsic-web-design-with-jen-simmons-the-big-web-show/" rel="noopener noreferrer"&gt;intrinsic web design&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Intrinsically Responsive Grid
&lt;/h2&gt;

&lt;p&gt;The previous example ran into problems because we were setting a fixed value as the minimum track size. When the container size shrunk, the items didn’t shrink with it, and thus: overflow. But what if we could set a minimum size that would shrink based on the container size, ensuring that our tracks were never too big for their container?&lt;/p&gt;

&lt;p&gt;As luck would have it, we can do just that, with the help of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/min" rel="noopener noreferrer"&gt;&lt;code&gt;min()&lt;/code&gt; function&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto-fill&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;min()&lt;/code&gt; accepts one or more values and returns the smallest value. The magic of the function is that, just like &lt;code&gt;calc()&lt;/code&gt;, the arguments can use different units, which allows us to return values that change dynamically based on context. In this case, we’re returning the current width of the container, capped at a maximum value of &lt;code&gt;10rem&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftql1bq8dgo6gl6uoskt6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftql1bq8dgo6gl6uoskt6.jpg" alt="Single column of black rectangles. They are all flush with the edge of the light gray background." width="328" height="815"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;min()&lt;/code&gt; is one of three new comparison functions introduced as part of the &lt;a href="https://drafts.csswg.org/css-values-4/#comp-func" rel="noopener noreferrer"&gt;CSS Values and Units Module Level 4&lt;/a&gt;. There’s also &lt;code&gt;max()&lt;/code&gt;, which naturally does the inverse of &lt;code&gt;min()&lt;/code&gt;. Finally &lt;code&gt;clamp()&lt;/code&gt; is a convenience function that applies both a minimum &lt;em&gt;and&lt;/em&gt; a maximum to a single value.&lt;/p&gt;

&lt;p&gt;In this example, there are two cases we have to worry about, and &lt;code&gt;min()&lt;/code&gt; solves for both of them. When the container is &lt;em&gt;smaller&lt;/em&gt; than &lt;code&gt;10rem&lt;/code&gt;, the minimum value in our &lt;code&gt;minmax()&lt;/code&gt; expression is set to the full width of the container, giving us a nice single-column view with no overflow. When the container is &lt;em&gt;larger&lt;/em&gt; than &lt;code&gt;10rem&lt;/code&gt;, the minimum value is set to &lt;code&gt;10rem&lt;/code&gt;, so we get a responsive grid with equal tracks of at least &lt;code&gt;10rem&lt;/code&gt; each.&lt;/p&gt;

&lt;h2&gt;
  
  
  In the Real World
&lt;/h2&gt;

&lt;p&gt;I know what you’re thinking. “This sounds cool and all, but I can’t actually &lt;em&gt;use&lt;/em&gt; it yet.” You’re only half right.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;min()&lt;/code&gt; isn’t very well supported, but it &lt;em&gt;is&lt;/em&gt; available in at least one browser: Safari quietly implemented both &lt;code&gt;min()&lt;/code&gt; and &lt;code&gt;max()&lt;/code&gt; a few versions back! Unfortunately there’s no &lt;code&gt;clamp()&lt;/code&gt; yet, but you can simulate it using the other two. Plus, Tab Atkins recently announced that &lt;a href="https://twitter.com/tabatkins/status/1141830941628780544" rel="noopener noreferrer"&gt;Chrome support is on its way&lt;/a&gt;. If you want to keep track of browser support, &lt;a href="https://caniuse.com/#feat=css-math-functions" rel="noopener noreferrer"&gt;CanIUse has a page for all three functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With that good news out of the way, here’s a working version of the code above (open in Safari to see it in action):&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vamptvo/embed/VJggRL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;One browser isn’t great, but it doesn’t mean you have to give up on this solution. For now, you can layer it onto your existing code as a progressive enhancement. Here’s my preferred solution:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vamptvo/embed/bPzzZr?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This one uses a &lt;code&gt;calc()&lt;/code&gt; expression as a fallback. The key part is the &lt;code&gt;calc(10% + 7.5rem)&lt;/code&gt;, which combines a fixed minimum with a percentage width. By computing the minimum based on &lt;em&gt;both&lt;/em&gt; of these, we lower the threshold for triggering overflow. Now it will trigger when the container is slightly larger than &lt;code&gt;7.5rem&lt;/code&gt;, whereas in our earlier solution it would trigger at &lt;code&gt;10rem&lt;/code&gt;. This solution is a bit finnicky though, so you’ll want to adjust both the percentage and fixed values to fit your grid’s design.&lt;/p&gt;

&lt;p&gt;With this as our fallback, we can take advantage of CSS’s ability to override duplicate property declarations by re-declaring &lt;code&gt;grid-template-columns&lt;/code&gt;. Browsers that support &lt;code&gt;min()&lt;/code&gt;, namely Safari, will get the fully responsive solution, while Chrome, Firefox, Edge, and others will get the fallback. As more browsers add support for &lt;code&gt;min()&lt;/code&gt;, they will automatically switch over to the new layout.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;min()&lt;/code&gt;, &lt;code&gt;max()&lt;/code&gt;, and &lt;code&gt;clamp()&lt;/code&gt; functions are really powerful tools, allowing us to toggle between values in real time based on changing conditions. Being able to test them out in a real browser opens up lots of opportunities for exploration, and I’m confident that this little fix for CSS Grid is just scratching the surface of the many ways that developers will use these features going forward.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;This post was originally published on &lt;a href="http://evanminto.com/blog/intrinsically-responsive-css-grid-minmax-min/" rel="noopener noreferrer"&gt;Evan Minto’s personal blog&lt;/a&gt;. Which, it turns out, is a great place to go if you’d like to hire Evan for a web development, design, or accessibility project!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>design</category>
      <category>cssgrid</category>
      <category>intrinsicdesign</category>
    </item>
  </channel>
</rss>
