Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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>Simple Masonry Flexbox & Vanilla JS</h1>
<h2>※横の並び順は維持されます。</h2>
<ul class="js-masonry-list test-list">
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/103/400/300" alt="" width="400" height="300">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/1003/300/400" alt="" width="300" height="400">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/90/300/200" alt="" width="300" height="200">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/100/300/150" alt="" width="300" height="150">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/61/300/280" alt="" width="300" height="280">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/23/300/400" alt="" width="300" height="400">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/13/400/300" alt="" width="400" height="300">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/43/300/380" alt="" width="300" height="380">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/54/300/280" alt="" width="300" height="280">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/76/300/150" alt="" width="300" height="150">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/56/400/300" alt="" width="400" height="300">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/783/300/300" alt="" width="300" height="300">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/72/300/200" alt="" width="300" height="200">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/161/300/150" alt="" width="300" height="150">
  </li>
  <li class="js-masonry-elm">
    <img src="https://picsum.photos/id/829/300/280" alt="" width="300" height="280">
  </li>
</ul>

<h2>テキストリスト</h2>
<ul class="js-masonry-list test-list-text">
  <li class="js-masonry-elm">
    <h3>リスト1</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト2</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
      <li><a href="">リンク4</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト3</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
      <li><a href="">リンク4</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト4</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
      <li><a href="">リンク4</a></li>
      <li><a href="">リンク5</a></li>
      <li><a href="">リンク6</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト5</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
      <li><a href="">リンク4</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト6</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
      <li><a href="">リンク4</a></li>
      <li><a href="">リンク5</a></li>
      <li><a href="">リンク6</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト7</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト8</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
      <li><a href="">リンク2</a></li>
      <li><a href="">リンク3</a></li>
      <li><a href="">リンク4</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト9</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
    </ul>
  </li>
  <li class="js-masonry-elm">
    <h3>リスト10</h3>
    <ul class="test-list-sub">
      <li><a href="">リンク1</a></li>
    </ul>
  </li>
</ul>
              
            
!

CSS

              
                /* Simple Masonry Flexbox & Vanilla JS ベース設定 */
.js-masonry-list {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
}
@media only screen and (min-width: 1025px) {
  .js-masonry-list {
    column-gap: 2.5%;
  }
  .js-masonry-elm {
    width: 18%;
    margin-bottom: 2.5%;
  }
}
@media only screen and (min-width: 601px) and (max-width: 1024px) {
  .js-masonry-list {
    column-gap: 1.25%;
  }
  .js-masonry-elm {
    width: 32.5%;
    margin-bottom: 1.25%;
  }
}
@media only screen and (min-width: 451px) and (max-width: 600px) {
  .js-masonry-list {
    column-gap: 2%;
  }
  .js-masonry-elm {
    width: 49%;
    margin-bottom: 2%;
  }
}
@media only screen and (max-width: 450px) {
  .js-masonry-elm {
    width: 100%;
    margin-bottom: 20px;
  }
}



/* テスト用設定 */
body {
  margin: 0;
  padding: 50px 0;
}
h1 {
  text-align: center;
  margin-bottom: 10vh;
}
h2 {
  margin: 0 0 40px;
  text-align: center;
}
h3 {
  margin: 0 0 10px;
  font-size: 16px;
}
.lead {
  margin: 0 0 40px;
  text-align: center;
  font-size: 18px;
}

img {
  height: auto;
  width: 100%;
  vertical-align: top;
}
.test-list,
.test-list-text {
  max-width: 1200px;
  margin: 0 auto 100px;
  padding: 0 50px;
  list-style: none;
}
.test-list-text .js-masonry-elm {
  padding: 15px;
  box-sizing: border-box;
}
.js-masonry-elm {
  background-color: #f7f7f7;
  font-size: 14px;
}
.test-list-sub {
  padding-left: 1em;
}
.test-list-sub li + li {
  margin-top: 10px;
}
@media only screen and (max-width: 600px) {
  .test-list {
    padding: 0 20px;
  }
}
              
            
!

