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>Available Options Adding Masonry to CSS Grid</h1>
<h3>Try variations (works in Firefox Nightly only, <a href="#note">pref on</a>):</h3>
<input type="radio" name="display" value="masonry1" id="masonry1" checked>
<label for="masonry1">Simple Masonry</label><br>
<input type="radio" name="display" value="masonry2" id="masonry2">
<label for="masonry2">Fixed-width first & last columns</label><br>
<input type="radio" name="display" value="masonry3" id="masonry3">
<label for="masonry3">Fibonacci sequence columns</label><br> 
<input type="radio" name="display" value="masonry4" id="masonry4">
<label for="masonry4">Some items span 2 or 3 columns</label><br> 
<input type="radio" name="display" value="masonry5" id="masonry5">
<label for="masonry5">For comparison, dense packing in Grid (not Masonry)</label><br> 
<p>Be sure to <a href="#note">read the note</a> about this demo. Also, I made <a href="https://codepen.io/jensimmons/full/QWjqbJj" target="_blank">another demo</a> that provides a quick way to compare Masonry to Grid 1, Multicolumn, and Flexbox.</p>

<main>
  <figure><figcaption>1</figcaption><img src="https://images.unsplash.com/photo-1587383378486-83d683d9d02d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>2</figcaption><img src="https://images.unsplash.com/photo-1588395856595-235a6a1ccca8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>3</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/10558/photo-1587825027984-c4476461c8f9.jpg"></figure>
  <figure><figcaption>4</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/10558/photo-1588503391289-5f7b28258022.jpg"></figure>
  <figure><figcaption>5</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/10558/photo-1586603234056-2158d916b705.jpg"></figure>
  <figure><figcaption>6</figcaption><img src="https://images.unsplash.com/photo-1587462829651-7fd0208d766e?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>7</figcaption><img src="https://images.unsplash.com/photo-1585652505968-6266ecf26133?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>8</figcaption><img src="https://images.unsplash.com/photo-1586450463118-8d0cddab713f?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>9</figcaption><img src="https://images.unsplash.com/photo-1587993988198-a3d05e119eb0?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>10</figcaption><img src="https://images.unsplash.com/photo-1587750059642-d55dfc265acd?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>11</figcaption><img src="https://images.unsplash.com/photo-1586692283466-6e08f395f627?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>12</figcaption><img src="https://images.unsplash.com/photo-1586796105950-b885786d8597?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>13</figcaption><img src="https://images.unsplash.com/photo-1587169544748-d21bd810f57e?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>14</figcaption><img src="https://images.unsplash.com/photo-1586943101559-4cdcf86a6f87?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>15</figcaption><img src="https://images.unsplash.com/photo-1586767451503-204b55047cb2?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>16</figcaption><img src="https://images.unsplash.com/photo-1586754102101-36b67e4c5bcf?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>17</figcaption><img src="https://images.unsplash.com/photo-1585791022456-be89ddf09c75?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>18</figcaption><img src="https://images.unsplash.com/photo-1587063749684-d4926a4fc233?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>19</figcaption><img src="https://images.unsplash.com/photo-1586695940356-4b1c61eabbc7?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>20</figcaption><img src="https://images.unsplash.com/photo-1587151711096-23c51f92c920?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>21</figcaption><img src="https://images.unsplash.com/photo-1587068786187-db37681155f8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>22</figcaption><img src="https://images.unsplash.com/photo-1585841960320-f70aa9b7da69?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>23</figcaption><img src="https://images.unsplash.com/photo-1586968099622-45379e907f32?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>24</figcaption><img src="https://images.unsplash.com/photo-1588046883141-44883b3c0f15?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
  <figure><figcaption>25</figcaption><img src="https://images.unsplash.com/photo-1587061966530-a50125ba8512?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
</main>

<aside id="note">
  <h2>About this demo</h2>  
  <p>Try this demo in <a href="http://nightly.mozilla.org/">Firefox Nightly</a>. Type <code>about:config</code> in the URL bar. Then search for “masonry”. And set <code>layout.css.grid-template-masonry-value.enabled</code> to true.</p>
  <p>This is an experimental implementation — being discussed as a <em>possible</em> CSS specification. It is NOT yet official, and likely will change. Do not write blog posts saying this is definitely a thing. It's not a thing. Not yet. It's an experiment. A prototype. If you have thoughts, chime in at <a href="https://github.com/w3c/csswg-drafts/issues/4650">the CSSWG</a>.</p>
</aside>
              
            
!

CSS

              
                main {
  display: grid;
  grid-template-rows: masonry;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: 1rem;
  margin: 2rem;
}

/* --- Fixed-width first & last columns --- */
[value="masonry2"]:checked ~ main {
  grid-template-columns: 8rem repeat(auto-fill, minmax(16rem, 1fr)) 12rem;
}

/* --- Fibonacci Sequence columns --- */

@media(min-width:500px) {
  [value="masonry3"]:checked ~ main {
    grid-template-columns: 1fr 1fr;
  }
}
@media(min-width:700px) {
  [value="masonry3"]:checked ~ main {
    grid-template-columns: 1fr 1fr 2fr;
  }
}
@media(min-width:900px) {
  [value="masonry3"]:checked ~ main {
    grid-template-columns: 1fr 1fr 2fr 3fr;
  }
}
@media(min-width:1100px) {
  [value="masonry3"]:checked ~ main {
    grid-template-columns: 1fr 1fr 2fr 3fr 5fr;
  }
}
@media(min-width:1300px) {
  [value="masonry3"]:checked ~ main {
    grid-template-columns: 1fr 1fr 2fr 3fr 5fr 8fr;
  }
}

/* --- masonry4 --- */
[value="masonry4"]:checked ~ main {
  grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
}
[value="masonry4"]:checked ~ main figure:nth-child(5n) {
  grid-column: span 2;
}
[value="masonry4"]:checked ~ main figure:nth-child(7n) {
  grid-column: span 3;
}

/* --- masonry5 --- */
[value="masonry5"]:checked ~ main {
  grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
  grid-template-rows: auto;
  grid-auto-flow: dense;
}
[value="masonry5"]:checked ~ main figure:nth-child(5n) {
  grid-column: span 2;
  grid-row: span 2;
}
[value="masonry5"]:checked ~ main figure:nth-child(7n) {
  grid-column: span 3;
  grid-row: span 3;
}

/* --- The rest of the styling --- */
* {
  box-sizing: border-box;
}
body { 
  font-family: avenir, roboto, helvetica, sans-serif;
  margin: 2rem calc(2vw + 2rem);
}
main {
  margin: 2rem 0;
}
img { 
  width: 100%; 
  display: block;
}
figure {
  margin: 0;
  box-shadow: 0px 0px 3px #999;
  align-self: start;
}
figcaption {
  padding: 0.2rem 1rem 0.1rem;
  background: #333;
  color: #eee;
  font-size: 0.8rem;
  text-align: end;
}

h1 {
  margin: 0;  
}
h2 {
  margin: 0;  
}
h3 {
  margin: 0.5rem 0 0;
  font-size: 1rem;
}
aside {  
  margin: 6rem 0;
  max-width: 80ch;
  padding: 2rem 2rem 1rem;
  border: 1px solid #000;
}
p {
  margin: 1rem 0;
  color: #444;
  font-weight: 400;  
}
code {
  font-size: 1.07rem;
  color: #d45b2c;
}
input {
  margin-left: 1rem;
}
label {
  margin: 0 1.1rem 0 0.1rem;
}

              
            
!

JS

              
                
              
            
!
999px

Console