Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Save Automatically?

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

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

CSS

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

JS

              
                
              
            
!
999px

Console