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

              
                <div class="wrapper">
  <h1>Align the height of each row (Vanilla JS)</h1>
  <h2>横並び要素の列ごとに高さを揃える</h2>
  <ul class="test-list">
    <li>
      <p class="js-test-text">高さを揃える高さを揃える高さを揃える高さを揃える高さを揃える</p>
      <img src="https://picsum.photos/id/103/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える</p>
      <img src="https://picsum.photos/id/1003/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える高さを揃える高さを揃える</p>
      <img src="https://picsum.photos/id/90/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える高さを揃える高さを揃える</p>
      <img src="https://picsum.photos/id/100/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える高さを揃える高さを揃える高さを揃える高さを揃える高さを揃える高さを揃える高さを揃える</p>
      <img src="https://picsum.photos/id/61/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える</p>
      <img src="https://picsum.photos/id/23/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える</p>
      <img src="https://picsum.photos/id/13/400/300" alt="" width="400" height="300">
    </li>
    <li>
      <p class="js-test-text">高さを揃える</p>
      <img src="https://picsum.photos/id/43/400/300" alt="" width="400" height="300">
    </li>
  </ul>


  <h2>スライダー毎に中の要素の高さを揃える</h2>
  <div class="swiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃えるスライダー毎に高さを揃えるスライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/103/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ高さを揃える2つめ高さを揃える2つめ高さを揃える2つめ高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/1003/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/90/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
    </div>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>

  <div class="swiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃えるスライダー毎に高さを揃えるスライダー毎に高さを揃えるスライダー毎に高さを揃えるスライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/103/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ高さを揃える2つめ高さを揃える2つめ高さを揃える2つめ高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃えるスライダー毎に高さを揃えるスライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/10/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/1003/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
      <div class="swiper-slide">
        <p class="js-test-title01">スライダー毎に高さを揃える</p>
        <img src="https://picsum.photos/id/90/400/300" alt="" width="400" height="300">
        <p class="js-test-title02">高さを揃える2つめ</p>
        <p>テキストテキスト</p>
      </div>
    </div>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</div>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 50px 0;
}
.wrapper {
  max-width: 1000px;
  margin: 0 auto;
}
h1 {
  text-align: center;
  margin-bottom: 10vh;
}
h2 {
  margin: 0 0 40px;
  text-align: center;
}
.test-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-bottom: 100px;
  list-style: none;
}
.test-list li {
  width: 23%;
  margin-bottom: 10px;
}
img {
  width: 100%;
  height: auto;
  vertical-align: top;
}
.swiper {
  padding-bottom: 40px;
  margin-bottom: 100px;
}
.js-test-title01 {
  font-weight: bold;
}
              
            
!

JS

              
                //要素の高さ調整関数
const listHeight = (setOptions) => {
	const defaultOptions = {
		targets: null,  //対象リスト
		parent: null,  //検索対象にする親要素
		column: 1,  //カラム数
	};

	//設定をマージ
	const options = Object.assign({}, defaultOptions, setOptions);

	//ターゲットが存在しない場合は終了
	let targets = options.targets;
	if(targets === null) {
		return;
	}

	//親グループが指定されているかチェック
	const parent = options.parent;
	if(parent !== null) {
		targets = parent.querySelectorAll(targets);
	} else {
		targets = document.querySelectorAll(targets);
	}

  //要素の高さをリセット
	targets.forEach((target) => {
		target.style.height = '';
	});

  //設定された列の数が1以下だった場合は処理を停止
	const column = options.column;
  if(column <= 1) {
    return;
  }

  //行の高さの配列
  let lineHeightArray = [];
  //行ごとの要素の高さを取得
	targets.forEach((target,i) => {
		const height = target.offsetHeight;
		//行番号を取得
		const arrayIndex = Math.floor(i / column);
    if (lineHeightArray.length <= arrayIndex || lineHeightArray[arrayIndex] < height) {
				lineHeightArray[arrayIndex] = height;
    }
	});

  //要素の高さを変更
	targets.forEach((target,i) => {
		target.style.height = lineHeightArray[Math.floor(i / column)] + 'px';
	});
}

//高さを揃え関数呼び出し
listHeight({
	targets: '.js-test-text',  //対象リスト
	column: 4,  //カラム数
});
//レスポンシブ対応
let lastWinWidth = window.innerWidth;
window.addEventListener('resize', () => {
	// 現在と前回の横幅が違う場合だけ実行
	if (lastWinWidth !== window.innerWidth) {
		lastWinWidth = window.innerWidth;

		//高さを揃え関数呼び出し
		listHeight({
			targets: '.test-text',  //対象リスト
			column: 4,  //カラム数
		});
	}
});


//テストスライダー
const sliders = document.querySelectorAll('.swiper');
if(sliders.length > 0) {

	//スライダーパーツの高さを揃える関数
	const sliderListHeight = (slider) => {
		const targets = slider.querySelectorAll('.js-test-title01');
		targetsLength = targets.length;
		if(targetsLength > 0) {
			//高さを揃え関数呼び出し
			listHeight({
				targets: '.js-test-title01',  //対象リスト
				parent: slider,  //検索対象にする親要素
				column: targetsLength,  //カラム数
			});
			//column数は変わらないので別の要素にも合わせて適応できす
			listHeight({
				targets: '.js-test-title02',  //対象リスト
				parent: slider,  //検索対象にする親要素
				column: targetsLength,  //カラム数
			});
		}
	};

	//スライダー定義
	sliders.forEach((slider) => {
		const swiper = new Swiper(slider, {
			loop: true,
			slidesPerView: 3,
			spaceBetween: 30,
			pagination: {
				el: '.swiper-pagination',
				clickable: true,
			},
			navigation: {
				nextEl: '.swiper-button-next',
				prevEl: '.swiper-button-prev',
			},
			on: {
				//スライダーが定義された時に高さを揃える
				init: ()=> {
					sliderListHeight(slider);
				},
				//レスポンシブ対応
				resize: ()=> {
					sliderListHeight(slider);
				}
			},
		});

	});
}
              
            
!
999px

Console