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.
<h1>Toggling accordions using 5 different methods</h1>
<section>
<details>
<summary class=toggler_1>Pure HTML, zero JS, zero CSS [recommended 2022]</summary>
<div class=tog_content>
<p>The <code>summary</code> element acts as a button, and the element that follows is the hidden content.</p>
<figure>
<figcaption>HTML</figcaption>
<pre><code class=language-markup spellcheck=false contenteditable><details>
<summary>Pure HTML accordian</summary>
<div>
<!-- Drop down content -->
</div>
</details></code></pre>
</figure>
</div>
</section>
<section>
<!-- Ref: https://www.w3.org/WAI/GL/wiki/Using_aria-expanded_to_indicate_the_state_of_a_collapsible_element -->
<button class=toggler_1 aria-expanded=false onclick="this.setAttribute('aria-expanded',this.getAttribute('aria-expanded')==='false')">Toggle <strong>next sibling</strong> using <strong>inline</strong> JS [simplest]</button>
<div class=toggled_1>
<div class=tog_content>
<h2 class=tog_title>Accessibily toggle a buttons <strong>next sibling</strong> using <strong>inline</strong> JS (on the button only).</h2>
<figure>
<figcaption>HTML <small>(with inline JavaScript)</small></figcaption>
<pre><code class=language-markup spellcheck=false contenteditable><button class="toggler"
onclick="this.setAttribute('aria-expanded', this.getAttribute('aria-expanded') === 'false');"
aria-expanded="false">Toggle button</button>
<div class="toggled">Toggled content</div></code></pre>
</figure>
<figure>
<figcaption>CSS</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.toggler[aria-expanded="false"] + .toggled {
overflow: hidden;
visibility: hidden;
opacity: 0;
max-height: 0;
transition:
visibility 0s ease .6s,
opacity .6s ease-in 0s,
max-height .6s ease-out 0s;
}
.toggler[aria-expanded="true"] + .toggled {
overflow: hidden;
visibility: visible;
opacity: 1;
/* Ensure max-height is sufficient at min-width */
max-height: 20rem;
transition:
visibility 0s ease 0s,
opacity .6s ease-in 0s,
max-height .6s ease-out 0s;
}</code></pre>
</figure>
</div>
</div>
</section>
<section>
<label class=tog_container>
<span class=toggler_2>Toggle <strong>next sibling</strong> with <strong>zero</strong> JavaScript</span>
<input class=visually-hidden type=checkbox>
<span class=toggled_2>
<span class=tog_content>
<em class=tog_title>Toggle <strong>next sibling</strong>. HTML & CSS only, no JavaScript</em>
<figure>
<figcaption>HTML</figcaption>
<pre><code class=language-markup spellcheck=false contenteditable><label>
<span class="toggler">Toggle button</span>
<input class="visually-hidden" type="checkbox">
<span class="toggled">Toggled content</span>
</label></code></pre>
</figure>
<figure>
<figcaption>CSS</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.toggler, .toggled {display: block}
.toggled {
overflow: hidden;
visibility: hidden;
opacity: 0;
max-height: 0;
transition:
visibility 0s ease .6s,
opacity .6s ease-in 0s,
max-height .6s ease-out 0s;
}
:checked + .toggled {
visibility: visible;
opacity: 1;
/* Ensure max-height is sufficient at min-width */
max-height: 50rem;
transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}</code></pre>
</figure>
</span>
</span>
</label>
</section>
<section>
<div>
<button aria-expanded=false aria-controls=toggled_5
onclick="(function(b){var s=b.getAttribute('aria-expanded')==='true',c=document.getElementById(b.getAttribute('aria-controls'));if(!c)return;c.setAttribute('style','max-height:'+c.scrollHeight+'px');b.setAttribute('aria-expanded',!s);c.setAttribute('aria-hidden',s);})(this);"
class=toggler_3>
Toggle a <strong>specified element</strong> using <strong>inline</strong> JS
</button>
</div>
<div class=toggled_5 id=toggled_5 aria-hidden=true>
<div class=tog_content>
<h2 class=tog_title>Accessibily toggle a buttons <strong>specified</strong> content panel using <strong>inline</strong> JS (on the button only). Button and content panel do not need to be siblings.</h2>
<figure>
<figcaption>HTML <small>(with inline JavaScript)</small></figcaption>
<pre><code class=language-markup spellcheck=false contenteditable><button class="toggler"
onclick="(function(b){var s=b.getAttribute('aria-expanded')==='true',c=document.getElementById(b.getAttribute('aria-controls'));if(!c)return;c.setAttribute('style','max-height:'+c.scrollHeight+'px');b.setAttribute('aria-expanded',!s);c.setAttribute('aria-hidden',s);})(this);"
aria-controls="toggled"
aria-expanded="false">Toggle button</button>
<div aria-hidden="true" id="toggled" class="toggled">Toggled content</div>
</code></pre>
</figure>
<p>The <code>max-height</code> of the content panel is accurately calculated by the buttons inline JavaScript.</p>
<figure>
<figcaption>CSS</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.toggled[aria-hidden] {
overflow: hidden;
visibility: hidden; /* Removes content from keyboard focus chain */
opacity: 0;
max-height: 0;
transition:
visibility 0s linear .6s,
opacity .6s ease-in,
max-height .6s ease-out;
}
.toggled[aria-hidden="true"] {
max-height: 0 !important; /* Overrides inline style */
}
.toggled[aria-hidden="false"] {
visibility: visible;
opacity: 1;
max-height: 50em; /* Set more accurately inline by JavaScript */
transition-delay: 0s, 0s, 0s;
transition-timing-function: linear, linear, ease-out;
}
</code></pre>
</figure>
<p>See the full pen for details: <a target=_blank title="[new window]" href="https://codepen.io/2kool2/pen/aGOmxZ">Accordion expand/collapse using inline JavaScript</a></p>
<p>Which also contains optional extensions to:</p>
<ul>
<li>Open from URL</li>
<li>Open from anchor</li>
<li>Scroll button to top</li>
<li>Embed accordions</li>
<li>Open a single accordion.</li>
</ul>
</div>
</div>
</section>
<section>
<div>
<button class=toggler_3 data-toggler="#toggled_3">
Toggle a <strong>specified element</strong> using JS
</button>
</div>
<div class="toggled_3 xtoggled-long" id=toggled_3>
<div class=tog_content>
<h2 class=tog_title>Accessibily toggle a <strong>specified element</strong>, and account for variable height, using JavaScript</h2>
<figure>
<figcaption>HTML</figcaption>
<pre><code class=language-markup spellcheck=false contenteditable><button data-toggler="#toggled">Toggle button</button>
<div id="toggled" class="toggled">Toggled content</div>
</code></pre>
</figure>
<figure>
<figcaption>CSS</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.toggled[aria-hidden] {
overflow: hidden;
visibility: hidden;
opacity: 0;
max-height: 0;
transition:
visibility 0s linear .6s,
opacity .6s ease-in,
max-height .6s ease-out;
}
.toggled[aria-hidden="true"] {
max-height: 0 !important; /* Overrides inline style */
}
.toggled[aria-hidden="false"] {
visibility: visible;
opacity: 1;
max-height: 50em; /* Set more accurately inline by JavaScript */
transition-delay: 0s, 0s, 0s; /* unit "s" is required */
transition-timing-function: linear, linear, ease-out;
}</code></pre>
</figure>
<figure>
<figcaption>JavaScript</figcaption>
<pre><code class=language-javascript spellcheck=false contenteditable>const toggler = (() => {
const dataAttr = "data-toggler";
// Get height of an element object
// Assumes it is hidden by max-height: 0 in the CSS
const getHeight = (obj) => {
obj.setAttribute("style", "max-height: auto");
const height = obj.scrollHeight + "px";
obj.removeAttribute("style");
return height;
};
// Toggles the show/hide state of button and block using ARIA attributes
const toggleState = (togglerBtn, isHidden) => {
const toggledAttr = togglerBtn.getAttribute(dataAttr);
if (!toggledAttr) {return;}
const toggled = document.querySelector(toggledAttr);
if (!toggled) {return;}
if (isHidden) { // Show
toggled.setAttribute("style", "max-height: " + getHeight(toggled));
} else { // Hide
toggled.removeAttribute("style");
}
togglerBtn.setAttribute("aria-expanded", isHidden);
toggled.setAttribute("aria-hidden", !isHidden);
};
const isOpen = (togglerBtn) => {
return togglerBtn.getAttribute("aria-expanded") === "true";
};
const togglerClicked = (e) => {
const togglerBtn = e.target;
toggleState(togglerBtn, !isOpen(togglerBtn));
};
const initialise = (() => {
const togglerButtons = document.querySelectorAll("["+ dataAttr + "]");
// Run through each toggle button which has a data-toggler3 set
for (const togglerBtn of togglerButtons) {
// Set ARIA states
toggleState(togglerBtn, isOpen(togglerBtn));
// Toggle ARIA state on a button click
togglerBtn.addEventListener("click", togglerClicked);
}
})();
})();</code></pre>
</figure>
</div>
</div>
</section>
<section>
<div>
<button class=toggler_3 data-toggler="#toggled_4">
Toggle a <strong>specified element</strong> using JS (short content)
</button>
</div>
<div class="toggled_3 toggled-short" id=toggled_4>
<div class=tog_content>
<figure class=tog_fig-solo>
<figcaption>CSS</figcaption>
<pre><code class=language-css spellcheck=false contenteditable>.toggled[aria-hidden].toggled-short {
transition-duration: 0s, .4s, .4s;
}</code></pre>
</figure>
</div>
</div>
</section>
<!-- Footer include -->
[[[https://codepen.io/2kool2/pen/mKeeGM]]]
/* Generic styles */
*, *::after, *::before {
box-sizing: inherit;
}
html {
overflow-y: scroll;
touch-action: manipulation;
}
body {
font-family: sans-serif;
padding: 0 2rem;
max-width: 50rem;
margin: 2rem auto;
text-align: center;
line-height: 1.5;
backgound-color: hsl(0, 0%, 35%);
background-position: center bottom;
background-size: cover;
background-attachment: fixed;
/* Unsplash: Ronald Yang */
background-image: url(https://images.unsplash.com/photo-1434144893279-2a9fc14e9337?w=1920);
}
h1 {
font-weight: 100;
color: #fff;
text-shadow: 0 0 3px #000;
background-color: hsla(0, 0%, 35%, 0.9);
margin: 3rem 0;
line-height: 1.2;
padding: 0.25rem;
}
section {
margin: 3rem 0;
}
ul {
max-width: 12rem;
margin: 1rem auto;
}
li {text-align: left}
.tog_title {
display: block;
font-size: 1rem;
font-weight: 100;
font-style: normal;
margin: 0;
text-shadow: 0 0 2px #000;
}
figure {
position: relative;
margin: 1rem 0 0;
}
figcaption {
font-weight: 700;
padding: 0;
margin: 0;
left: 1rem;
font-size: 1rem;
letter-spacing: 0.05em;
text-shadow: 0 1px #000;
}
.smaller,
a {
color:#fff;
background-color: hsla(0, 0%, 35%, 0.8);
padding:.5rem;
text-shadow: 0 1px #000;
letter-spacing: 0.05em;
}
.visually-hidden {
/*https://developer.yahoo.com/blogs/ydn/clip-hidden-content-better-accessibility-53456.html*/
position: absolute !important;
clip: rect(1px, 1px, 1px, 1px);
padding:0 !important;
border:0 !important;
height: 1px !important;
width: 1px !important;
overflow: hidden;
}
button * {pointer-events:none;}
button::-moz-focus-inner {
border: 0;
padding: 0;
margin-top:-2px;
margin-bottom: -2px;
}
[class^="toggler"] {
/* Just the button prettiness here */
font-size: 1.125rem;
line-height: 1.2;
display: block;
box-sizing: border-box;
width: 100%;
margin: 0;
padding: 1rem;
border: 0 solid;
background-color: hsl(0, 0%, 80%);
cursor: pointer;
transition: all .3s ease-out;
outline: 0 solid;
box-shadow: 0 2px 4px rgba(0,0,0,.5);
}
[class^="toggler"]:hover,
[class^="toggler"]:focus {
background-color: hsl(0, 0%, 90%);
outline: 0 solid;
box-shadow: 0 3px 6px rgba(0,0,0,.75);
}
[class^="toggler"]:active {
box-shadow: inset 0 0 2px rgba(0,0,0,.75);
}
[class^="toggled"] {
box-shadow: 0 3px 6px rgba(0,0,0,.75);
}
.tog_content {
background-color: hsla(0, 0%, 35%, 0.8);
color: #fff;
display: block;
padding: 1rem;
}
.tog_fig-solo {
margin:0
}
/* Next Sibling inline JS */
.toggler_1[aria-expanded="false"] + .toggled_1 {
overflow: hidden;
visibility: hidden;
opacity: 0.35;
max-height: 0;
transition:
visibility 0s ease .6s,
opacity .6s ease-in 0s,
max-height .6s ease-out 0s;
}
.toggler_1[aria-expanded="true"] + .toggled_1 {
overflow: hidden;
visibility: visible;
opacity: 1;
/* Note: Manually set, check content visible at minimum viewport widths */
max-height: 75rem;
transition:
visibility 0s ease 0s,
opacity .6s ease-in 0s,
max-height 1s ease-out 0s;
}
/* Specified element using inline JS*/
.toggled_5[aria-hidden] {
overflow: hidden;
visibility: hidden; /* Removes content from keyboard focus chain */
opacity: 0;
max-height: 0;
transition:
visibility 0s linear .6s,
opacity .6s ease-in,
max-height .6s ease-out;
}
.toggled_5[aria-hidden="true"] {
max-height: 0 !important; /* Overrides inline style */
}
.toggled_5[aria-hidden="false"] {
visibility: visible;
opacity: 1;
max-height: 50em; /* Set more accurately inline by JavaScript */
transition-delay: 0s, 0s, 0s;
transition-timing-function: linear, linear, ease-out;
}
/* Next sibling without JS */
.tog_container,
.toggler_2,
.toggled_2 {
display: block;
}
.toggled_2 {
overflow: hidden;
visibility: hidden;
opacity: 0.35;
max-height: 0;
transition:
visibility 0s ease .6s,
opacity .6s ease-in 0s,
max-height .6s ease-out 0s;
}
:checked + .toggled_2 {
visibility: visible;
opacity: 1;
/* Ensure max-height is sufficient at min-width */
max-height: 50rem;
transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}
/* Specified element using unobtrusive JS */
.toggled_3[aria-hidden] {
overflow: hidden;
visibility: hidden;
opacity: 0;
max-height: 0;
transition:
visibility 0s linear .6s,
opacity .6s ease-in,
max-height .6s ease-out;
}
.toggled_3[aria-hidden="true"] {
max-height: 0 !important; /* Overrides inline style */
}
.toggled_3[aria-hidden="false"] {
visibility: visible;
opacity: 1;
max-height: 50em; /* Set more accurately inline by JavaScript */
transition-delay: 0s, 0s, 0s; /* unit "s" is required */
transition-timing-function: linear, linear, ease-out;
}
/* Specified element, short content addition */
[class^="toggled"][aria-hidden].toggled-short {
transition-duration: 0s, .4s, .4s;
}
/* Specified element, extra long content addition */
.toggled-long[aria-hidden="true"] { /* Closing */
transition-duration: 0s, .6s, .6s;
transition-delay: 0.8s, 0s, 0s; /* unit "s" is required */
}
.toggled-long[aria-hidden="false"] { /* Opening */
transition-duration: 0s, .8s, .8s;
transition-delay: 0s, 0s, 0s; /* unit "s" is required */
}
/* Prism - is used for code highlighting from an external pen: https://codepen.io/2kool2/pen/MEbeEg */
// JS is needed to toggle an object which isn't a sibling.
const toggler = (() => {
const dataAttr = "data-toggler";
// Get height of an element object
// Assumes it is hidden by max-height: 0 in the CSS
const getHeight = (obj) => {
obj.setAttribute("style", "max-height: auto");
const height = obj.scrollHeight + "px";
obj.removeAttribute("style");
return height;
};
// Toggles the show/hide state of button and block using ARIA attributes
const toggleState = (togglerBtn, isHidden) => {
const toggledAttr = togglerBtn.getAttribute(dataAttr);
if (!toggledAttr) {return;}
const toggled = document.querySelector(toggledAttr);
if (!toggled) {return;}
if (isHidden) { // Show
//toggled.setAttribute("style", "max-height: " + getHeight(toggled));
toggled.setAttribute('style', 'max-height:' + toggled.scrollHeight + 'px');
} else { // Hide
//toggled.removeAttribute("style");
}
togglerBtn.setAttribute("aria-expanded", isHidden);
toggled.setAttribute("aria-hidden", !isHidden);
};
const isOpen = (togglerBtn) => {
return togglerBtn.getAttribute("aria-expanded") === "true";
};
const togglerClicked = (e) => {
const togglerBtn = e.target;
toggleState(togglerBtn, !isOpen(togglerBtn));
};
const initialise = (() => {
const togglerButtons = document.querySelectorAll("["+ dataAttr + "]");
// Run through each toggle button which has a data-toggler3 set
for (const togglerBtn of togglerButtons) {
// Set ARIA states
toggleState(togglerBtn, isOpen(togglerBtn));
// Toggle ARIA state on a button click
togglerBtn.addEventListener("click", togglerClicked);
}
})();
})();
/* Prism - is used for code highlighting from an external pen: https://codepen.io/2kool2/pen/MEbeEg */
Also see: Tab Triggers