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>🛠 Simple CSS+JS dynamic <code>max-height</code> accordions!</h1>
<h2>⚠️ The problem with <code>max-height</code> accordions</h2>
<p>I love the technique of using <code>max-height</code> to create accordions. <strong>Buuuuut</strong>, a big caveat is you can't transition between a <code>max-height</code> of <code>0</code> and <code>auto</code>. Leaving you to either:</p>
<ol>
<li>Use something like <code>max-height: 999em</code> to account for the tallest content length possible. This causes your opening and closing animations to be too quick.</li>
<li>Use a smaller value for <code>max-height</code>, but what if your content is too large and it "cuts off" part of it?</li>
</ol>
<h2>💪 transitionend to the rescue!</h2>
<p>You can add a <code>transitionend</code> eventListener to the accordion body (part that opens and closes) to dynamically calculate the <code>max-height</code> value! This event fires after a CSS transition completes. Meaning:</p>
<ul>
<li>When the accordion is closed the content is positioned absolute so we can hide the content, but still get the <code>scrollHeight</code> value</li>
<li>When clicking a closed accordion we, store the <code>scrollHeight</code> in a variable and briefly set the <code>max-height</code> to <code>0</code>. We then immediately set the <code>max-height</code> to the value of the <code>scrollHeight</code> on the absolutely positioned element.</li>
<li>Once the transition is complete we remove the <code>max-height</code> so the content isn't capped at this value. Otherwise, if you resize your browser you might have overflow issues.</li>
<li>When a user closes the accordion we immediately capture the height again, set the <code>max-height</code> to this value briefly and finally change the <code>max-height</code> back to <code>0</code> to close the accordion seamlessly.</li>
</ul>
<h2>🎉 Demo</h2>
<div class="Accordion" data-accordion>
<div class="Accordion__title" data-title>
<h3>Example Heading</h3>
</div>
<div class="Accordion__content" data-content>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aperiam tempora voluptatum eligendi rem molestias numquam totam dignissimos animi ullam a culpa modi soluta, vero quisquam vel similique quibusdam neque nam?</p>
<p>Ipsam dolorem ut similique ullam architecto eum corrupti dolor laboriosam neque error iure aliquam, inventore esse nemo, nisi laborum eaque, est officiis rem id. Doloremque velit excepturi eos tempore neque?</p>
<p>Pariatur eius harum, quidem cumque corporis ipsa odio quo at, fugit atque inventore sit accusamus dolor unde? Porro ab nihil maxime sunt quasi? Asperiores animi, consectetur provident a aspernatur totam.</p>
<p>Corrupti, voluptatem! Rem eveniet labore itaque velit amet vel sapiente qui harum quaerat aliquam veniam molestiae, corporis optio molestias magnam, nihil unde! Tempore voluptatem, quaerat sit neque repellat dignissimos earum?</p>
</div>
</div>
.Accordion {
border: 1px solid #ccc;
border-radius: 5px;
padding: 1rem;
position: relative;
&__title {
cursor: pointer;
user-select: none;
}
&__title h3 {
margin: 0;
}
&__content {
overflow: hidden;
transition: .2s max-height;
will-change: max-height;
}
&.is-hidden &__content {
position: absolute;
opacity: 0;
visibility: hidden;
}
}
/*
* Demo stuff
*/
body {
padding: 2rem;
line-height: 1.5;
}
code {
background: #4A484C;
color: #fff;
border-radius: 5px;
padding: 0 8px;
}
class Accordion {
constructor($el) {
this.$el = $el;
this.$title = this.$el.querySelector('[data-title]');
this.$content = this.$el.querySelector('[data-content]');
this.isOpen = false;
this.height = 0;
this.events();
this.close();
}
events() {
this.$title.addEventListener('click', this.handleClick.bind(this));
this.$content.addEventListener('transitionend', this.handleTransition.bind(this));
}
handleClick() {
this.height = this.$content.scrollHeight;
if (this.isOpen) {
this.close();
} else {
this.open();
}
}
close() {
this.isOpen = false;
this.$el.classList.remove('is-active');
this.$content.style.maxHeight = `${this.height}px`;
setTimeout(() => {
this.$content.style.maxHeight = `${0}px`;
}, 1);
}
open() {
this.isOpen = true;
this.$el.classList.add('is-active');
this.$el.classList.remove('is-hidden');
this.$content.style.maxHeight = `${0}px`;
setTimeout(() => {
this.$content.style.maxHeight = `${this.height}px`;
}, 1);
}
handleTransition() {
if (!this.isOpen) {
this.$el.classList.add('is-hidden');
}
this.$content.style.maxHeight = '';
}
}
/*
* Instantiate a new Accordion
*/
new Accordion(document.querySelector('[data-accordion]'));
Also see: Tab Triggers