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.
Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.
Upgrade to PROIf 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.
Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.
Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:
Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.
This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.
Upgrade to PRO
<head>
<meta charset="utf-8">
<title>Accessible Multi-Level Dropdown Navigation</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Accessible Multi-Level Dropdown Navigation by <a href="http://www.ireaderinokun.com/" target="_blank">Ire Aderinokun</a></h1>
<nav id="nav">
<ul>
<!-- Menu tooggle on smaller screens -->
<li id="toggleMenu">
<a href="/sitemap.html">
<i class="fa fa-bars"></i> <i class="fa fa-times"></i>
<!-- As icons are being used to represent open/close,
provide hidden text for screen readers -->
<span id="toggleMenu-text">Toggle Navigation</span>
</a>
</li>
<!-- Simple menu item without sub menu -->
<li><a href="/">Level 1</a></li>
<!-- Menu item with one sub menu -->
<li>
<a href="/sitemap.html">Level 2</a>
<ul>
<li><a href="/">Example Link</a></li>
<li><a href="/">Example Link</a></li>
</ul>
</li>
<!-- Menu item with two levels of sub menu -->
<li>
<a href="/sitemap.html">Level 3</a>
<ul>
<li><a href="/">Example Link</a></li>
<li><a href="/">Example Link</a></li>
<li>
<a href="/">Example Link</a>
<ul>
<li><a href="/">Example Link</a></li>
<li><a href="/">Example Link</a></li>
<li><a href="/">Example Link</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="/">Level 1</a></li>
<li>
<a href="/sitemap.html">Level 3</a>
<ul>
<li><a href="/">Example Link</a></li>
<li>
<a href="/">Example Link</a>
<ul>
<li><a href="/">Example Link</a></li>
<li><a href="/">Example Link</a></li>
</ul>
</li>
<li>
<a href="/">Example Link</a>
<ul>
<li><a href="/">Example Link</a></li>
<li><a href="/">Example Link</a></li>
</ul>
</li>
<li><a href="/">Example Link</a></li>
</ul>
</li>
</ul>
</nav>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
html {
font-family: 'Proxima Nova', 'Helvetica Neue', 'Arial', sans-serif;
}
.container {
width: 90%;
margin: 10vh auto;
}
a {
color: inherit;
}
/*
* Accessible Multi-Level Dropdown Navigation Menu
* Created by Ire Aderinokun
*
*/
/* Resets */
#nav,
#nav ul,
#nav li {
margin: 0;
padding: 0;
border: 0;
list-style: none;
box-sizing: border-box;
}
#nav {
position: relative;
min-height: 60px;
max-width: 100%;
background-color: #ffdb3a;
color: #000;
border: 1px solid #D9BA31;
}
@media screen and (min-width: 650px) {
#nav {
display: inline-block;
}
}
@media screen and (max-width: 650px) {
#nav {
display: block;
}
}
#nav li {
position: relative;
}
#nav a {
text-decoration: none;
height: 100%;
display: block;
padding: 0 20px;
}
@media screen and (min-width: 650px) {
#nav a:focus {
outline: none;
}
}
.plusMark {
margin-left: 10px;
font-size: 20px;
font-weight: 700;
}
@media screen and (min-width: 650px) {
#nav li {
text-align: left;
width: 200px;
}
}
@media screen and (max-width: 650px) {
#nav li {
text-align: center;
width: 100%;
}
}
/* Any sub menu */
@media screen and (min-width: 650px) {
a + ul {
position: absolute;
}
a + ul:not(.js-showElement) {
display: none;
}
}
@media screen and (max-width: 650px) {
a + ul {
position: relative;
}
a + ul:not(.js-hideElement) {
display: block;
}
}
/* The Main Navigation Bar - Navigation Level One */
#nav > ul,
.fa {
height: 100%;
line-height: 60px;
}
#nav > ul > li {
position: relative;
text-align: center;
}
@media screen and (min-width: 650px) {
#nav > ul > li {
float: left;
width: auto;
}
}
@media screen and (max-width: 650px) {
#nav > ul > li {
float: none;
display: block;
width: 100%;
}
}
#nav > ul > li > a {
background-color: #ffdb3a;
}
#nav > ul > li > a:hover,
#nav > ul > li > a:focus,
#nav > ul > li > a.js-openSubMenu {
background-color: #D9BA31;
}
@media screen and (min-width: 650px) {
#nav > ul > li: not(: last-child) {
border-right: 1px solid #D9BA31;
border-bottom: none;
}
}
@media screen and (max-width: 650px) {
#nav > ul > li: not(: last-child) {
border-right: none;
}
#nav > ul > li:not(:last-child):not(:first-child) {
border-bottom: 1px solid #D9BA31;
}
}
#nav > ul > li:not(#toggleMenu):not(.js-showElement) {
/* first level nav li except toggleMenu icon */
}
@media screen and (min-width: 650px) {
#nav > ul > li: not(#toggleMenu): not(.js-showElement) {
display: inline-block;
}
}
@media screen and (max-width: 650px) {
#nav > ul > li: not(#toggleMenu): not(.js-showElement) {
display: none;
}
}
@media screen and (min-width: 650px) {
#nav #toggleMenu {
display: none;
}
}
@media screen and (max-width: 650px) {
#nav #toggleMenu {
display: block;
width: 100%;
}
#nav #toggleMenu.js-open {
border-bottom: 1px solid #D9BA31;
}
#nav #toggleMenu.js-open .fa-times {
display: block;
}
#nav #toggleMenu.js-open .fa-bars {
display: none;
}
#nav #toggleMenu.js-open a {
background-color: #D9BA31;
}
#nav #toggleMenu:not(.js-open) .fa-times {
display: none;
}
#nav #toggleMenu:not(.js-open) .fa-bars {
display: block;
}
}
span#toggleMenu-text {
position: absolute;
opacity: 0;
}
/* Second Level Dropdown */
#nav > ul > li > ul {
background-color: #D9BA31;
}
@media screen and (min-width: 650px) {
#nav > ul > li > ul {
top: 60px;
left: 0;
}
}
@media screen and (max-width: 650px) {
#nav > ul > li > ul {
width: 100%;
position: relative;
}
#nav > ul > li > ul:not(.js-showElement) {
display: none;
}
}
#nav > ul > li > ul > li > a {
background-color: #D9BA31;
}
#nav > ul > li > ul > li > a:hover,
#nav > ul > li > ul > li > a:focus {
background-color: #ffdb3a;
}
#nav > ul > li > ul > li:not(:last-child) a {
border-bottom: 1px solid #ffdb3a;
}
/* Third Level Dropdown */
@media screen and (min-width: 650px) {
#nav > ul > li > ul > li > ul {
top: 0;
left: 200px;
/* width of ul */
}
}
@media screen and (max-width: 650px) {
#nav > ul > li > ul > li > ul {
width: 100%;
position: relative;
}
#nav > ul > li > ul > li > ul:not(.js-showElement) {
display: none;
}
}
#nav > ul > li > ul > li > ul > li > a {
background-color: #ffdb3a;
}
#nav > ul > li > ul > li > ul > li > a:hover,
#nav > ul > li > ul > li > ul > li > a:focus {
background-color: #D9BA31;
}
#nav > ul > li > ul > li > ul > li:not(:last-child) > a {
border-bottom: 1px solid #D9BA31;
}
/* Javascript classes */
#nav .js-hideElement {
display: none;
}
#nav .js-showElement {
display: block;
}
/* Fallback for users without javascript */
html.no-js li:hover > a + ul,
html.no-js li:focus > a + ul {
display: block;
}
@media screen and (max-width: 650px) {
html.no-js #nav: hover > ul > li: not(#toggleMenu), html.no-js #nav: focus > ul > li: not(#toggleMenu) {
display: block;
}
html.no-js #nav:hover li:hover > a + ul,
html.no-js #nav:hover li:focus > a + ul,
html.no-js #nav:focus li:hover > a + ul,
html.no-js #nav:focus li:focus > a + ul {
display: block;
}
}
$(document).ready(function() {
// Remove no-js class
$('html').removeClass('no-js');
$('#toggleMenu').on('click', function() {
if ($(this).hasClass('js-open')) {
$('#nav > ul > li:not(#toggleMenu)').removeClass('js-showElement');
$(this).removeClass('js-open');
} else {
$('#nav > ul > li:not(#toggleMenu)').addClass('js-showElement');
$(this).addClass('js-open');
}
return false;
})
// Add plus mark to li that have a sub menu
$('li:has("ul") > a').append('<span class="plusMark">+</span>');
// sub menu
// ------------------------
// When interacting with a li that has a sub menu
$('li:has("ul")').on('mouseover keyup click mouseleave', function(e) {
// If either -
// tabbing into the li that has a sub menu
// hovering over the li that has a sub menu
if (e.keyCode === 9 | e.type === 'mouseover') {
// Show sub menu
$(this).children('ul').removeClass('js-hideElement');
$(this).children('ul').addClass('js-showElement');
}
// If mouse leaves li that has sub menu
if (e.type === 'mouseleave') {
// hide sub menu
$(this).children('ul').removeClass('js-showElement');
$(this).children('ul').addClass('js-hideElement');
}
// If clicking on li that has a sub menu
if (e.type === 'click') {
// If sub menu is already open
if ($(this).children('a').hasClass('js-openSubMenu')) {
// remove Open class
$(this).children('a').removeClass('js-openSubMenu');
// Hide sub menu
$(this).children('ul').removeClass('js-showElement');
$(this).children('ul').addClass('js-hideElement');
// If sub menu is closed
} else {
// add Open class
$(this).children('a').addClass('js-openSubMenu');
// Show sub menu
$(this).children('ul').removeClass('js-hideElement');
$(this).children('ul').addClass('js-showElement');
}
return false;
} // end click event
});
// Tabbing through Levels of sub menu
// ------------------------
// If key is pressed while on the last link in a sub menu
$('li > ul > li:last-child > a').on('keydown', function(e) {
// If tabbing out of the last link in a sub menu AND not tabbing into another sub menu
if ((e.keyCode == 9) && $(this).parent('li').children('ul').length == 0) {
// Close this sub menu
$(this).parent('li').parent('ul').removeClass('js-showElement');
$(this).parent('li').parent('ul').addClass('js-hideElement');
// If tabbing out of a third level sub menu and there are no other links in the parent (level 2) sub menu
if ($(this).parent('li').parent('ul').parent('li').parent('ul').parent('li').children('ul').length > 0 && $(this).parent('li').parent('ul').parent('li').is(':last-child')) {
// Close the parent sub menu (level 2) as well
$(this).parent('li').parent('ul').parent('li').parent('ul').removeClass('js-showElement');
$(this).parent('li').parent('ul').parent('li').parent('ul').addClass('js-hideElement');
}
}
})
})
<u>
nited than <div>
ided.
Also see: Tab Triggers