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">
<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>
<header>
  <h1><a href="#home">Toggle Menu w/ Focus-Trap</a></h1>
  <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 Focus Trapping and 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 autofocus between them.</li>
    <li>Content will be hidden when menu is open, preventing content being focused.</li>
  </ol>
  <h3>Trap Focus by Removing Content</h3>
  <p>Without JavaScript, the ideal solution to trap focus would have been the ill-fated <code>nav-index</code> CSS property, alas it's gone.</p>
  <p>So the only other solution is to remove everything else so there's nothing <i>to</i> focus beside the menu.</p>
  <p>Doing this is disorienting for the user though.</p>
  <h3>Tab Order Issue</h3>
  <p>Because the anchors needs to auto-focus (set :target to self) and also set <code>display</code> property for contents, it means the anchor tag must live on the same level as the content elements. This makes the HTML structure a bit weird. The anchor tag must live outside of the header, and because it must alter label state it needs to be placed ahead of the header.</p>
  <p>Unfortunately, this means the anchor tags is placed ahead of the header for tab order, creating confusion if you have any other focusable element in the header besides the menu, a hyperlinked logo, for instance.</p>
  <p>You'd think it can be solved with <code>taborder</code>, but because the default tab order is 0 and negative value removes it from tab order, you cannot selectively place an element ahead of everything else - there's nothing between 0 and negative, a gross oversight in my opinion.</p>
  <p>Of course you can do the opposite, but adding tabindex for every other focusable element on the entire page is an unfeasible proposition.</p>
  <p>So for me the solution as it currently stands is not optimal, I will probably revisit it.</p>
  <a href="#">Focus Test 1</a>
  <p></p>
</main>
<aside>
  <h2>Behaviour</h2>
  <p>Trap focus by removing content while mobile menu is open.</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: #ae92f5;
  --clr-body: #f5f2fb;
  --clr-side: #e0daee;
  --clr-footer: #ae92f5;
  --clr-btn: #4a3ca1;
  --clr-btn-hover: #6353c3;
  --clr-btn-txt: #d1c3f5;
  --clr-btn-txt-hover: white;
  --clr-btn-border: #6e5fcb;
  --clr-body-txt: #2f2c35;
}

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;
}
h1 a {color:var(--clr-body-txt)}
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}

@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;
    z-index:1
  }
  .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}

  #close:target ~ header nav,
  #nav:checked ~ header nav {
    grid-area:nav / main / aside / footer;
    justify-content:flex-start;
   background:var(--clr-btn);
   display:flex;
   z-index:1;
  }
  #close:target ~ header ~ * ,
  #nav:checked ~ header ~ * {display:none;}
  
  /* label toggle */
  #nav:not(:checked) ~ header label :last-child  {visibility:hidden}
  #nav:checked ~ header label :first-child {visibility:hidden}
  /* anchor toggle */
  #close,
  #close:target + #open {display:none}
  #close:target {display:initial}
  #close:target ~ header label span:first-child {visibility:hidden}
  #close:target ~ header label span:last-child {visibility:visible}
}
              
            
!

JS

              
                
              
            
!
999px

Console