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 URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<body>
<div class="container">
<h1>Toggle & Accordions with ARIA sausage <span>by <a href="https://twitter.com/williamrembert">@williamrembert</a></span></h1>
<p>While working on a project, tried to update the way of coding a Toggle panel and an accordion. Inspired by this excellent tutorial in French from Accede Web initiative <a href="http://wiki.accede-web.com/notices/interfaces-riches-javascript/accordeons-plier-deplier" lang="fr">Accordéons (plier/déplier)</a> by <a href="https://twitter.com/societe_atalan">@societe_atalan</a>, but need some improvements for my project. Here is the compiled result.</p>
<h2>The challenge</h2>
<ul>
<li>Try to respect the <abbr>WAI-ARIA</abbr> rules : <a href="http://www.w3.org/TR/2013/WD-wai-aria-practices-20130307/#aria_ex"><abbr>WAI</abbr>-<abbr>ARIA</abbr> Design patterns</a>,</li>
<li>if <abbr>JS</abbr> is disabled, content must be readable and unstyled to fit the design rules,</li>
<li>make it independant of the markup. Should work with any title level,</li>
<li>easy navigation with keyboard,</li>
<li>inform users and screen readers of what happening,</li>
<li>try to make it easy to setup, activation by class only,</li>
<li>make it smooth and beautifull,</li>
<li>share it !</li>
</ul>
<h2>Accordion demo</h2>
<p class="intro">For my example, will use <a href="http://www.un.org/en/documents/udhr/">The universal declaration of human rights</a> ! Though it could be usefull to remind it. Accordion must to close the active panels when opening a new one.</p>
<div class="accordion">
<h3 class="panel-title">Article 1</h3>
<div class="panel-content">
<p>All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.</p>
</div>
<h3 class="panel-title">Article 2</h3>
<div class="panel-content">
<p>Everyone is entitled to all the rights and freedoms set forth in this Declaration, without distinction of any kind, such as race, colour, sex, language, religion, political or other opinion, national or social origin, property, birth or other status. Furthermore, no distinction shall be made on the basis of the political, jurisdictional or international status of the country or territory to which a person belongs, whether it be independent, trust, non-self-governing or under any other limitation of sovereignty. <a href="#">A link to test the focus order</a></p>
</div>
</div>
<p>Try with a second accordion to prevent conflict, can be setup in the JS see Options on line 42 & 46</p>
<div class="accordion">
<h3 class="panel-title">Article 3 & 4</h3>
<div class="panel-content">
<p>Everyone has the right to life, liberty and security of person.</p>
<p>No one shall be held in slavery or servitude; slavery and the slave trade shall be prohibited in all their forms.</p>
</div>
<h3 class="panel-title">Article 5</h3>
<div class="panel-content">
<p>No one shall be subjected to torture or to cruel, inhuman or degrading treatment or punishment.</p>
</div>
</div>
<h2>Toggles demo</h2>
<p class="intro">Difference between Accordion is that opening a panel won't close the others</p>
<h3 class="panel-title">Article 6</h3>
<div class="panel-content">
<p>Everyone has the right to recognition everywhere as a person before the law.</p>
</div>
<h3 class="panel-title">Article 7</h3>
<div class="panel-content">
<p>All are equal before the law and are entitled without any discrimination to equal protection of the law. All are entitled to equal protection against any discrimination in violation of this Declaration and against any incitement to such discrimination. <a href="#">A link to test the focus order</a></p>
</div>
<h2>How to use ?</h2>
<h3>Setup an accordion</h3>
<ol>
<li>Copy the content of <abbr>JS</abbr> Panel in your script.js files</li>
<li>Copy the content of <abbr>CSS</abbr> Panel in your styles.css. You'll need only since line 62.</li>
<li>Wrap the content you want to affect with a container with class 'accordion'</li>
<li>Add class panel-title to each panel title. Obvious no ?</li>
<li>Wrap you panel content in a container with class 'panel-content'</li>
</ol>
<h3>Setup a toggle</h3>
<ol>
<li>Copy the content of <abbr>JS</abbr> Panel in your script.js files</li>
<li>Copy the content of <abbr>CSS</abbr> Panel in your styles.css. You'll need only since line 62.</li>
<li>Add class panel-title to each panel title. Obvious no ?</li>
<li>Wrap you panel content in a container with class 'panel-content'</li>
</ol>
</div>
</body>
// Styles for demo purpose
@import url(https://fonts.googleapis.com/css?family=Roboto:400,500,700,300);
@bgcolor: #607d8b;
@bordercolor: #ececec;
@txtcolor: #414141;
@headingscolor: #191919;
@linkcolor: #607d8b;
@interactioncolor: #ec407a;
body {
background-color: @bgcolor;
color: @txtcolor;
font-family: "roboto", arial, helvetica, sans-serif;
font-size:100%;
line-height: 1.45;
font-weight: 400;
}
.container {
background-color: white;
margin: 3em auto;
padding: 2em 3em 3em 4em;
width: 70%;
}
h1, h2, h3 {
color: @headingscolor;
}
h1 {
border-bottom: 1px solid @bordercolor;
font-weight: 300;
margin-bottom: 2em;
padding-bottom: 1em;
text-align:center;
span {
display: block;
font-size: .5em;
}
}
a {
border-bottom: 1px solid @interactioncolor;
color: @linkcolor;
text-decoration:none;
&:hover, &:focus {
color: @interactioncolor;
border-bottom: none;
}
}
h3 {
font-weight: 500;
}
.intro {
font-size: 1.25em;
}
// Start styling ou panels
.js .panel-title {
margin: 0;
}
.panel-title a {
border-bottom: none;
color: #292929;
display: block;
padding: 1.25em 0;
position: relative;
text-decoration: none;
transition: color 200ms ease 0s;
width: 100%;
.icon {
color: #9e9e9e;
position:absolute;
right: 0;
transition: all 200ms ease 0s;
}
&:hover, &:focus {
color: #37474f;
.icon {
color: #ec407a;
}
}
&.active {
color: #37474f;
.icon {
color: #ec407a;
transform: rotate(45deg);
}
}
}
.js .accordion {
border-bottom: 1px solid #ececec;
margin: 2em 0;
}
.accordion .panel-title a {
border-top: 1px solid #ececec;
}
[id^="panel-"] {
padding-bottom: 2em;
}
// Hiding the panel content. If JS is inactive, content will be displayed
$( '.panel-content' ).hide();
// Preparing the DOM
// -- Update the markup of accordion container
$( '.accordion' ).attr({
role: 'tablist',
multiselectable: 'true'
});
// -- Adding ID, aria-labelled-by, role and aria-labelledby attributes to panel content
$( '.panel-content' ).attr( 'id', function( IDcount ) {
return 'panel-' + IDcount;
});
$( '.panel-content' ).attr( 'aria-labelledby', function( IDcount ) {
return 'control-panel-' + IDcount;
});
$( '.panel-content' ).attr( 'aria-hidden' , 'true' );
// ---- Only for accordion, add role tabpanel
$( '.accordion .panel-content' ).attr( 'role' , 'tabpanel' );
// -- Wrapping panel title content with a <a href="">
$( '.panel-title' ).each(function(i){
// ---- Need to identify the target, easy it's the immediate brother
$target = $(this).next( '.panel-content' )[0].id;
// ---- Creating the link with aria and link it to the panel content
$link = $( '<a>', {
'href': '#' + $target,
'aria-expanded': 'false',
'aria-controls': $target,
'id' : 'control-' + $target
});
// ---- Output the link
$(this).wrapInner($link);
});
// Optional : include an icon. Better in JS because without JS it have non-sense.
$( '.panel-title a' ).append('<span class="icon">+</span>');
// Now we can play with it
$( '.panel-title a' ).click(function() {
if ($(this).attr( 'aria-expanded' ) == 'false'){ //If aria expanded is false then it's not opened and we want it opened !
// -- Only for accordion effect (2 options) : comment or uncomment the one you want
// ---- Option 1 : close only opened panel in the same accordion
// search through the current Accordion container for opened panel and close it, remove class and change aria expanded value
$(this).parents( '.accordion' ).find( '[aria-expanded=true]' ).attr( 'aria-expanded' , false ).removeClass( 'active' ).parent().next( '.panel-content' ).slideUp(200).attr( 'aria-hidden' , 'true');
// Option 2 : close all opened panels in all accordion container
//$('.accordion .panel-title > a').attr('aria-expanded', false).removeClass('active').parent().next('.panel-content').slideUp(200);
// Finally we open the panel, set class active for styling purpos on a and aria-expanded to "true"
$(this).attr( 'aria-expanded' , true ).addClass( 'active' ).parent().next( '.panel-content' ).slideDown(200).attr( 'aria-hidden' , 'false');
} else { // The current panel is opened and we want to close it
$(this).attr( 'aria-expanded' , false ).removeClass( 'active' ).parent().next( '.panel-content' ).slideUp(200).attr( 'aria-hidden' , 'true');;
}
// No Boing Boing
return false;
});
Also see: Tab Triggers