JS

              
                masonry({
  target: '.js-masonry-list',  //対象リスト
  column: 5,  //カラム数
  responsive: [{  //レスポンシブ設定
    breakpoint: 1024, //ブレイクポイント max-width
    column: 3
  }, {
    breakpoint: 600,
    column: 2
  }, {
    breakpoint: 450,
    column: 1
  }]
});

//Simple Masonry Flexbox & Vanilla JS
function masonry(setOptions) {
  'use strict';

	//options
	const defaultOptions = {
		target: '.js-masonry-list',  //対象リスト
		column: 1,  //カラム数
		responsive: null,
		/*
			//レスポンシブ設定
			responsive: [{
				breakpoint: 1024, //ブレイクポイント max-width
				column: 3
			}, {
				breakpoint: 500,
				column: 1
			}]
		*/
		activeClass: 'is-active',  //Masonry active class
		listClass: '.js-masonry-list',  //対象リスト class
		listElmsClass: '.js-masonry-elm',  //対象リスト内要素 class
	}
	//設定をマージ
	const options = Object.assign({}, defaultOptions, setOptions);

	//class設定
	const listClass = options.listClass;
	const listElmsClass = options.listElmsClass;

	//対象リスト取得
	const lists = Array.prototype.slice.call(document.querySelectorAll(listClass),0);
	//表示エリアが存在するかチェック
	if(lists.length === 0) {
		return false;
	}

	//masonry関数呼び出し
	masonryFunk(options);
	window.addEventListener('resize',  function() {
		masonryFunk(options);
	});

	//masonry関数
	function masonryFunk(options) {
		//カラム数チェック
		let column = options.column;
		//レスポンシブ切り替え
		const responsive = options.responsive;
		if(responsive) {
			//ウィンドウの幅取得
			const winWidth = window.innerWidth;
			//ウィンドウの幅とブレイクポイントを比較
			for (let i = 0; i < responsive.length; i++) {
				if(winWidth <= responsive[i].breakpoint) {
					column = responsive[i].column;
				}
			}
		}

		//1カラムの場合は設定をリセットし処理を終了
		if(column === 1) {
			lists.forEach(function(list){
				const listElms = Array.prototype.slice.call(list.querySelectorAll(listElmsClass),0);
				listElms.forEach(function(listElm){
					//設定リセット
					listElm.style.marginTop = '';
				});
			});
			return false;
		}

		//対象リストごとの設定
		lists.forEach(function(list){
			//対象リスト内要素取得
			const listElms = Array.prototype.slice.call(list.querySelectorAll(listElmsClass),0);
			//対象リスト内要が存在するかチェック
			if(listElms.length === 0) {
				return false;
			}
			
			//初期設定
			if(!list.classList.contains(options.activeClass)) {
				//active class設定
				list.classList.add(options.activeClass);
			}

			//要素の位置
			listElms.forEach(function(listElm,index){
				//設定リセット
				listElm.style.marginTop = '';

				//最初の行は処理しない
				if(column > index) {
					return;
				}

				//直上の要素取得
				const topListElm = listElms[index-column];
				//直上の要素の位置取得
				const topListElmPosi =  topListElm.getBoundingClientRect().top;
				//直上の要素のmargin-bottomを含む高さ取得
				const topListHeight =  getHeightAndMarginBottom(topListElm);
				//直上の要素の下端位置取得
				const topListBottomPosi = topListElmPosi + topListHeight;

				//表示エリアの位置取得
				const listElmPosi =  listElm.getBoundingClientRect().top;
				//調整位置取得
				let setPosi = listElmPosi.toFixed(0) - topListBottomPosi.toFixed(0);
				//0の場合処理しない
				if(setPosi === 0) {
					return false;
				}
				//位置をズラす
				setPosi = '-' + setPosi + 'px';
				listElm.style.marginTop = setPosi;
			});
		});
	}

	//margin-bottomを含む高さ
	function getHeightAndMarginBottom(elm) {
		//高さ取得
		const height = elm.getBoundingClientRect().height;
		//margin取得
		const styles = getComputedStyle(elm);
		const bottom = parseFloat(styles.marginBottom);
		return height + bottom;
	}

}

              
            
!
999px

Console