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>
  <nav id="Nav">
    <ul>
      <li><a href="http://example.com/?1" id="item01">Home</a></li>
      <li class="selected">
        <a href="http://example.com/?2" id="Item02" aria-current="page">About</a>
        <details>
          <summary aria-labelledby="Item02">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" focusable="false"><path d="M70.3 13.8L40 66.3 9.7 13.8z"></path></svg>
          </summary>
          <ul id="SubItem02">
            <li><a href="http://example.com/?2a">Our Team</a></li>
            <li><a href="http://example.com/?2b">Press</a></li>
            <li><a href="http://example.com/?2c">Contact</a></li>
            <li><a href="http://example.com/?2d" aria-current="page">Job Postings</a></li>
          </ul>
        </details>
      </li>
      <li><a href="http://example.com/?3" id="Item03">Blog</a></li>
      <li><a href="http://example.com/?4" id="Item04">Services</a>
        <details>
          <summary aria-labelledby="Item04">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" focusable="false"><path d="M70.3 13.8L40 66.3 9.7 13.8z"></path></svg>
          </summary>
          <ul id="SubItem04">
            <li><a href="http://example.com/?4a">Management Consulting</a></li>
            <li><a href="http://example.com/?4b">Training</a></li>
            <li><a href="http://example.com/?4c">Financial Audits</a></li>
            <li><a href="http://example.com/?4d">Expert Witness</a></li>
            <li><a href="http://example.com/?4e">Localization</a></li>
            <li><a href="http://example.com/?4f">Writing Very Nasty Letters</a></li>
          </ul>
        </details>
      </li>
      <li>
        <a href="http://example.com/?5" id="Item05">Products</a>
        <details>
          <summary aria-labelledby="Item05">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" focusable="false"><path d="M70.3 13.8L40 66.3 9.7 13.8z"></path></svg>
          </summary>
          <ul id="SubItem05">
            <li><a href="http://example.com/?5a">Content Management Systems</a></li>
            <li><a href="http://example.com/?5b">Translation Management Software</a></li>
            <li><a href="http://example.com/?5c">Dessert Topping</a></li>
            <li><a href="http://example.com/?5d">Floor Wax</a></li>
          </ul>
        </details>
      </li>
      <li>
        <a href="http://example.com/?6" id="Item06">Events</a>
        <details>
          <summary aria-labelledby="Item06">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" focusable="false"><path d="M70.3 13.8L40 66.3 9.7 13.8z"></path></svg>
          </summary>
          <ul id="SubItem06">
            <li><a href="http://example.com/?6a">Press Events</a></li>
            <li><a href="http://example.com/?6b">Conferences</a></li>
            <li><a href="http://example.com/?6c">Workshops</a></li>
            <li><a href="http://example.com/?6d">Algonquinpalooza</a></li>
          </ul>
        </details>
      </li>
      <li><a href="http://example.com/?7" id="Item07">Contact</a>
        <details>
          <summary aria-labelledby="Item07">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80" focusable="false"><path d="M70.3 13.8L40 66.3 9.7 13.8z"></path></svg>
          </summary>
          <ul id="SubItem07">
            <li><a href="http://example.com/?7a">Phone</a></li>
            <li><a href="http://example.com/?7b">Mailing Address</a></li>
            <li><a href="http://example.com/?7c">Summoning Circles</a></li>
          </ul>
        </details>
      </li>
    </ul>
  </nav>
</header>
<main>
  <h1>Navigation</h1>
  <p>
    Prototype navigation pattern. Forked from Adrian Roselli's version using a custom disclosure widget pattern; read more about it in his blog post <a href="http://adrianroselli.com/2019/06/link-disclosure-widget-navigation.html"><cite>Link + Disclosure Widget Navigation</cite></a>.
  </p>
  <p>Thanks to Paul Grenier for investigating a problem with the JavaScript and Chrome's find-in-page feature and <a href="https://codepen.io/AutoSponge/pen/eYebqNa">making his own version</a>.</p>
  <p><strong>March 2024</strong> <a href="https://codepen.io/ccwilcox/pen/mdgypGJ">Details-Summary Navigation with name attribute</a> is the latest version of this experiment.</p>
  <p class="force-scroll">Paragraph to demo Firefox  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1726454">Bug 1726454 Spacebar performs two actions when summary element has focus</a>. <strong>Possibly resolved in Firefox 107</strong>.</p>
</main>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Open+Sans');

body {
  background-color: #eeeeee;
  font-size: 100%;
  color: #333;
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto,
    Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  padding: 0;
  margin: 0;
}

header,
nav,
main {
  display: block;
  box-sizing: border-box;
  width: auto;
  padding: 1em 1vw;
  margin: 1em 1vw;
  color: #000;
}

header,
nav {
  padding: 0;
  margin: 0;
}

nav {
  margin-top: 0.5em;
  background-color: #ddd;
  font-size: 90%;
}

