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="ly_wrapper" id="ly_wrapper">
<a href="#dialog" class="js_openModal">クリックでモーダルが開きます</a>
<dialog id="dialog">
<div class="dialog_container">
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Et ipsa sit dolore quam fuga? Accusantium atque ipsam cupiditate repellendus fugiat magnam voluptate enim totam hic nam aliquam, reprehenderit velit error. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quibusdam sed accusantium quo odit, quasi hic, rerum animi voluptate itaque maxime harum. Nemo similique vitae ut adipisci repellat voluptatum temporibus unde. Lorem ipsum dolor sit amet consectetur, adipisicing elit. In vel illum sit dicta repudiandae ab distinctio rem enim? Ut, velit! Ad sint cum, non eligendi ullam eaque eos quasi quod! Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam ipsa quibusdam porro repudiandae ducimus quo aliquid animi atque consectetur quidem! Dolor soluta a eligendi perspiciatis esse voluptas id, quos fugit?</p>
<button class="dialog_closeBtn" aria-label="モーダルを閉じる"></button>
</div>
<div class="dialog_backdrop"></div>
</dialog>
</div>
:root {
--scrollbarW: 0px;
--dialogScrollbarW: 0px;
}
html {
&.is_locked {
position: fixed;
width: 100%;
}
}
body {
max-width: 100vw;
}
.ly_wrapper {
padding: 1em;
// height: 200vh;
}
dialog {
--_fadeDuration: 0.5s;
// 縦スクロールバーはモーダル領域全体に対して出したい(モーダル内に閉じるボタンを付けたときに違和感がある)ため、max-width等の指定を打ち消してdialogの領域を画面いっぱいに広げる
max-width: none;
max-height: 100%;
// フェードイン/アウトを有効にするため、display:noneを打ち消す(中央寄せのためdisplay:gridを指定)
display: grid;
// コンテンツ(.dialog_container)を中央寄せに
place-items: center;
// display:noneでなくなるので、本文(モーダル外のコンテンツ)よりモーダル内のコンテンツが長い場合に、そちらに引っ張られて縦スクロールバーが出るのを防ぐため、position:fixedとinsetを指定して位置を固定しておく
position: fixed;
inset: 0;
// .dialog_containerの外側にスペースを確保
padding: 40px;
// 背景を透過させる
background: transparent;
// フェードイン/アウトのための指定
opacity: 0;
visibility: hidden;
transition-property: opacity, visibility;
transition-duration: var(--_fadeDuration);
// モーダルをスクロールしたまま閉じる際、ガタつくのを防ぐ(ただし開発者ツールを開いたままだとこの指定をしてもガタつくのを確認)
overflow: hidden;
// 本文(モーダル外のコンテンツ)の縦スクロールバーなし&モーダル内縦スクロールバーありの場合のガタつきを防ぐ
:where(.ly_wrapper:not(.can_scroll)) & {
right: var(--dialogScrollbarW);
}
&[open] {
opacity: 1;
visibility: visible;
inset: 0;
overflow-y: auto;
}
// 本文(モーダル外のコンテンツ)で縦スクロールバーあり&モーダル内でなしの場合のガタつきを防ぐ
&:not(.can_scroll) {
right: calc(var(--scrollbarW) * -1);
}
// デフォルトのモーダル背景(::backdrop)はdialogにopen属性が付いている時だけ出現=フェードイン/アウトができないため、無効化して自前で用意
&::backdrop {
display: none;
}
.dialog_backdrop {
opacity: 0.4;
position: fixed;
inset: 0;
background-color: #000;
}
&[open] .dialog_backdrop {
// 自前の背景は縦スクロールバーより前面に来てしまうため、縦スクロールバーの幅分の余白を確保して被らないようにする
right: var(--dialogScrollbarW);
}
.dialog_container {
position: relative;
z-index: 1;
padding: 2em;
background-color: #fff;
p {
font-size: 2rem;
}
}
.dialog_closeBtn {
position: absolute;
right: 0;
top: 0;
width: 2em;
height: 2em;
background-color: #000;
&::before,
&::after {
content: "";
position: absolute;
left: 50%;
top: 50%;
border-bottom: 2px solid #fff;
width: 1em;
translate: -50% -50%;
}
&::before {
rotate: -45deg;
}
&::after {
rotate: 45deg;
}
}
}
// モーダル表示時に本文(モーダル外のコンテンツ)の縦スクロールをロックするため、toggleScrollLock関数とそのために必要な定数・変数を用意。
// 本文(モーダル外のコンテンツ)の縦スクロールバーを非表示にしないと、モーダル内に縦スクロールバーが出る場合に、モーダル外と合わせて2つ縦スクロールバーが出てしまう
const $window = $(window);
const $html = $('html');
let scrollTopVal = 0;
function toggleScrollLock(_scrollTopVal) {
if ($html.hasClass('is_locked')) {
$html.removeClass('is_locked');
$window.scrollTop(_scrollTopVal);
} else {
$html.addClass('is_locked').css({
top: -_scrollTopVal,
});
}
}
// htmlやbodyはheight:100%などを指定することが多いため、コンテンツ用のwrapperを用意し、リサイズ監視などはそちらで行う
const $ly_wrapper = $('#ly_wrapper');
// #ly_wrapperのResizeObserverを定義
const resizeObserver_wrapper = new ResizeObserver((entries) => {
entries.forEach((entry) => {
// #ly_wrapperの縦スクロールバーの有無を元に--scrollbarWを更新
const scrollbarW = window.innerWidth - document.body.clientWidth;
$html.css('--scrollbarW', `${scrollbarW}px`);
// 縦スクロール可能かどうかをcan_scrollクラスの有無でCSSから参照可能にしておく(モーダル開閉時のガタつきを防ぐため)
if ($html.css('--scrollbarW') !== '0px') {
$ly_wrapper.addClass('can_scroll');
} else {
$ly_wrapper.removeClass('can_scroll');
}
});
});
resizeObserver_wrapper.observe($ly_wrapper[0]);
// .js_openModal毎にセット化
$('.js_openModal').each(function() {
const $this = $(this);
// 対象のdialog要素を$targetDialogに格納
const $targetDialog = $($this.attr('href'));
// 対象のdialog要素内の閉じるボタン
const $closeBtn = $targetDialog.find('.dialog_closeBtn');
// 対象のdialog要素内の背景
const $backdrop = $targetDialog.find('.dialog_backdrop');
// 対象のdialog要素が取得できたときのみif内を実行(エラー回避のため)
if ($targetDialog.length) {
// 対象dialogのResizeObserverを定義
const resizeObserver_dialog = new ResizeObserver((entries) => {
entries.forEach((entry) => {
// 対象dialogの縦スクロールバーの有無を元に--dialogScrollbarWを更新
const dialogScrollbarW = window.innerWidth - $targetDialog[0].clientWidth;
$html.css('--dialogScrollbarW', `${dialogScrollbarW}px`);
// 縦スクロール可能かどうかをcan_scrollクラスの有無でCSSから参照可能にしておく(モーダル開閉時のガタつきを防ぐため)
if ($html.css('--dialogScrollbarW') !== '0px') {
$targetDialog.addClass('can_scroll');
} else {
$targetDialog.removeClass('can_scroll');
}
});
});
// .js_openModalクリック時
$this.on('click', function(event) {
// a要素クリック時の標準の挙動(リンク)をキャンセル
event.preventDefault();
// jQueryオブジェクトから添字[0]でDOMを取得し、dialogネイティブのメソッド(showModal)でモーダルを表示
$targetDialog[0].showModal();
// スクロールをロック
scrollTopVal = $window.scrollTop();
toggleScrollLock(scrollTopVal);
// ResizeObserverを有効化
resizeObserver_dialog.observe($targetDialog[0]);
});
// 閉じるボタンおよび背景のクリック時
$closeBtn.add($backdrop).on('click', function() {
// ResizeObserverを無効化
resizeObserver_dialog.unobserve($targetDialog[0]);
// dialogネイティブのメソッドclose()でモーダルを閉じる
$targetDialog[0].close();
// スクロールロックを解除
toggleScrollLock(scrollTopVal);
});
}
});
Also see: Tab Triggers