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

              
                <main class="demo">
	<h1>🧱 Masonry Lite</h1>

	<button id="randomize">Randomize Block Sizes</button>
	<button id="add">+ Add Block</button>
	<button id="remove">- Remove Block</button>
	<br><br>

	<div id="example" class="grid">
		<div style="height: 100px; background: #e92525;"></div>
		<div style="height: 200px; background: #ffae6b;"></div>
		<div style="height: 300px; background: #1dbb1d;"></div>
		<div style="height: 300px; background: #2e9c9c;"></div>
		<div style="height: 100px; background: #ffd9ba;"></div>
		<div style="height: 200px; background: #ff6b6b;"></div>
		<div style="height: 200px; background: #77d5d5;"></div>
		<div style="height: 300px; background: #ff4c4c;"></div>
		<div style="height: 100px; background: #83ea83;"></div>
	</div>

	<h2>⚠️ Limitations</h2>
	<ul>
		<li>IE 11+. Requires <code>MutationObserver</code>. <a href="https://www.caniuse.com/#feat=mutationobserver" target="_blank" rel="noopener">Can I use…</a></li>
		<li>Doesn’t auto-detect number of columns (requires <code>data-masonry</code> attribute or <code>columns</code> parameter).</li>
		<li>Doesn’t auto-fill empty spaces (e.g., if one column is shorter than the others).</li>
		<li>Doesn’t support elements spanning multiple columns.</li>
		<li>Doesn’t support transitions.</li>
	</ul>

	<h2>📚 References</h2>
	<ul>
		<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver"target="_blank" rel="noopener">MDN: MutationObserver</a></li>
	</ul>
</main>

              
            
!

CSS

              
                html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	display: block; /* Fix IE rendering main as inline */
	margin: 0 auto;
	max-width: 42rem;
	padding-left: 1rem;
	padding-right: 1rem;
}

pre {
	border: 1px solid #ccc;
	padding: 1rem;
	overflow: auto;
	-webkit-overflow-scroll: touch;
}

/* Grid */
.grid {
	display: flex;
	flex-wrap: wrap;
	align-items: flex-start;
}

.grid > div {
	/* Equal 3rds */
	flex: 0 1 33.333%;
	/* Demo Styles */
	padding: 1rem;
	overflow: hidden;
	touch-action: none;
	user-select: none;
}

/* Demo Styles */
.grid > div:hover {
	background: black;
	outline: 3px dashed white;
	outline-offset: -3px;
}

.grid > div[style*="yellow"]:hover {
	outline-color: black;
}

              
            
!

JS

              
                /**
 * 🧱 Masonry Lite.
 */
class Masonry {
	/**
	 * Create a new Masonry grid.
	 * @param {Element} el
	 * @param {Number}  columns
	 */
  constructor(el, columns) {
		// Settings
    this.grid = el;
		this.columns = parseInt(el.getAttribute('data-masonry'), 10) || columns;
		
		// Observer
		const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
		this.observer = new MutationObserver(this.resize.bind(this));
		this.observerConfig = {
			attributes: true,
			childList: true,
			characterData: true,
			subtree: true,
		};
		
		// Init on page load
		this.resize();

		// And again every time the grid elements change
		this.observer.observe(this.grid, this.observerConfig);
  }

	/**
	 * Resize grid elements.
	 * @return {void}
	 */
	resize() {
		// Stop observing to avoid infinite loops
		this.observer.disconnect();

		const blocks = Array.from(this.grid.children);

		blocks.forEach((block, i) => {
			// Reset
			block.style.marginTop = '';

			// Offset
			const blockAbove = blocks[i - this.columns];

			if (blockAbove) {
				block.style.marginTop = `-${block.offsetTop - blockAbove.offsetTop - blockAbove.offsetHeight}px`;
			}
		});

		// Start watching again
		this.observer.observe(this.grid, this.observerConfig);
	}
}

// DEMO: Init Masonry for all grid elements on page load
// document.querySelectorAll('[data-masonry]').forEach(el => new Masonry(el));



// ------------------------------
// DEMO: Helpers
// ------------------------------

const grid = document.querySelector('#example');
new Masonry(grid, 3);

/**
 * Get a random integer between a minimum and maximum value.
 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values
 * @param  {Number} min
 * @param  {Number} max
 * @return {Number}
 */
function random(min, max) {
	min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}



// ------------------------------
// DEMO: Resizable divs
// @see http://interactjs.io/#resizing
// ------------------------------

interact('.grid > div').resizable({
	// resize vertical edges
	edges: { bottom: true, top: true },
	// keep the edges inside the parent
	restrictEdges: { outer: 'parent', endOnly: true },
	// minimum size
	restrictSize: { min: { height: 100 } },
	// smooth
	inertia: true,
}).on('resizemove', (event) => {
	const target = event.target;
	target.style.width  = event.rect.width + 'px';
	target.style.height = event.rect.height + 'px';
});



// ------------------------------
// DEMO: Randomize Block Sizes
// ------------------------------

const randomizeButton = document.querySelector('#randomize');

randomizeButton.addEventListener('click', (event) => {
	event.preventDefault();
	const blocks = Array.from(grid.children);
	blocks.forEach((block) => block.style.height = `${random(100, 300)}px`);
});



// ------------------------------
// DEMO: Add/Remove Blocks
// ------------------------------

// Colors from http://paletton.com/#uid=7000u0kiCFn8GVde7NVmtwSqXtg
const colors = ['#ff6b6b', '#ffbaba', '#ff8f8f', '#ff4c4c', '#e92525', '#ffae6b', '#ffd9ba', '#ffc18f', '#ff9d4c', '#e97e25', '#4ebaba', '#aceded', '#77d5d5', '#2e9c9c', '#168c8c', '#5cdd5c', '#b3f6b3', '#83ea83', '#3dcd3d', '#1dbb1d'];
let lastColor = '#83ea83';

function color() {
	let result;
	
	// Get a new color
	do {
		result = colors[random(0, colors.length)];
	} while (result === lastColor);
	
	// Keep track of the last color
	lastColor = result;
	
	return result;
}

const addButton = document.querySelector('#add');
const removeButton = document.querySelector('#remove');

addButton.addEventListener('click', (event) => {
	event.preventDefault();
	grid.insertAdjacentHTML('beforeend', `<div style="
		height: ${random(100, 300)}px;
		background: ${color()};
	"></div>`);
});

removeButton.addEventListener('click', (event) => {
	event.preventDefault();
	grid.removeChild(grid.firstElementChild);
});

              
            
!
999px

Console