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.
<nav class="focus-within-variant">
<ul>
<li><a href="">Vorspeise</a></li>
<li>
<a href="">Hauptgericht</a>
<ul>
<li><a href="">Hauptgericht mit Fleisch</a></li>
<li><a href="">Hauptgericht vegetarisch</a></li>
</ul>
</li>
<li><a href="">Dessert</a></li>
</ul>
</nav>
<p>Dieses Menü wurde ohne JavaScript zubereitet. Als Zutat wurde <code>:focus-within</code> verwendet; Gäste mit <s>Edge oder gar</s> Internet Explorer schauen also in die Röhre.</p>
<p>Das Menü ist für JS-Allergiker geeignet; es schmeckt bloß nicht. Vom Hauptgericht kommt man nicht zum Dessert, sondern man muss das Untermenü über sich ergehen lassen.</p>
<p>Das Menü ist nicht für kleine Tische geeignet. Wenn es nicht in eine Zeile passt, kommt man vom Hauptgericht nicht ins Untermenü.</p>
<nav class="focus-within-variant">
<ul>
<li><a href="">Vorspeise</a></li>
<li tabindex="0">
Hauptgericht
<ul>
<li><a href="">Hauptgericht mit Fleisch</a></li>
<li><a href="">Hauptgericht vegetarisch</a></li>
</ul>
</li>
<li><a href="">Dessert</a></li>
</ul>
</nav>
<p>Im Unterschied zu oben ist das Hauptgericht hier kein Link. Damit das Menü tastaturbedienbar ist, trägt das <code>li</code>-Element das Attribut <code>tabindex="0"</code>. Ansonsten gilt das oben Gesagte.</p>
<p>Mit JavaScript könnte man noch dafür sorgen, dass beim Aufklappen des Untermenüs der erste Untermenüpunkt den Fokus erhält. Aber wenn schon JavaScript, dann kann man’s auch gleich richtig machen …</p>
<nav class="button-variant">
<ul>
<li><a href="">Vorspeise</a></li>
<li>
<button aria-expanded="false">Hauptgericht</button>
<ul>
<li><a href="">Hauptgericht mit Fleisch</a></li>
<li><a href="">Hauptgericht vegetarisch</a></li>
</ul>
</li>
<li><a href="">Dessert</a></li>
</ul>
</nav>
<p>Das Hauptgericht ist hier ein Button (<code><button></code>), der per JavaScript die Sichtbarkeit des Untermenüs steuert – über das <code>aria-expanded</code>-Attribut zur barrierefreien Bedienung.
<nav class="button-variant">
<ul>
<li><a href="">Vorspeise</a></li>
<li>
<a href="">Hauptgericht</a>
<button aria-expanded="false">
<span visually-hidden="true">Untermenü Hauptgericht</span>
</button>
<ul>
<li><a href="">Hauptgericht mit Fleisch</a></li>
<li><a href="">Hauptgericht vegetarisch</a></li>
</ul>
</li>
<li><a href="">Dessert</a></li>
</ul>
</nav>
<p>Hier ist das Hauptgericht wieder ein Link. Es sind zwei getrennte UI-Elemente: der Link und der Button zum Öffnen/Schließen des Untermenüs.</p>
<p>Bei den unteren Menüs fehlen noch Zutaten:</p>
<ul>
<li>Das Untermenü sollte sich beim Verlassen schließen.</li>
<li>Das Untermenü sollte sich per Esc-Taste schließen.</li>
</ul>
<p>(Die Liste erhebt keinen Anspruch auf Vollständigkeit.)</p>
body
{
margin: 0;
padding-bottom: 4em;
font-family: Calibri, sans-serif;
line-height: 1.5;
}
nav
{
--background-color: hsl(-10deg 80% 40%);
--text-color: white;
background-color: var(--background-color);
color: var(--text-color);
box-shadow: 0 0.1em 0.1em black;
font-family: Didot, serif;
font-size: 1.25em;
line-height: 1.25;
}
nav ul
{
list-style: none;
// Safari hack, see https://www.scottohara.me/blog/2019/01/12/lists-and-safari.html
list-style: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E");
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
}
nav ul ul
{
display: none;
position: absolute;
left: 0;
top: 100%;
background-color: var(--background-color);
box-shadow: 0 0.1em 0.1em 0em black;
z-index: 1;
font-size: 0.9em;
}
nav.focus-within-variant li:focus-within ul
{
display: block;
}
nav [aria-expanded="true"] + ul
{
display: block;
}
nav li:hover ul
{
display: block;
}
[aria-expanded]
{
padding-right: 2em;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'%3E%3Cpath d='M0.3,0.1 0.3,0.9 0.8,0.5z' fill='white'/%3E%3C/svg%3E");
background-position: right 1em center;
background-size: 0.8em 0.8em;
background-repeat: no-repeat;
}
[aria-expanded="true"]
{
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'%3E%3Cpath d='M0.1,0.3 0.9,0.3 0.5,0.8z' fill='white'/%3E%3C/svg%3E");
}
nav li
{
padding: 0.25em 1em;
position: relative;
}
nav a,
nav button
{
display: inline-block;
color: currentColor;
text-decoration: none;
margin: -0.25em -1em;
padding: 0.25em 1em;
}
nav button
{
background-color: transparent;
background-color: var(--background-color);
border: none;
font: inherit;
}
nav a + button
{
margin-left: 0;
padding-left: 0;
width: 1em;
height: 1.75em;
vertical-align: text-top;
}
nav a:hover
{
background-color: hsl(-10deg 75% 50%);
}
nav a:focus,
nav button:focus
{
background-color: black;
outline: none;
}
:is(p, ul)
{
margin: 1em 1em 4em;
max-width: 36em;
}
:is(p, ul) + :is(p, ul)
{
margin-top: -4em;
}
code
{
font-family: Fira Mono, monospace;
}
visually-hidden,
.visually-hidden,
[visually-hidden="true"]
{
position: absolute !important;
clip: rect(1px, 1px, 1px, 1px) !important;
padding: 0 !important;
border: 0 !important;
height: 1px !important;
width: 1px !important;
overflow: hidden !important;
}
document.documentElement.addEventListener('click', event => {
if (event.target.tagName == 'BUTTON' && event.target.hasAttribute('aria-expanded'))
{
event.target.setAttribute('aria-expanded', event.target.getAttribute('aria-expanded') != 'true');
}
});
document.documentElement.lang = 'de';
Also see: Tab Triggers