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>
<h2>Header content</h2>
<nav>
<ul>
<li><a href="#">Nav Link 1</a></li>
<li><a href="#">Nav Link 2</a></li>
<li><a href="#">Nav Link 3</a></li>
<li><a href="#">Nav Link 4</a></li>
<li><a href="#">Nav Link 5</a></li>
<li><a href="#">Nav Link 6</a></li>
<li><a href="#">Nav Link 7</a></li>
<li><a href="#">Nav Link 8</a></li>
<li><a href="#">Nav Link 9</a></li>
</ul>
</nav>
</header>
<main>
<h1>Scroll-Padding Demo</h1>
<p>This demo uses <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-padding-top"><code>scroll-padding-top</code></a> to constrain browser auto-scroll behavior, preventing focused elements from being obscured by sticky headers. It's based on the <a href="https://alastairc.uk/tests/wcag22-examples/sticky-footer4.html">Sticky headers / footers and focus styles</a> demo by Alastair Campbell, and is a different technique from our <a href="https://www.tpgi.com/prevent-focused-elements-from-being-obscured-by-sticky-headers/">Sticky Margin design pattern</a>.</p>
<p>To see the behavior in action: TAB down to the bottom of the page, and then start to SHIFT+TAB back up again.</p>
<p>For this demo, the behavior only applies if the viewport width is <code>40rem</code> or more. For smaller viewports, the header is static rather than sticky, and so the scroll padding isn't applied.</p>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a></li>
<li><a href="#">Link 7</a></li>
<li><a href="#">Link 8</a></li>
<li><a href="#">Link 9</a></li>
<li><a href="#">Link 10</a></li>
<li><a href="#">Link 11</a></li>
<li><a href="#">Link 12</a></li>
<li><a href="#">Link 13</a></li>
<li><a href="#">Link 14</a></li>
<li><a href="#">Link 15</a></li>
<li><a href="#">Link 16</a></li>
<li><a href="#">Link 17</a></li>
<li><a href="#">Link 18</a></li>
<li><a href="#">Link 19</a></li>
<li><a href="#">Link 20</a></li>
<li><a href="#">Link 21</a></li>
<li><a href="#">Link 22</a></li>
<li><a href="#">Link 23</a></li>
<li><a href="#">Link 24</a></li>
<li><a href="#">Link 25</a></li>
<li><a href="#">Link 26</a></li>
<li><a href="#">Link 27</a></li>
<li><a href="#">Link 28</a></li>
<li><a href="#">Link 29</a></li>
<li><a href="#">Link 30</a></li>
</ul>
</main>
/* Required: */
header {
left: 0;
position: static;
top: 0;
}
@media (min-width: 40rem) {
header {
position: sticky;
}
}
/* Optional: */
:root {
font-family: sans-serif;
font-size: 1.2em;
}
html, body {
background: #fff;
margin: 0;
color: #444;
}
ul, li {
list-style: none;
margin: 0;
padding: 0;
}
a:focus {
outline: 2px solid #05f;
outline-offset: 2px;
}
code {
font-family: Menlo, Courier, monospace;
}
header {
background: #ded;
border: 2px solid #090;
box-sizing: border-box;
padding: 1rem;
width: 100%;
}
header > h2::after {
content: " (static)";
}
@media (min-width: 40rem) {
header {
background: #fec;
border-color: #f60;
}
header > h2::after {
content: " (sticky)";
}
}
header h2 {
font-weight: normal;
margin: 0 0 1rem 0;
}
header nav,
header nav ul,
header nav li {
display: flex;
flex-wrap: wrap;
}
header nav a {
background: #fff;
border: 2px solid #090;
color: #05f;
display: block;
margin: 0 2px 2px 0;
padding: 0.4rem 0.7rem;
position: relative;
z-index: 0;
}
@media (min-width: 40rem) {
header nav a {
border-color: #f60;
}
}
header nav a:focus {
z-index: 1;
}
main {
padding: 2rem;
}
main h1 {
font-weight: normal;
margin: 0 0 1rem 0;
}
main p {
line-height: 1.5;
margin: 0 0 1rem 0;
}
main li a {
background: #eee;
border: 2px solid #666;
color: #05f;
display: inline-block;
margin: 0 0 6px 0;
min-width: 4rem;
padding: 0.4rem 0.7rem;
}
window.addEventListener('DOMContentLoaded', () => {
//CONFIG: specify a limiting media query
//n.b. this is so small-screen/high-zoom layouts can un-stick the header
//in which case, scroll-padding is unnecessary and wastes valuable space
//so it will only be applied if the page matches this media query
const MEDIA_MATCH = '(min-width: 40rem)';
//CONFIG: specify extra padding to allow for focus outlines
//n.b. this avoids the possibility of the outline being slightly clipped,
//when a focused element is close to the threshold, since scroll-padding
//applies to the layout box, and outline isn't part of the layout box
const EXTRA_PADDING = 6;
//get the header reference
const header = document.querySelector('header');
//create a style node and get its stylesheet reference
const style = document.head.appendChild(document.createElement('style'));
const stylesheet = style.sheet;
//function: apply scroll padding from header height
const applyScrollPadding = () => {
//delete any existing rule from the stylesheet
//n.b. using while in case of race conditions that add duplicate rules
while(stylesheet.cssRules.length) {
stylesheet.deleteRule(0);
}
//get the header height, and add a little extra as specified
let height = header.offsetHeight + EXTRA_PADDING;
//create a new rule for scroll padding from the header height
//filtered using :has(main *:focus) if that's supported
//so that it doesn't apply to elements inside the header
//n.b. it's fine to use pixels, because any change in height
//caused by text resizing will trigger the resize observer
if('CSS' in window && CSS.supports('selector(:has(*))')) {
stylesheet.insertRule(`html:has(main *:focus) { scroll-padding-top:${height}px }`);
} else {
stylesheet.insertRule(`html { scroll-padding-top:${height}px }`);
}
//if an element outside the header is currently focused,
//and the change in height has caused it to become obscured,
//then scroll it into view (as though the user had tabbed there)
//n.b. this prevents active focus being obscured on the fly
//but is not technically required for 2.4.11 conformance
let focused = document.activeElement || document.body;
if(focused !== document.body && !header.contains(focused)) {
if(focused.getBoundingClientRect().top < height) {
focused.scrollIntoView();
}
}
};
//define a resize observer to monitor the size of the header
//n.b. this fires on first observation, then for anything that resizes
//the elements, eg. font size, text spacing, window size or orientation
const observer = new ResizeObserver(applyScrollPadding);
//function: enable resize monitor and apply scroll-padding
//n.b. this is abstracted so we can turn it on and off
//in response to changes in the media query match status
const enableResizeMonitor = () => {
observer.observe(header);
};
//function: disable resize monitor and remove applied scroll-padding
const disableResizeMonitor = () => {
observer.unobserve(header);
while(stylesheet.cssRules.length) {
stylesheet.deleteRule(0);
}
};
//pass the limiting media query to matchMedia to get the initial match
//then define a change listener to update this when the match changes
//and either enable or disable the resize monitor accordingly
const query = window.matchMedia(MEDIA_MATCH);
query.addEventListener('change', (e) => {
if(e.matches) {
enableResizeMonitor();
} else {
disableResizeMonitor();
}
});
//if we already match the current media, enable the monitor
if(query.matches) {
enableResizeMonitor();
}
});
Also see: Tab Triggers