Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Holy AlbaGrids, Batman</h1>
<p>Heydon Pickering came up with a technique called the <a href="http://www.heydonworks.com/article/the-flexbox-holy-albatross">Flexbox Holy Albatross</a>. I explain how the technique works in <a href="https://snook.ca/archives/html_and_css/understanding-the-flexbox-albatross">my own article</a>.</p>

<p>A discussion popped up on Twitter today and I wondered if the same could be pulled off with Grids and the answer is... I think so. Here is my demo:</p>
<div class="grid a">
  <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
</div>

<p>If you look at the CSS pane, you'll notice that I've created a "breakpoint" at 1024px. Essentially, once the container that the div is in hits 1024px, it'll drop down to a single column of divs.</p>

<p>You'll notice that there's a lot of functions inside of functions inside of functions. Func-ception, if you will.</p>

<p>First, we do a calculation that will make a big number when the size of the container is smaller than 100% of our "breakpoint" and a small number when the size of the container is bigger than 100% of our "breakpoint".</p>

<pre>calc(1024px * 999 - 100% * 999)</pre>

<p>Fun fact about the minmax function: if the min value is bigger than the max value, then it becomes the max value. Since Heydon's technique has large values at small container sizes, we need to say that it should "max" out at the width of the container. If the container size is large, the size of each item will be small. Thus, we need a way to get the lower of two values: </p>

<pre>min( CALC, 100% )</pre>

<p>Excellent. If the calculation is really big, it'll max out each item at 100% width. If the calculation is really small, it'll use that value. But we don't want really tiny cells. We want them spread out to take up the full width.</p>

<p>For that, we'll use minmax.</p>

<pre>minmax( MINCALC, 1fr )</pre>

<p>We can collapse the individual items and use max-content. But it'll still break at the breakpoint we defined. In this case, 1024px.</p>

<div class="grid b">
  <div>This item has some content in it. </div>
   <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
   <div>Onea</div>
</div>

<p>We can collapse the individual items and use max-content. But it'll still break at the breakpoint we defined. In this case, 1024px. We can define it as ems or rems or ch, if we'd like, too.</p>

<h2>Browser Support</h2>
<p>If you managed to read all the way down here, you likely had one of two thoughts. If you are using Safari, you probably thought, "Oh, nice." If you are using Chrome or Firefox or Edge or anything else, you're probably thinking, "Has he gone mad? None of these examples work at all!" And aye, there's the rub. No min() or max() support outside of Safari, at the moment. Which, honestly, is a bit surprising given the support for minmax().</p>

              
            
!

CSS

              
                .grid {
  display: grid;
  margin-bottom: 20px;
}

.grid div {
  border: 1px solid #FF000066;
}

.a {
  grid-template-columns: repeat(auto-fit, minmax(min(calc(1024px * 999 - 100% * 999), 100%), 1fr));  
}

.b {
 grid-template-columns: repeat(auto-fit, minmax(min(calc(1024px * 999 - 100% * 999), 100%), max-content));  
}

/* Unreleated CSS below */

body {
  font-size: 110%;
  font-family: sans-serif;
}

h1, h2, p, pre {
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

pre {
  background-color: #DDD;
  padding: 20px; 
}
              
            
!

JS

              
                
              
            
!
999px

Console