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="space-y-8">
<h1 class="text-3xl font-bold mb-5">Accessible smooth collapse experiment</h1>
<p>Primis ullamcorper laoreet morbi ex sem id, sapien molestie torquent aptent vivamus, felis dapibus diam lectus donec. <a href="https://wolstenhol.me" target="_top">Elit aenean eu eleifend vestibulum</a> sem quis commodo pretium sed rutrum aptent, proin cursus nunc sollicitudin vehicula dapibus iaculis est malesuada lobortis ante eget, nostra molestie morbi natoque luctus tortor bibendum taciti amet tristique.</p>
<div
x-data="collapse"
class="collapse border py-3 px-5 space-y-3"
@resize.window.debounce="updateHeight"
>
<h2>
<button
class="flex items-center w-full space-x-3 font-bold text-xl select-none text-left"
@click="toggle"
id="collapse-1-button"
aria-controls="collapse-1-content"
:aria-expanded="expanded ? 'true' : 'false'"
>
<svg
class="flex-shrink-0 w-6 h-6"
aria-hidden="true"
focusable="false"
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /> </svg>
<span>Expandable region heading</span>
</button>
</h2>
<section
class="space-y-2 overflow-hidden transition-all duration-300 ease-out"
x-ref="content"
aria-labelledby="collapse-1-button"
>
<p>Congue interdum nibh at lobortis habitasse vehicula facilisis auctor, convallis nascetur aenean mattis egestas neque ante molestie penatibus, cras efficitur cursus class magnis quam tempus.</p>
<p>
<a href="https://wolstenhol.me" target="_top">I am a link that should only be focusable when the element is open</a>.
</p>
<p>Vehicula cursus velit elit placerat porta urna sit risus etiam, ullamcorper turpis praesent non tempus volutpat mus. Blandit magna hac auctor nec aliquet consequat orci maximus, rhoncus litora ultricies taciti tempor class. A mus consectetur nisl tellus cras blandit quisque consequat, duis aliquet rutrum aptent fermentum metus ligula, sapien penatibus venenatis potenti nisi eu eros. Turpis quam vestibulum praesent imperdiet amet curae hendrerit semper, ullamcorper metus lacinia cras lacus senectus sed. Dictumst torquent integer interdum mus consectetur efficitur natoque ultricies nec sodales auctor suspendisse orci cubilia, parturient egestas ligula est metus non scelerisque netus lacus imperdiet blandit pellentesque inceptos. Duis ad justo gravida turpis quam dui consequat dictumst, metus curabitur nisl magna vestibulum ex tempus sollicitudin morbi, interdum montes lacus vel sit facilisis sociosqu.</p>
</section>
</div>
<p>Platea ligula justo auctor elit cubilia velit laoreet viverra, mollis feugiat amet dignissim phasellus aenean sed dui, tellus convallis at erat tortor donec efficitur. Litora aliquam habitasse ultrices dictumst torquent <a href="https://wolstenhol.me" target="_top">magna malesuada et urna magnis</a>, odio tristique praesent eu facilisi vel sed lacinia non, pulvinar tellus aliquet in ullamcorper consectetur lobortis metus imperdiet.</p>
</div>
.collapse__content {
// We set the max-height using the --collapse-height
// CSS custom property. If the custom property is
// missing then the fallback value of 0 is used.
// By toggling the value of the custom property with
// JavaScript we can animate the max-height.
max-height: var(--collapse-height, 0);
}
// Unrelated to component - just for presentation of example content.
a {
color: rgb(109, 40, 217);
text-decoration: underline;
&:hover,
&:focus {
color: rgb(91, 33, 182);
}
}
document.addEventListener('alpine:init', () => {
Alpine.data('collapse', () => ({
expanded: null,
init() {
const elem = this.$refs.content;
// We add the hidden attribute and CSS class via Alpine's init function
// so that content is not hidden if the JS fails to execute. The downside
// of this is a layout-shift issue and a flash of content appearing then
// disappearing as the page loads then the JS executes. The balance
// between progressive enhancement and layout-shift issues is tricky!
elem.hidden = true;
elem.classList.add('collapse__content');
},
toggle() {
const elem = this.$refs.content;
if (this.expanded) {
// Remove the --collapse-height custom property so the browser uses
// the fallback value of 0.
elem.style.removeProperty('--collapse-height');
elem.addEventListener(
'transitionend',
(e) => {
// We need to make sure the event hasn't come from a child element
// and bubbled up to our element.
if (e.target === elem) {
// Mark the element as hidden so its contents will be
// hidden from assistive tech like screen readers or
// keyboard navigation.
elem.hidden = true;
this.expanded = false;
}
},
{
once: true,
}
);
} else {
// Unhide our element so we can calculate its dimensions.
// It will still be visually hidden because of the maxHeight
// of 0.
elem.hidden = false;
// Set a --collapse-height property that matches the elements height.
// This will cause the browser to animate the opening of the
// element.
elem.style.setProperty('--collapse-height', `${elem.scrollHeight}px`);
elem.addEventListener(
'transitionend',
(e) => {
// We need to make sure the event hasn't come from a child element
// and bubbled up to our element.
if (e.target === elem) {
this.expanded = true;
}
},
{
once: true,
}
);
}
},
updateHeight() {
// A function to update the custom property on window resize, to avoid
// clipping content if the scrollHeight value of the element changes.
if (this.expanded) {
const elem = this.$refs['content'];
elem.style.setProperty('--collapse-height', `${elem.scrollHeight}px`);
}
},
}));
});
Also see: Tab Triggers