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.
<input type="checkbox" id="nav">
<header>
<h1>Toggle Menu w/ Autofocus</h1>
<a id="close" href="#open"><span class="sr-only">Close Menu</span></a>
<a id="open" href="#close"><span class="sr-only">Open Menu</span></a>
<label for="nav">
<span>Menu</span>
<span>Dismiss</span>
</label>
<nav>
<a href="#item1">Item One</a>
<a href="#item2">Item Two</a>
<a href="#item3">Item Three</a>
<a href="#item4">Item Four</a>
</nav>
</header>
<main>
<h2>An Update of Apple's CSS Menu with Autofocus</h2>
<p>This menu is built upon the ideas of Apple's responsive menu <a id="item1" href="https://developer.apple.com/design/human-interface-guidelines/">here</a>, stripped down without extras like CSS animation.</p>
<p>It has the following change and - I hope - improvement:</p>
<ol>
<li>Toggling between the open and close anchor tag will swap focus between them.</li>
<li>Menu is inserted between header and content, instead of overlaid on top, to prevent focus from moving behind it.</li>
</ol>
<h3>Autofocus Toggling</h3>
<p>To facilitate this change, the toggle anchor tags must be siblings to either the menu or the parent of menu, CSS sibling selector "~" or "+" can then be used to toggle the menu.</p>
<p>In order to style the header without faking it visually, we position a sibling (:before pseudo element in this case) which is then overlaid onto the correct header position using CSS Grid.</p>
<p>Unfortunately <b>following anchor link does not move focus in Firefox</b> due to <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=308064">bug 308064</a>, which is 16 years old (!!!). On the flip side if you use keyboard navigation and you use Firefox this shouldn't be news to you.</p>
<h3>Position Menu Within Document Flow</h3>
<p>Because we cannot trap focus without JavaScript, it's a bad idea overlay the menu on top of the content because once the focus move out of the menu, it will move onto the first focusable element in the content, which is likely positioned behind the menu, leading to accessibility problems.</p>
<p>So the solution is to insert the menu into the document flow, so it appears above the content. The disadvantage is that the content will shift down to accommodate the menu, overall I feel it's a fair trade.</p>
<a href="#">Focus Test 1</a>
<p></p>
</main>
<aside>
<h2>Behaviour</h2>
<p>Checkbox hack and anchor links (using :target) are used to toggle the menu without JavaScript.</p>
<a href="#">Focus Test 2</a>
</aside>
<footer>
Footer
</footer>
/* 100% height */
html {
height:100%;
display: flex;
flex-direction:column;
}
body {
width:100%;
flex-grow:1;
flex-basis:0;
}
html, body {margin:0;padding:0;}
/* color scheme */
:root {
--clr-header: #e1d64b;
--clr-body: #e2e2df;
--clr-side: #cfcec4;
--clr-footer: #e1d64b;
--clr-btn: #6f5d24;
--clr-btn-hover: #a2811c;
--clr-btn-txt: #e3dea0;
--clr-btn-txt-hover: white;
--clr-btn-border: #937d37;
--clr-body-txt: #37352f;
}
body > * {padding: 1rem}
h1 {font-size: 1.4rem; margin:0}
h2 {font-size: 1.2rem}
h3 {font-size: 1.1rem;}
/* pull up menu */
body {
display:grid;
grid-template-areas:
"header header"
"main aside"
"footer footer";
grid-template-rows: auto 1fr auto;
grid-template-columns: 2fr 1fr;
font-family: sans-serif;
color: var(--clr-body-txt);
}
header {grid-area:header; background:var(--clr-header)}
main {grid-area:main; background:var(--clr-body)}
aside {grid-area:aside; background:var(--clr-side)}
footer {grid-area:footer; background:var(--clr-footer)}
header {
display:flex;
flex-wrap:wrap;
align-items:center;
justify-content:space-between;
gap:1rem;
}
nav {
display:flex;
flex-wrap:wrap;
justify-content:flex-end;
gap:.5rem .75rem
}
nav a {
padding:.3rem .6rem;
background:var(--clr-btn);
color: var(--clr-btn-txt);
text-decoration:none;
border-radius:.4rem;
}
nav a:hover,
nav a:active,
header label:hover span,
header label:active span {
background:var(--clr-btn-hover);
color:var(--clr-btn-txt-hover);
}
#open,
#close,
input#nav,
header label {display:none;user-select: none}
@media (max-width: 768px) {
body {
grid-template-areas:
"header toggle"
"nav nav"
"main main"
"aside aside"
"footer footer";
grid-template-columns: 1fr auto;
grid-template-rows: auto auto 1fr;
}
/* open up header */
header {display:contents}
/* use overlapping pseudo element for background */
header:before {
content:"";
grid-column:header/toggle;
grid-row:header;
background:var(--clr-header);
}
h1 {
grid-area:header;
padding:1rem;
display:flex;
align-items:center;
justify-content:space-between;
gap:1rem;
}
/* keep toggle label span the same size */
#open,
#close {
display:initial;
grid-area:toggle;
pointer-events: none;
cursor: default;
}
.sr-only {
position:absolute;
clip-path:inset(0px 0px 99.9% 99.9%);
overflow:hidden;
height:1px;
width:1px;
padding:0;
border:0
}
header label {
grid-area:toggle;
display:grid;
grid-template-areas:"label";
font-family:sans-serif;
font-weight:bold;
color:white;cursor:pointer;
user-select:none;
margin:.5rem 1rem;
}
header label span {
grid-area:label;
text-align:center;
background: var(--clr-btn);
padding: .5rem;
border-radius:.5rem;
align-self:center;
color:var(--clr-btn-txt)
}
/* vertical menu styling */
nav {
grid-area:nav;
flex-direction:column;
gap:0;
display:none;
}
nav a {
padding:.75rem 1rem;
border-radius:0
}
nav a:not(:last-child) {
border-bottom:1px solid var(--clr-btn-border);
}
/* prevent outline from being cut off */
#open:focus,
#close:focus,
nav a:focus {
outline-offset:-2px;
}
/* toggle logic */
#nav:checked + header nav,
#close:target ~ nav {display:flex}
/* label toggle */
#nav:not(:checked) ~ header label :last-child,
#nav:checked ~ header label :first-child {visibility:hidden}
/* anchor toggle */
#close,
#close:target + #open {display:none}
#close:target {display:initial}
#close:target ~ label span:first-child {visibility:hidden}
#close:target ~ label span:last-child {visibility:visible}
}
Also see: Tab Triggers