Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <h1>Masonry (резиновые колонки) на гридах</h1>

<div class="grid">
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item grid-item--height3"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item grid-item--width3"></div>
  <div class="grid-item">
    <p>А я внезапно такой самый хитрый блок с размерами по контенту, чтоб вы не подумали, что мы тут что-то нарочно долго и упорно подгоняли для красоты:)</p>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item grid-item--width2 grid-item--height3"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--height3"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--height2"></div>

              * { box-sizing: border-box; }

body { font-family: sans-serif; }

/* ---- grid ---- */

.grid {
  background: #EEE;
  max-width: 1200px;
  display: grid;
  --grid-gutter: 5px;
  grid-gap: var(--grid-gutter);
  grid-template-columns: repeat(5, 1fr);
  --grid-row-height: 20px;
  grid-auto-rows: var(--grid-row-height);
  grid-auto-flow: row dense;
  position: relative;

/* ---- grid-item ---- */

.grid-item {
  min-height: 120px;
  background: #CFC;
  border: 2px solid #333;
  border-color: hsla(0, 0%, 0%, 0.5);
  border-radius: 5px;
  /* Значения ниже нужны только для измерения эл-та при перестройке грида */
  position: absolute;
  width: calc(20% - var(--grid-gutter));
  top: 0;
  left: 0;

/* после перестройки грида сбрасываем вспомогательные свойства, эти правила должны перекрывать любой класс */
.grid-item.grid-item-ready {
  position: static;
  height: auto;
  width: auto;

.grid-item--width2 {
  calc(40% - var(--grid-gutter));
  grid-column-end: span 2;
.grid-item--width3 {
  calc(60% - var(--grid-gutter));
  grid-column-end: span 3;

.grid-item--height2 { height: 200px; }
.grid-item--height3 { height: 260px; }
.grid-item--height4 { height: 360px; }

p {
  margin: .5em;
              const gridStyles = getComputedStyle(document.querySelector('.grid',null));
const rowHeight = parseInt(gridStyles.getPropertyValue('--grid-row-height'));
const gap = parseInt(gridStyles.getPropertyValue('--grid-gutter'));;

let makeGrid = function() {
  let items = document.querySelectorAll('.grid-item');
  for (let i=0, item; item = items[i]; i++) {
    // вынимаем элемент из грида и измеряем
    let height = item.offsetHeight;
    // рассчитываем, сколько рядов он займет
    let rowSpan = Math.ceil((height + gap)/(rowHeight + gap));
    // задаем соответствующий span в grid-row-end
    item.style.gridRowEnd = 'span '+rowSpan;
    // возвращаем элемент в грид

window.addEventListener('load', makeGrid);
window.addEventListener('resize', () => {
  makeGrid.resizeTimer = setTimeout(makeGrid, 50);

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.