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>Accessible Accordion Widget</h1>
<ul>
<li>Use up and down cursor keys to navigate through the "accordion triggers".</li>
<li>Click or press Space or Enter to open/close an accordion.</li>
<li>Press Tab to jump to the open accordion panel.</li>
<li>Applies correct <code>aria</code> states and stateful classes - so you can style them how you prefer</li>
<li>Default open panels can be set with JS when instantiating, or by adding <code>.is-selected</code> to the accordion trigger</li>
<li>Supports an accordion of 1 item (useful for "show advanced")</li>
</ul>
<div role="tablist" aria-multiselectable="true" class="accordion-interface">
<h2>Multiple panels can be opened at once</h2>
<a href="#accordion1" role="tab" aria-controls="accordion1" class="accordion-trigger js-accordion-trigger">Accordion 1</a>
<div id="accordion1" role="tabpanel" class="accordion-panel js-accordion-panel">
<div class="accordion-panel__inner">
<h3>Accordion 1</h3>
<p>Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Vestibulum sit amet ipsum lacus. Suspendisse <em>potenti</em>. </p>
<p>Nulla lobortis tempus commodo. Fusce ac sodales magna. Cras molestie risus a <q cite='http://www.heydonworks.com'>enim convallis vitae</q> luctus libero lacinia. Donec tempus tempus tellus, ac lacinia turpis mattis ac. Maecenas sit <q cite='http://www.heydonworks.com'>amet tellus nec mi gravida posuere</q> non pretium magna. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Aliquam tincidunt velit sit amet ante hendrerit tempus. Sed mauris arcu, aliquet ultrices malesuada sed, pretium id <kbd>CTRL</kbd> + <kbd>V</kbd> massa. </p>
</div>
</div>
<a href="#accordion2" role="tab" aria-controls="accordion2" class="accordion-trigger js-accordion-trigger">Accordion 2</a>
<div id="accordion2" role="tabpanel" class="accordion-panel js-accordion-panel">
<div class="accordion-panel__inner">
<h3>Accordion 2</h3>
<p>Fusce ac sodales <abbr title='Cascading Style Sheets'>CSS</abbr> magna. Donec et nisi dictum felis sollicitudin congue. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
nibh, iaculis pretium sem orci aliquet mauris. Suspendisse <del>potenti</del>. Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Fusce ac sodales magna. Potenti et eros sed justo commodo bibendum
non at nunc. </p>
<p>Suspendisse potenti cras molestie, risus a enim convallis vitae luctus libero lacinia. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. Suspendisse potenti. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
nibh, <a href='#'>iaculis pretium</a> sem orci aliquet mauris. Donec a congue leo. Vestibulum sit amet ipsum lacus. </p>
</div>
</div>
<a href="#accordion3" role="tab" aria-controls="accordion3" class="accordion-trigger js-accordion-trigger">Accordion 3</a>
<div id="accordion3" role="tabpanel" class="accordion-panel js-accordion-panel">
<div class="accordion-panel__inner">
<h3>Accordion 3</h3>
<p>Potenti et eros sed justo commodo bibendum non at nunc. Maecenas sit amet tellus nec mi gravida posuere non pretium magna. Nulla auctor eleifend turpis consequat pharetra. </p>
<p>Vestibulum sit amet ipsum lacus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra nibh, iaculis pretium sem orci aliquet mauris. Duis sagittis, est sit amet gravida
tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. </p>
<p>Suspendisse potenti. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Suspendisse potenti H<sub>2</sub>0. </p>
</div>
</div>
</div>
<div role="tablist" class="accordion-interface">
<h2>Only one panel can be opened</h2>
<a href="#accordion1" role="tab" aria-controls="accordion1" class="accordion-trigger js-accordion-trigger">Accordion 1</a>
<div id="accordion1" role="tabpanel" class="accordion-panel js-accordion-panel">
<div class="accordion-panel__inner">
<h3>Accordion 1</h3>
<p>Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Vestibulum sit amet ipsum lacus. Suspendisse <em>potenti</em>. </p>
<p>Nulla lobortis tempus commodo. Fusce ac sodales magna. Cras molestie risus a <q cite='http://www.heydonworks.com'>enim convallis vitae</q> luctus libero lacinia. Donec tempus tempus tellus, ac lacinia turpis mattis ac. Maecenas sit <q cite='http://www.heydonworks.com'>amet tellus nec mi gravida posuere</q> non pretium magna. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Aliquam tincidunt velit sit amet ante hendrerit tempus. Sed mauris arcu, aliquet ultrices malesuada sed, pretium id <kbd>CTRL</kbd> + <kbd>V</kbd> massa. </p>
</div>
</div>
<a href="#accordion2" role="tab" aria-controls="accordion2" class="accordion-trigger js-accordion-trigger">Accordion 2</a>
<div id="accordion2" role="tabpanel" class="accordion-panel js-accordion-panel">
<div class="accordion-panel__inner">
<h3>Accordion 2</h3>
<p>Fusce ac sodales <abbr title='Cascading Style Sheets'>CSS</abbr> magna. Donec et nisi dictum felis sollicitudin congue. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
nibh, iaculis pretium sem orci aliquet mauris. Suspendisse <del>potenti</del>. Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Fusce ac sodales magna. Potenti et eros sed justo commodo bibendum
non at nunc. </p>
<p>Suspendisse potenti cras molestie, risus a enim convallis vitae luctus libero lacinia. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. Suspendisse potenti. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra
nibh, <a href='#'>iaculis pretium</a> sem orci aliquet mauris. Donec a congue leo. Vestibulum sit amet ipsum lacus. </p>
</div>
</div>
<a href="#accordion3" role="tab" aria-controls="accordion3" class="accordion-trigger js-accordion-trigger">Accordion 3</a>
<div id="accordion3" role="tabpanel" class="accordion-panel js-accordion-panel">
<div class="accordion-panel__inner">
<h3>Accordion 3</h3>
<p>Potenti et eros sed justo commodo bibendum non at nunc. Maecenas sit amet tellus nec mi gravida posuere non pretium magna. Nulla auctor eleifend turpis consequat pharetra. </p>
<p>Vestibulum sit amet ipsum lacus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Sed dapibus, lectus sit amet adipiscing egestas, mauris est viverra nibh, iaculis pretium sem orci aliquet mauris. Duis sagittis, est sit amet gravida
tristique, purus lectus venenatis urna, id molestie magna risus ut nunc. Nulla vel magna sit amet dui <a href='#'>lobortis</a> commodo vitae vel nulla. </p>
<p>Suspendisse potenti. Aliquam nisl enim, tristique tempus placerat at, posuere in lectus. Lorem ipsum dolor sit amet, consectetur <em>adipiscing elit</em>. Suspendisse potenti H<sub>2</sub>0. </p>
</div>
</div>
</div>
.accordion-trigger {
background: lightgrey;
border: 1px solid;
color: #000;
display: block;
font-weight: bold;
padding: 10px;
text-decoration: none;
&.is-selected {
background: lightblue;
}
&:before {
// Don't use generated content for the indicator in production as it gets read to screenreaders
content: "+";
display: inline-block;
width: 15px;
}
&.is-selected:before {
content: "–";
}
}
.accordion-panel {
max-height: 1000px;
opacity: 1;
transform: scaleY(1);
transform-origin: top;
transition: transform ease 0.5s, opacity ease 0.5s, max-height ease 0.75s;
visibility: visible;
&.is-hidden {
max-height: 0;
opacity: 0;
overflow: hidden;
transform: scaleY(0);
transition: all ease 0.5s;
visibility: hidden;
}
}
.accordion-panel__inner {
border: 1px solid;
border-top: none;
padding: 10px;
}
body {
font-family: sans-serif;
margin: auto;
padding: 30px;
max-width: 960px;
overflow-y: scroll;
}
li {
margin: 0.25em 0;
}
h1, h2, h3 {
margin-top: 0;
}
h1 {
font-size: (25 / 16) * 1em;
}
h2 {
font-size: (22 / 16) * 1em;
}
code {
font-size: (20 / 16) * 1em;
}
.accordion-interface {
float: left;
margin: 0 2% 30px;
width: 46%
}
:focus {
box-shadow: 0 0 4px dodgerblue;
outline: none;
}
function AccordionWidget (el, selectedIndex) {
if (!el) {
return;
}
this.el = el;
this.accordionTriggers = this.el.getElementsByClassName('js-accordion-trigger');
this.accordionPanels = this.el.getElementsByClassName('js-accordion-panel');
if (this.accordionTriggers.length === 0 || this.accordionTriggers.length !== this.accordionPanels.length) {
return;
}
this._init(selectedIndex);
}
AccordionWidget.prototype._init = function (selectedIndex) {
this.accordionTriggersLength = this.accordionTriggers.length;
this.expandedAccordions = new Array(this.accordionTriggersLength);
this.multiSelectable = this.el.hasAttribute('aria-multiselectable');
this.clickListener = this._clickEvent.bind(this);
this.keydownListener = this._keydownEvent.bind(this);
this.focusListener = this._focusEvent.bind(this);
this.keys = {
prev: 38,
next: 40,
space: 32
};
var initialSelectedIndex;
for (var i = 0; i < this.accordionTriggersLength; i++) {
this.accordionTriggers[i].index = i;
this.accordionTriggers[i].addEventListener('click', this.clickListener, false);
this.accordionTriggers[i].addEventListener('keydown', this.keydownListener, false);
this.accordionTriggers[i].addEventListener('focus', this.focusListener, false);
if (this.accordionTriggers[i].classList.contains('is-selected')) {
this.expandedAccordions[i] = true;
}
}
if (!isNaN(selectedIndex)) {
initialSelectedIndex = selectedIndex < this.accordionTriggersLength ? selectedIndex : this.accordionTriggersLength - 1;
this.expandedAccordions = new Array(this.accordionTriggersLength);
this.expandedAccordions[initialSelectedIndex] = true;
}
else {
initialSelectedIndex = this.expandedAccordions.lastIndexOf(true);
if (!this.multiSelectable) {
this.expandedAccordions = new Array(this.accordionTriggersLength);
this.expandedAccordions[initialSelectedIndex] = true;
}
}
this.setSelected(initialSelectedIndex);
this.setExpanded();
this.el.classList.add('is-initialized');
};
AccordionWidget.prototype._clickEvent = function (e) {
e.preventDefault();
this.setSelected(e.target.index, true);
this.setExpanded(e.target.index, true);
};
AccordionWidget.prototype._keydownEvent = function (e) {
var targetIndex;
switch (e.keyCode) {
case this.keys.space:
case this.keys.prev:
case this.keys.next:
e.preventDefault();
break;
default:
return;
}
if (e.keyCode === this.keys.space) {
this.setExpanded(e.target.index, true);
return;
}
else if (e.keyCode === this.keys.prev && e.target.index > 0) {
targetIndex = e.target.index - 1;
}
else if (e.keyCode === this.keys.next && e.target.index < this.accordionTriggersLength - 1) {
targetIndex = e.target.index + 1;
}
else {
return;
}
this.setSelected(targetIndex, true);
};
AccordionWidget.prototype._focusEvent = function () {
if (this.accordionTriggersLength === 1) {
this.setSelected(0);
}
return;
};
AccordionWidget.prototype.setSelected = function (index, userInvoked) {
if (index === -1) {
this.accordionTriggers[0].setAttribute('tabindex', 0);
return;
}
for (var i = 0; i < this.accordionTriggersLength; i++) {
if (i === index) {
this.accordionTriggers[i].classList.add('is-selected');
this.accordionTriggers[i].setAttribute('aria-selected', true);
this.accordionTriggers[i].setAttribute('tabindex', 0);
if (userInvoked) {
this.accordionTriggers[i].focus();
}
}
else {
this.accordionTriggers[i].classList.remove('is-selected');
this.accordionTriggers[i].setAttribute('aria-selected', false);
this.accordionTriggers[i].setAttribute('tabindex', -1);
}
}
};
AccordionWidget.prototype.setExpanded = function (index, userInvoked) {
var i;
if (userInvoked) {
if (this.multiSelectable) {
this.expandedAccordions[index] = !this.expandedAccordions[index];
}
else {
for (i = 0; i < this.accordionTriggersLength; i++) {
if (i === index) {
this.expandedAccordions[i] = !this.expandedAccordions[i];
}
else {
this.expandedAccordions[i] = false;
}
}
}
}
for (i = 0; i < this.accordionTriggersLength; i++) {
if (this.expandedAccordions[i]) {
this.accordionTriggers[i].setAttribute('aria-expanded', true);
this.accordionTriggers[i].classList.add('is-expanded');
this.accordionPanels[i].setAttribute('aria-hidden', false);
this.accordionPanels[i].classList.remove('is-hidden');
this.accordionPanels[i].setAttribute('tabindex', 0);
}
else {
this.accordionTriggers[i].setAttribute('aria-expanded', false);
this.accordionTriggers[i].classList.remove('is-expanded');
this.accordionPanels[i].setAttribute('aria-hidden', true);
this.accordionPanels[i].classList.add('is-hidden');
this.accordionPanels[i].setAttribute('tabindex', -1);
}
}
};
AccordionWidget.prototype.destroy = function () {
this.el.classList.remove('is-initialized');
for (var i = 0; i < this.accordionTriggersLength; i++) {
this.accordionTriggers[i].removeAttribute('aria-expanded');
this.accordionTriggers[i].removeAttribute('aria-selected');
this.accordionTriggers[i].classList.remove('is-expanded');
this.accordionPanels[i].removeAttribute('aria-hidden');
this.accordionPanels[i].classList.remove('is-hidden');
this.accordionPanels[i].removeAttribute('tabindex');
this.accordionTriggers[i].removeEventListener('click', this.clickListener, false);
this.accordionTriggers[i].removeEventListener('keydown', this.keydownListener, false);
this.accordionTriggers[i].removeEventListener('focus', this.focusListener, false);
delete this.accordionTriggers[i].index;
}
};
new AccordionWidget(document.getElementsByClassName('accordion-interface')[0], 0);
new AccordionWidget(document.getElementsByClassName('accordion-interface')[1], 1);
Also see: Tab Triggers