Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>


              
            
!

CSS

              
                /* 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;
}

              
            
!

JS

              
                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();
    }

});
              
            
!
999px

Console