nav ul,
nav li {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

nav > ul {
  display: flex;
  flex-wrap: no-wrap; /* RWD */
  justify-content: space-evenly;
  align-items: stretch;
}

nav > ul > li {
  flex: 1 1 auto; /* RWD */
  margin: 0;
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-start;
  align-content: flex-start;
  padding: 0 1em;
  border-right: 0.1em solid #ccc;
  border-bottom: 0.1em solid #ccc;
  position: relative;
}

nav > ul > li:first-child {
  border-left: 0.1em solid #ccc;
}

nav > ul > li > a {
  padding: 1em 0;
  text-align: center;
}

nav > ul > li > * {
  flex: 1 1 auto;
  box-sizing: border-box;
}

/* Prevent details contents from overlapping other content in browsers that don't support 'details' element.  */
nav > ul > li > details:not([open]) > * {
  position: inherit;
}

/* Prevent browser's find-in-page feature from finding details contents and opening the the details element  */
nav > ul > li > details:not([open]) > :not(summary) {
  display: none;
}

/* Optional: Class added to nav in browsers that don't support the 'details' element. */
.details-unsupported ul {
  flex-wrap: wrap;
}

/* Optional: Manually add 'details-hide' to nav if you want added class to hide 'details', instead of changing their layout, in unsupported browsers */
.details-unsupported.details-hide details {
  display: none;
}

.details-unsupported ul > li,
.details-unsupported ul > li > *{
  display: block;
}

.details-unsupported summary {
  display: none;
}

nav > ul > li > details {
  flex: 0 0 1em;
}

nav > ul > li > details summary {
  border: 0.15em solid #000;
  border-radius: 50%;
  line-height: 0;
  list-style: none; /* remove Chrome, Firefox marker */
  padding: 0.15em 0.15em 0.05em 0.15em;
  margin: 1.1em 1em 0 1em;
  text-align: center;
}

/* remove Safari marker */
nav > ul > li > details summary::-webkit-details-marker,
nav > ul > li > details summary::marker {
 display: none; 
}

nav > ul > li > details summary svg {
  width: 1em;
  height: 1em;
  fill: #000;
  transition: transform 0.25s ease-in;
  transform-origin: center 45%;
}

details[open] summary svg {
  transform: rotate(180deg);
}

nav > ul li > details ul {
  position: absolute;
  top: 100%;
  left: -0.1em;
  right: -0.5em;
  width: auto;
  min-width: 12em;
  height: auto;
  overflow: visible;
  z-index: 10;
  margin: 0;
  padding: 1em 0;
  border: 0.01em solid transparent;
}

nav > ul ul a {
  display: block;
  font-size: 95%;
  margin: 0;
  padding: 0.5em 1.3em;
}

nav > ul ul a:hover,
nav > ul ul a:focus {
  background-color: #ccc;
}

@media screen and (min-width: 60em) {
  /* At arbitrary desktop width, keep right menu from going out of viewport */
  nav > ul li:last-child > ul {
    left: auto;
    right: 0;
  }
}

@media screen and (max-width: 60em) {
  body {
    background-color: #ffe;
  }
  nav > ul {
    /* Has to happen in a max-width MQ */
    /* Sorry, no mobile-first in this case */
    flex-wrap: wrap; /* RWD */
  }
  nav > ul > li {
    border-right: 0.05em solid #ccc;
    border-left: 0.05em solid #ccc;
  }
  nav > ul > li:first-child {
    border-left-width: 0.05em;
  }
  nav > ul li > ul {
    right: 0;
  }
}

@media screen and (-ms-high-contrast: active) {
  nav {
    border-top: 0.1em solid windowText;
  }
}

/* Colors */

nav,
nav ul,
nav > ul ul a:hover,
nav > ul ul a:focus,
nav summary {
  background-color: #013461;
}

nav a,
nav a:link,
nav a:visited,
nav :hover,
nav :focus,
nav :active {
  color: #fff;
  text-decoration: none;
  outline: none;
}

nav > ul > li > details summary svg {
  fill: #fff;
}

nav > ul > li,
nav > ul > li:first-child,
nav > ul > li > details summary {
  border-color: #0f5f8c;
}

nav > ul > li > details summary:hover, 
nav > ul > li > details summary:focus {
  border-color: #fff;
}

nav > ul > li:hover,
nav > ul > li > a:hover,
nav > ul > li > a:focus,
nav > ul li > ul,
nav > ul li > details ul {
  background-color: #005c90;
}

nav > ul > li:focus-within {
  /* Keep separate for IE11 */
  background-color: #005c90;
}

nav > ul > li a[aria-current="page"],
nav > ul > li:hover a[aria-current="page"],
nav > ul > li:focus a[aria-current="page"],
nav > ul > li.selected,
nav > ul > li.selected:hover {
  background-color: #187aba;
  border-color: #187aba;
}

.force-scroll {
  margin-top: calc(100vh + 5rem);
}
              
            
!

JS

              
                // get details in the nav ("Nav" is id of the relevant <nav> element)
const allNavDetails = Nav.querySelectorAll('details');

// detect whether 'details' elements are supported
if (!('open' in document.createElement('details'))) {
  // Add class to change layout if 'details' elements are unsupported (optional, improves usability and appearance).
  Nav.classList.add('details-unsupported');
}
else {
  // Enhance 'details' if they're supported
  
  // add toggle event listeners to details
  for (var i = 0; i < allNavDetails.length; i++) {
    allNavDetails[i].addEventListener('toggle', function(e) {
      if (e.target.open) closeSubNavs(e); // don't fire on close
    });
  }

  // close all details except one just clicked
  function closeSubNavs(e) {
    for (var i = 0; i < allNavDetails.length; i++) {
      if (allNavDetails[i] !== e.target ) {
        allNavDetails[i].removeAttribute('open');
      }
    }
  }

  // Escape key closes open details
  document.onkeydown = function(evt) {
    evt = evt || window.event;
    var isEscape = false;
    if ("key" in evt) {
      isEscape = evt.key == "Escape" || evt.key == "Esc";
    } else {
      isEscape = evt.keyCode == 27;
    }
    if (isEscape) {
      //alert("Escape");
      closeSubNavs(evt);
    }
  };
}

              
            
!
999px

Console