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="ph app" id="app">
<header class="ph header">
<a class="ph home" href="https://phun-ky.net" target="_blank">home</a>
<a class="ph projects-home" href="https://phun-ky.net/projects" target="_blank">projects</a>
<div class="ph header-menu">
<a class="ph" href="https://www.paypal.me/phunky/20" rel="noopener noreferrer" target="_blank">buy me a coffee?</a>
<a class="ph" href="https://github.com/sponsors/phun-ky" rel="noopener noreferrer" target="_blank">sponsor me!</a>
<button class="ph darkmode-button">
<svg xmlns="http://www.w3.org/2000/svg" aria-label="Sun Icon" fill="currentColor" stroke="none" class="ph hide-dark" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z" clip-rule="evenodd"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" aria-label="Moon Icon" fill="currentColor" stroke="none" class="ph hide-light" viewBox="0 0 20 20">
<path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"></path>
</svg>
</button>
</div>
</header>
<main class="ph">
<section class="ph section">
<div class="ph container">
<h1 class="ph">Invoker shim</h1>
<p class="ph"></p>
</div>
</section>
<section class="ph section">
<div class="ph container">
<div class="ph counter">
<button class="ph" commandfor="num" command="stepDown">-</button>
<input class="ph" type="number" min="1" id="num" value="1" />
<button class="ph" commandfor="num" command="stepUp">+</button>
</div>
<p class="ph">
Click on the minus and plus buttons, and the invoker shim will work :)
</p>
<div class="ph message note">
<span class="ph title"><svg class="ph icon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
<path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path>
</svg>Note</span>
<p class="ph">
This is just a shim, if you need a polyfill, check out <a href="https://github.com/keithamus/invokers-polyfill" class="ph">https://github.com/keithamus/invokers-polyfill</a>
</p>
</div>
</div>
</section>
<section class="ph section">
<div class="ph container">
<pre class="ph language-html"><code class="ph language-html"><div class="ph counter">
<button class="ph" commandfor="num" command="stepDown">-</button>
<input class="ph" type="number" min="1" id="num" value="1" />
<button class="ph" commandfor="num" command="stepUp">+</button>
</div></code></pre>
</div>
</section>
<section class="ph section">
<div class="ph container">
<pre class="ph language-js"><code class="ph language-js">const invoke = (): void => {
document.querySelectorAll("[commandfor]").forEach((element) => {
const command = element.getAttribute("command");
const targetElementSelector = element.getAttribute("commandfor");
if (!command || !targetElementSelector) return;
const targetElement = document.getElementById(targetElementSelector);
if (!targetElement) return;
const handleClick = () => {
try {
if (typeof targetElement[command as keyof HTMLElement] === "function") {
(targetElement[command as keyof HTMLElement] as Function).call(
targetElement
);
} else {
console.error(
`Command '${command}' is not a function on the target element.`
);
}
} catch (e) {
console.error(
`Failed to execute command '${command}' on target element:`,
e
);
}
};
element.removeEventListener("click", handleClick);
element.addEventListener("click", handleClick);
});
};
// Bind command events when the script is loaded
invoke();</code></pre>
</div>
</section>
</main>
<footer class="ph">
<div class="ph container">
<p class="ph">
Made with <strong class="ph">saturated fat</strong> by
<a class="ph" target="_blank" href="https://phun-ky.net">Alexander Vassbotn Røyne-Helgesen</a>. Buy <strong class="ph">me</strong> some
<a class="ph" href="https://www.paypal.me/phunky/20">bacon?</a>
</p>
<p class="ph">
This project created by
<a rel="noopener noreferrer" target="_blank" class="ph" href="http://phun-ky.net" property="cc:attributionName">
Alexander Vassbotn Røyne-Helgesen</a>
is licensed under a
<a rel="noopener noreferrer" target="_blank" class="ph" href="https://choosealicense.com/licenses/mit/">
MIT License </a>.
</p>
<p class="ph">
<a rel="noopener noreferrer" target="_blank" class="ph" rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
<img alt="Creative Commons Licence" style="border-width: 0" width="88" height="31" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
</a>
<br />Any other creation or work related to this project, if not specified, like
templates, logos etc created by
<a rel="noopener noreferrer" target="_blank" class="ph" href="http://phun-ky.net" property="cc:attributionName" rel="cc:attributionURL">
Alexander Vassbotn Røyne-Helgesen</a>
is licensed under a
<a rel="noopener noreferrer" target="_blank" class="ph" rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
Creative Commons Attribution-NonCommercial-ShareAlike 4.0
International License </a>.
</p>
</div>
</footer>
</div>
<script src="https://unpkg.com/prismjs@1.29.0/prism.js"></script>
<script src="https://unpkg.com/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script src="https://unpkg.com/prismjs@1.29.0/components/prism-css-extras.min.js"></script>
<script src="https://unpkg.com/prismjs@1.29.0/plugins/inline-color/prism-inline-color.min.js"></script>
<link async href="https://unpkg.com/prismjs@1.29.0/plugins/inline-color/prism-inline-color.min.css" rel="stylesheet" type="text/css" />
*.ph,
*.ph::after,
*.ph::before {
margin: 0;
padding: 0;
word-break: break-word;
box-sizing: border-box;
-webkit-font-smoothing: antialised;
}
html {
font-family: var(
--ph-font-family,
system-ui,
-apple-system,
Segoe UI,
Roboto,
Helvetica,
Arial,
sans-serif,
Apple Color Emoji,
Segoe UI Emoji
);
}
html,
body {
height: 100%;
min-height: 100%;
}
body {
background-color: var(--ph-color-background, #181a22);
color: var(--ph-color-text, #9fa8ad);
padding: 0;
margin: 0;
line-height: 1.5;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif;
}
main.ph {
flex-grow: 1;
}
.ph.container {
max-width: 48rem;
width: 100%;
margin-left: auto;
margin-right: auto;
margin-top: 2.5rem;
margin-bottom: 2.5rem;
}
.ph.frontpage .ph.container {
margin-bottom: 1.25rem;
margin-top: 1.25rem;
max-width: 1024px;
}
.ph.project .ph.banner .ph.container {
max-width: 48rem;
width: 100%;
margin-top: 2.5rem;
margin-bottom: 2.5rem;
padding-top: 6rem;
padding-bottom: 6rem;
}
footer.ph.footer {
font-size: 1.25rem;
z-index: 100;
flex-grow: 0;
margin-top: auto;
}
.ph.app {
display: flex;
flex-direction: column;
min-height: 100%;
position: relative;
padding: 1.25rem;
}
a.ph {
color: var(--ph-color-link, #6ca7f9);
font-weight: 500;
}
a.ph:visited {
color: var(--ph-color-link-visited, #6ca7f9);
}
a.ph:active {
color: var(--ph-color-link-active, #6ca7f9);
}
a.ph:hover {
color: var(--ph-color-link-hover, #6ca7f9);
}
p,
p.ph,
.ph.byline {
font-style: normal;
max-width: 65ch;
font-size: 1rem;
line-height: 1.75rem;
color: var(--ph-color-text, #9fa8ad);
margin-top: 1.25em;
margin-bottom: 1.25em;
}
.ph.byline time,
.ph.byline .ph.read-time {
opacity: 0.7;
}
article.ph {
color: var(--ph-color-text, #9fa8ad);
font-size: 1rem;
line-height: 1.75rem;
}
p.lead,
p.ph.lead {
color: var(--ph-color-text-accent, #e4e4e7);
font-weight: 600;
line-height: 1.75rem;
margin-bottom: 2.5rem;
font-size: 1.125rem;
}
.ph.frontpage p.ph.lead {
padding-top: 2.5rem;
padding-bottom: 2.5rem;
margin: 0;
font-weight: normal;
color: inherit;
}
.ph.author p.ph,
footer.ph p.ph {
max-width: none;
}
h1.ph,
h2.ph,
h3.ph,
h4.ph,
h5.ph,
h6.ph {
color: var(--ph-color-text-accent, #e4e4e7);
}
h1.ph {
margin-bottom: 2.5rem;
font-size: 2.25rem;
line-height: 1.25;
font-weight: 700;
}
@media screen and (min-width: 768px) {
h1.ph {
font-size: 3rem;
line-height: 1.375;
}
}
@media screen and (min-width: 1024px) {
h1.ph {
font-size: 3.75rem;
line-height: 1.375;
}
}
h1.ph > em.ph {
font-weight: normal;
max-width: 65ch;
font-size: 1rem;
line-height: 1.75rem;
display: block;
color: var(--ph-color-text);
}
.ph.frontpage h1.ph {
margin-bottom: 2.5rem;
font-size: 3rem;
line-height: 1.375;
}
@media screen and (min-width: 1024px) {
.ph.frontpage h1.ph {
font-size: 4.75rem;
}
}
h2.ph {
font-size: 1.75rem;
line-height: 1.3333333;
margin-bottom: 1rem;
margin-top: 2rem;
}
h3.ph {
font-size: 1.5rem;
line-height: 1.3333333;
margin-bottom: 1rem;
margin-top: 1rem;
font-weight: 400;
}
h4.ph {
font-size: 1.25rem;
line-height: 1.3333333;
margin-bottom: 1rem;
margin-top: 1rem;
font-weight: 500;
text-transform: uppercase;
color: var(--ph-color-text);
}
header.ph.header {
display: flex;
justify-content: flex-start;
align-items: center;
flex-grow: 0;
grid-gap: 1rem;
min-height: 3rem;
}
@media screen and (min-width: 1440px) {
header.ph.header {
position: sticky;
top: 20px;
z-index: 100;
}
.ph.project header.ph.header {
position: static;
top: unset;
}
}
.ph.header-menu {
margin-left: auto;
min-height: 3rem;
display: flex;
align-items: center;
justify-content: flex-end;
grid-gap: 1rem;
}
.ph.header-menu a.ph {
display: none;
}
@media screen and (min-width: 640px) {
.ph.header-menu a.ph {
display: inline-block;
}
}
.ph.darkmode-button {
align-items: center;
background-color: transparent;
border: none;
border-radius: 50%;
color: var(--ph-color-text);
cursor: pointer;
display: flex;
height: 3rem;
justify-content: center;
width: 3rem;
}
.ph.darkmode-button:active,
.ph.darkmode-button:hover {
background-color: var(--ph-color-well-background);
}
.ph.darkmode-button > svg.ph {
pointer-events: none;
height: 2rem;
width: 2rem;
}
.ph.hide-dark {
display: none;
}
.ph.hide-light {
display: block;
}
.ph.theme--light .hide-light {
display: none;
}
.ph.theme--light .hide-dark {
display: block;
}
p.ph:has(> img[src^="/img"]) {
max-width: none;
}
.ph.counter button.ph {
height: 3rem;
width: 3rem;
font-size: 2rem;
font-weight: bold;
}
.ph.counter input.ph {
height: 3rem;
width: auto;
max-width: 5rem;
font-size: 2rem;
font-weight: bold;
}
.ph.counter {
display: flex;
align-items: center;
justify-content: flex-start;
grid-gap: 0.5rem;
}
/**
Template stuff, ignore this
**/
code[class*="language-"] .namespace,
pre[class*="language-"] .namespace {
opacity: 0.7;
}
code[class*="language-"] .token.comment,
code[class*="language-"] .token.prolog,
code[class*="language-"] .token.doctype,
code[class*="language-"] .token.cdata,
pre[class*="language-"] .token.comment,
pre[class*="language-"] .token.prolog,
pre[class*="language-"] .token.doctype,
pre[class*="language-"] .token.cdata {
color: var(--ph-color-code-color-1);
}
code[class*="language-"] .token.null,
code[class*="language-"] .token.operator,
code[class*="language-"] .token.boolean,
code[class*="language-"] .token.number,
pre[class*="language-"] .token.null,
pre[class*="language-"] .token.operator,
pre[class*="language-"] .token.boolean,
pre[class*="language-"] .token.number {
color: var(--ph-color-code-color-2);
}
code[class*="language-"] .token.attr-name,
code[class*="language-"] .token.string,
pre[class*="language-"] .token.attr-name,
pre[class*="language-"] .token.string {
color: var(--ph-color-code-color-3);
}
code[class*="language-"] .token.entity,
code[class*="language-"] .token.url,
.language-css code[class*="language-"] .token.string,
.style code[class*="language-"] .token.string,
pre[class*="language-"] .token.entity,
pre[class*="language-"] .token.url,
.language-css pre[class*="language-"] .token.string,
.style pre[class*="language-"] .token.string {
color: var(--ph-color-code-color-3);
}
code[class*="language-"] .token.selector,
pre[class*="language-"] .token.selector {
color: var(--ph-color-code-block);
}
code[class*="language-"] .token.atrule,
code[class*="language-"] .token.attr-value,
code[class*="language-"] .token.keyword,
code[class*="language-"] .token.control,
code[class*="language-"] .token.directive,
code[class*="language-"] .token.important,
code[class*="language-"] .token.unit,
pre[class*="language-"] .token.atrule,
pre[class*="language-"] .token.attr-value,
pre[class*="language-"] .token.keyword,
pre[class*="language-"] .token.control,
pre[class*="language-"] .token.directive,
pre[class*="language-"] .token.important,
pre[class*="language-"] .token.unit {
color: var(--ph-color-code-color-4);
}
code[class*="language-"] .token.regex,
code[class*="language-"] .token.statement,
pre[class*="language-"] .token.regex,
pre[class*="language-"] .token.statement {
color: var(--ph-color-code-color-3);
}
code[class*="language-"] .token.placeholder,
code[class*="language-"] .token.variable,
pre[class*="language-"] .token.placeholder,
pre[class*="language-"] .token.variable {
color: var(--ph-color-code-color-4);
}
code[class*="language-"] .token.property,
code[class*="language-"] .token.tag,
pre[class*="language-"] .token.property,
pre[class*="language-"] .token.tag {
font-style: italic;
}
code[class*="language-"] .token.important,
code[class*="language-"] .token.statement,
pre[class*="language-"] .token.important,
pre[class*="language-"] .token.statement {
font-weight: bold;
}
code[class*="language-"] .token.punctuation,
pre[class*="language-"] .token.punctuation {
color: var(--ph-color-code-color-5);
}
code[class*="language-"] .token.entity,
pre[class*="language-"] .token.entity {
cursor: help;
}
code[class*="language-"] .token.debug,
pre[class*="language-"] .token.debug {
color: var(--ph-color-code-color-8);
}
pre,
pre.ph {
background-color: var(--ph-color-code-block-background);
border-radius: 0.375rem;
color: var(--ph-color-code-block);
font-size: 0.875rem;
line-height: 1.5rem;
margin-bottom: 1.5rem;
margin-top: 1.5rem;
max-height: 80vh;
overflow-x: auto;
padding: 0.75rem 1rem;
}
@media screen and (min-width: 1024px) {
pre,
pre.ph {
font-size: 1rem;
line-height: 1.75rem;
margin-bottom: 2rem;
margin-top: 2rem;
padding: 1rem 1.5rem;
}
}
code,
kbd,
samp,
pre {
font-family: var(--ph-font-family-code);
}
.ph span.inline-color-wrapper {
border-radius: 50%;
border: 1pt solid white;
background-clip: padding-box;
outline: none;
margin: 0 0.5ch 0 0.333ch;
}
.theme--light .ph span.inline-color-wrapper {
border: 1pt solid black;
}
console.clear();
/**
* @author Alexander Vassbotn Røyne-Helgesen <alexander@phun-ky.net>
* @license
* Copyright (c) 2024 Alexander Vassbotn Røyne-Helgesen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* An invoker shim (https://open-ui.org/components/invokers.explainer/)
* It binds click events to elements with a `commandfor` attribute to execute a specified command on a target element.
*
* This function selects all elements with a `commandfor` attribute and adds a click event listener that executes
* the specified command on the target element identified by the `commandfor` attribute.
*
* @example
* ```ts
* // HTML example
* // <button command="focus" commandfor="inputField">Focus Input</button>
* // <input id="inputField" type="text" />
*
* // JavaScript
* invoke();
* ```
*/
const invoke = (): void => {
document.querySelectorAll("[commandfor]").forEach((element) => {
const command = element.getAttribute("command");
const targetElementSelector = element.getAttribute("commandfor");
if (!command || !targetElementSelector) return;
const targetElement = document.getElementById(targetElementSelector);
if (!targetElement) return;
const handleClick = () => {
try {
if (typeof targetElement[command as keyof HTMLElement] === "function") {
(targetElement[command as keyof HTMLElement] as Function).call(
targetElement
);
} else {
console.error(
`Command '${command}' is not a function on the target element.`
);
}
} catch (e) {
console.error(
`Failed to execute command '${command}' on target element:`,
e
);
}
};
element.removeEventListener("click", handleClick);
element.addEventListener("click", handleClick);
});
};
// Bind command events when the script is loaded
invoke();
Also see: Tab Triggers