cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

              <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);


Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.


Loading ..................