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.
<header class="sample-header">
<div class="title" id="id_website_title">Mythical University</div>
<div class="tagline">Using a disclosure + link pattern for navigation</div>
</header>
<nav aria-label="Mythical University">
<ul id="exTest" class="disclosure-nav">
<li>
<a href="#mythical-page-content" class="main-link">About</a>
<button type="button" aria-expanded="false" aria-controls="id_about_menu" aria-label="More About pages"> </button>
<ul id="id_about_menu" style="display:none">
<li>
<a href="#mythical-page-content">Overview</a>
</li>
<li>
<a href="#mythical-page-content">Administration</a>
</li>
<li>
<a href="#mythical-page-content">Facts</a>
</li>
<li>
<a href="#mythical-page-content">Campus Tours</a>
</li>
</ul>
</li>
<li>
<a href="#mythical-page-content" class="main-link">Admissions</a>
<button type="button" aria-expanded="false" aria-controls="id_admissions_menu" aria-label="More Admissions pages"> </button>
<ul id="id_admissions_menu" style="display:none">
<li>
<a href="#mythical-page-content">Apply</a>
</li>
<li>
<a href="#mythical-page-content">Tuition</a>
</li>
<li>
<a href="#mythical-page-content">Sign Up</a>
</li>
<li>
<a href="#mythical-page-content">Visit</a>
</li>
<li>
<a href="#mythical-page-content">Photo Tour</a>
</li>
<li>
<a href="#mythical-page-content">Connect</a>
</li>
</ul>
</li>
<li>
<a href="#mythical-page-content" class="main-link">Academics</a>
<button type="button" aria-expanded="false" aria-controls="id_academics_menu" aria-label="More Academics pages"> </button>
<ul id="id_academics_menu" style="display:none">
<li>
<a href="#mythical-page-content">Colleges & Schools</a>
</li>
<li>
<a href="#mythical-page-content">Programs of Study</a>
</li>
<li>
<a href="#mythical-page-content">Honors Programs</a>
</li>
<li>
<a href="#mythical-page-content">Online Courses</a>
</li>
<li>
<a href="#mythical-page-content">Course Explorer</a>
</li>
<li>
<a href="#mythical-page-content">Register for Class</a>
</li>
<li>
<a href="#mythical-page-content">Academic Calendar</a>
</li>
<li>
<a href="#mythical-page-content">Transcripts</a>
</li>
</ul>
</li>
</ul>
</nav>
<div id="mythical-page-content" class="disclosure-page-content" tabindex="-1" role="region" aria-labelledby="mythical-page-heading">
<h3 id="mythical-page-heading">Home</h3>
<p>Sample content section. Activating a link above will update and navigate to this region.</p>
</div>
<footer class="sample-footer">
Mythical University footer information
</footer>
.disclosure-nav {
background-color: #eee;
display: flex;
list-style-type: none;
padding: 0;
margin: 0;
}
.disclosure-nav ul {
background-color: #eee;
border: 1px solid #005a9c;
border-top-width: 5px;
border-radius: 0 0 4px 4px;
display: block;
list-style-type: none;
margin: 0;
min-width: 200px;
padding: 0;
position: absolute;
top: 100%;
}
.disclosure-nav li {
margin: 0;
}
.disclosure-nav > li {
display: flex;
position: relative;
}
.disclosure-nav ul a {
border: 0;
color: #000;
display: block;
margin: 0;
padding: 0.5em 1em;
text-decoration: underline;
}
.disclosure-nav ul a:hover,
.disclosure-nav ul a:focus {
background-color: #ddd;
margin-bottom: 0;
text-decoration: none;
}
.disclosure-nav ul a:focus {
outline: 5px solid rgb(0 90 156 / 75%);
position: relative;
}
.disclosure-nav button,
.disclosure-nav .main-link {
align-items: center;
background-color: transparent;
border: 1px solid transparent;
border-right-color: #ccc;
display: flex;
padding: 1em;
}
.disclosure-nav .main-link {
border-right-color: transparent;
}
.disclosure-nav button::after {
content: "";
border-bottom: 1px solid #000;
border-right: 1px solid #000;
height: 0.5em;
margin-left: 0.75em;
width: 0.5em;
transform: rotate(45deg);
}
.disclosure-nav .main-link + button::after {
margin-left: 0;
}
.disclosure-nav button:focus,
.disclosure-nav .main-link:focus {
border-color: #005a9c;
outline: 5px solid rgb(0 90 156 / 75%);
position: relative;
}
.disclosure-nav button:hover,
.disclosure-nav button[aria-expanded="true"] {
background-color: #005a9c;
color: #fff;
}
.disclosure-nav button:hover::after,
.disclosure-nav button[aria-expanded="true"]::after {
border-color: #fff;
}
/* Styles for example page content section */
.disclosure-page-content {
border: 1px solid #ccc;
padding: 1em;
}
.disclosure-page-content h3 {
margin-top: 0.5em;
}
.sample-header {
border: #005a9c solid 2px;
background: #005a9c;
color: white;
text-align: center;
}
.sample-header .title {
font-size: 2.5em;
font-weight: bold;
font-family: serif;
}
.sample-header .tagline {
font-style: italic;
}
.sample-footer {
border: #005a9c solid 2px;
background: #005a9c;
font-family: serif;
color: white;
font-style: italic;
padding-left: 1em;
}
/*
* This content is licensed according to the W3C Software License at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*
* Supplemental JS for the disclosure menu keyboard behavior
*/
'use strict';
class DisclosureNav {
constructor(domNode) {
this.rootNode = domNode;
this.controlledNodes = [];
this.openIndex = null;
this.useArrowKeys = true;
this.topLevelNodes = [
...this.rootNode.querySelectorAll(
'.main-link, button[aria-expanded][aria-controls]'
),
];
this.topLevelNodes.forEach((node) => {
// handle button + menu
if (
node.tagName.toLowerCase() === 'button' &&
node.hasAttribute('aria-controls')
) {
const menu = node.parentNode.querySelector('ul');
if (menu) {
// save ref controlled menu
this.controlledNodes.push(menu);
// collapse menus
node.setAttribute('aria-expanded', 'false');
this.toggleMenu(menu, false);
// attach event listeners
menu.addEventListener('keydown', this.onMenuKeyDown.bind(this));
node.addEventListener('click', this.onButtonClick.bind(this));
node.addEventListener('keydown', this.onButtonKeyDown.bind(this));
}
}
// handle links
else {
this.controlledNodes.push(null);
node.addEventListener('keydown', this.onLinkKeyDown.bind(this));
}
});
this.rootNode.addEventListener('focusout', this.onBlur.bind(this));
}
controlFocusByKey(keyboardEvent, nodeList, currentIndex) {
switch (keyboardEvent.key) {
case 'ArrowUp':
case 'ArrowLeft':
keyboardEvent.preventDefault();
if (currentIndex > -1) {
var prevIndex = Math.max(0, currentIndex - 1);
nodeList[prevIndex].focus();
}
break;
case 'ArrowDown':
case 'ArrowRight':
keyboardEvent.preventDefault();
if (currentIndex > -1) {
var nextIndex = Math.min(nodeList.length - 1, currentIndex + 1);
nodeList[nextIndex].focus();
}
break;
case 'Home':
keyboardEvent.preventDefault();
nodeList[0].focus();
break;
case 'End':
keyboardEvent.preventDefault();
nodeList[nodeList.length - 1].focus();
break;
}
}
// public function to close open menu
close() {
this.toggleExpand(this.openIndex, false);
}
onBlur(event) {
var menuContainsFocus = this.rootNode.contains(event.relatedTarget);
if (!menuContainsFocus && this.openIndex !== null) {
this.toggleExpand(this.openIndex, false);
}
}
onButtonClick(event) {
var button = event.target;
var buttonIndex = this.topLevelNodes.indexOf(button);
var buttonExpanded = button.getAttribute('aria-expanded') === 'true';
this.toggleExpand(buttonIndex, !buttonExpanded);
}
onButtonKeyDown(event) {
var targetButtonIndex = this.topLevelNodes.indexOf(document.activeElement);
// close on escape
if (event.key === 'Escape') {
this.toggleExpand(this.openIndex, false);
}
// move focus into the open menu if the current menu is open
else if (
this.useArrowKeys &&
this.openIndex === targetButtonIndex &&
event.key === 'ArrowDown'
) {
event.preventDefault();
this.controlledNodes[this.openIndex].querySelector('a').focus();
}
// handle arrow key navigation between top-level buttons, if set
else if (this.useArrowKeys) {
this.controlFocusByKey(event, this.topLevelNodes, targetButtonIndex);
}
}
onLinkKeyDown(event) {
var targetLinkIndex = this.topLevelNodes.indexOf(document.activeElement);
// handle arrow key navigation between top-level buttons, if set
if (this.useArrowKeys) {
this.controlFocusByKey(event, this.topLevelNodes, targetLinkIndex);
}
}
onMenuKeyDown(event) {
if (this.openIndex === null) {
return;
}
var menuLinks = Array.prototype.slice.call(
this.controlledNodes[this.openIndex].querySelectorAll('a')
);
var currentIndex = menuLinks.indexOf(document.activeElement);
// close on escape
if (event.key === 'Escape') {
this.topLevelNodes[this.openIndex].focus();
this.toggleExpand(this.openIndex, false);
}
// handle arrow key navigation within menu links, if set
else if (this.useArrowKeys) {
this.controlFocusByKey(event, menuLinks, currentIndex);
}
}
toggleExpand(index, expanded) {
// close open menu, if applicable
if (this.openIndex !== index) {
this.toggleExpand(this.openIndex, false);
}
// handle menu at called index
if (this.topLevelNodes[index]) {
this.openIndex = expanded ? index : null;
this.topLevelNodes[index].setAttribute('aria-expanded', expanded);
this.toggleMenu(this.controlledNodes[index], expanded);
}
}
toggleMenu(domNode, show) {
if (domNode) {
domNode.style.display = show ? 'block' : 'none';
}
}
updateKeyControls(useArrowKeys) {
this.useArrowKeys = useArrowKeys;
}
}
/* Initialize Disclosure Menus */
window.addEventListener(
'load',
function () {
var menus = document.querySelectorAll('.disclosure-nav');
var disclosureMenus = [];
for (var i = 0; i < menus.length; i++) {
disclosureMenus[i] = new DisclosureNav(menus[i]);
}
// listen to arrow key checkbox
var arrowKeySwitch = document.getElementById('arrow-behavior-switch');
if (arrowKeySwitch) {
arrowKeySwitch.addEventListener('change', function () {
var checked = arrowKeySwitch.checked;
for (var i = 0; i < disclosureMenus.length; i++) {
disclosureMenus[i].updateKeyControls(checked);
}
});
}
// fake link behavior
disclosureMenus.forEach((disclosureNav, i) => {
var links = menus[i].querySelectorAll('[href="#mythical-page-content"]');
var examplePageHeading = document.getElementById('mythical-page-heading');
for (var k = 0; k < links.length; k++) {
// The codepen export script updates the internal link href with a full URL
// we're just manually fixing that behavior here
links[k].href = '#mythical-page-content';
links[k].addEventListener('click', (event) => {
// change the heading text to fake a page change
var pageTitle = event.target.innerText;
examplePageHeading.innerText = pageTitle;
// handle aria-current
for (var n = 0; n < links.length; n++) {
links[n].removeAttribute('aria-current');
}
event.target.setAttribute('aria-current', 'page');
});
}
});
},
false
);
Also see: Tab Triggers