HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
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;
}
//要素の高さ調整関数
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);
}
},
});
});
}
Also see: Tab Triggers