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


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


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.


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.


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

<div class="grid">
  <div class="grid-item">1</div>
  <div class="grid-item grid-item--width2 grid-item--height2">2</div>
  <div class="grid-item grid-item--height3">3</div>
  <div class="grid-item grid-item--height2">4</div>
  <div class="grid-item grid-item--width3">5</div>
  <div class="grid-item">
    <p>6. А я внезапно такой самый хитрый блок с размерами по контенту, чтоб вы не подумали, что мы тут что-то нарочно долго и упорно подгоняли для красоты:)</p>
  <div class="grid-item">7</div>
  <div class="grid-item grid-item--height2">8</div>
  <div class="grid-item grid-item--width2 grid-item--height3">9</div>
  <div class="grid-item">10</div>
  <div class="grid-item grid-item--height2">11</div>
  <div class="grid-item">12</div>
  <div class="grid-item grid-item--width2 grid-item--height2">13</div>
  <div class="grid-item grid-item--width2">14</div>
  <div class="grid-item">15</div>
  <div class="grid-item grid-item--height2">16</div>
  <div class="grid-item">17</div>
  <div class="grid-item">18</div>
  <div class="grid-item grid-item--height3">19</div>
  <div class="grid-item grid-item--height2">20</div>
  <div class="grid-item">21</div>
  <div class="grid-item">22</div>
  <div class="grid-item grid-item--height2">23</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 = 'span '+rowSpan;
    // возвращаем элемент в грид

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