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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="container">
<header>
    <nav class="menu-container">
      <div class="menu-toggler" id="menu-toggler">
<div class="hamburger"></div>
      </div>
      <ul class="menu-list" id="menu-list">
<li><a href="/01">Element 01</a>
    <ul class="submenu-container">
<li><a href="#">Element 011</a></li>
<li><a href="#">Element 012</a></li>
<li><a href="#">Element 013</a></li>
<li><a href="#">Element 014</a></li>
<li><a href="#">Element 015</a></li>
    </ul>
</li>
<li><a href="/02">Element 02</a>
    <ul class="submenu-container">
        <li><a href="#">Element 021</a></li>
        <li><a href="#">Element 022</a></li>
        <li><a href="#">Element 023</a></li>
        <li><a href="#">Element 024</a></li>
        <li><a href="#">Element 025</a></li>
    </ul>
</li>
<li><a href="/03">Element 03</a>
    <ul class="submenu-container">
        <li><a href="#">Element 031</a></li>
        <li><a href="#">Element 032</a></li>
        <li><a href="#">Element 033</a></li>
        <li><a href="#">Element 034</a></li>
        <li><a href="#">Element 035</a></li>
      </ul>
</li>
<li><a href="/04">Element 04</a>
    <ul class="submenu-container">
        <li><a href="#">Element 041</a></li>
        <li><a href="#">Element 042</a></li>
        <li><a href="#">Element 043</a></li>
        <li><a href="#">Element 044</a></li>
        <li><a href="#">Element 045</a></li>
      </ul>
</li>
<li><a href="/05">Element 05</a>
    <ul class="submenu-container">
        <li><a href="#">Element 051</a></li>
        <li><a href="#">Element 052</a></li>
        <li><a href="#">Element 053</a></li>
        <li><a href="#">Element 054</a></li>
        <li><a href="#">Element 055</a></li>
      </ul>
</li>
     </ul>
   </nav>
</header>

<main>
    <h3>About</h3>
    <ul>
        <li>Minimal, easy to understand code.</li>
        <li>Dropdown menu with Hamburger in mobile resolution.</li>
        <li>In desktop resolution works only CSS, JS action is blocked or impossible.</li>
        <li>In mobile resolution all action is only on click, because of JS.</li>
        <li>Hamburger menu works like accordion, expanding any submenu collapses previously expanded.</li>
        <li>Second click on menu link of expanded submenu collapses it.</li>
        <li>Collapsing Hamburger collapses expanded submenu.</li>
        <li>Both previous action can be easily turned off.</li>
        <li>Resizing from mobile resolution to desktop one collapses expanded submenu (it also can be turned off, but IMO it's nonsense).</li>
        <li>TODO: find graceful way of hiding Hamburger. Except for that as for 2022-05-14 this is final version.</li>
    </ul>
  
<p>Illustation to blogpost <a href="https://dygresje.info/blog/webdev-menu">Frontend - menu RWD z Hamburgerem</a></p>
</main>
</div>
              
            
!

CSS

              
                .container {
  max-width: 1100px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
}

ul {
  padding-left: 0;
  list-style-type: none;
  margin: 0;
}

.menu-list > li > a {
  pointer-events: none;
}

.menu-list > li:hover > a {
    background-color: #ccc;
}

/* # MENU # */

.menu-container {
  position: relative;
  height: 2rem;
}

.menu-list {
  display: flex;
  position: absolute;
  flex-direction: column;
  left: -100%;
  width: 100%;
  /* transition: all 0.3s linear; */
}

.menu-list li {
  flex: 1;
}

.menu-list a {
  display: block;
  text-align: center;
  text-decoration: none;
  height: 1.2rem;
  padding: 5px 0;
  user-select: none;
}

.menu-list a:hover {
  text-decoration: underline;
  background-color: beige;
}

.submenu-container {
  display: none;
}

.menu-toggler {
  display: flex;
  height: 100%;
  cursor: pointer;
  padding: 0 10vw;
  padding-left: 1rem;
  background-color: #666;
}

/* # HAMBURGER # */

.hamburger {
  background-color: #ccc;
  width: 30px;
  height: 3px;
  transition: all 0.3s linear;
  align-self: center;
  position: relative;
}

.hamburger::before,
.hamburger::after {
  width: 30px;
  height: 3px;
  background-color: #ccc;
  content: "";
  position: absolute;
  transition: all 0.3s linear;
}

.hamburger::before {
  top: -10px;
}

.hamburger::after {
  top: 10px;
}

/* # OPEN # */

.open .hamburger {
  transform: rotate(-45deg);
}

.open .hamburger::before {
  transform: rotate(-90deg) translate(-10px, 0px);
}

.open .hamburger::after {
  opacity: 0;
  transform: rotate(90deg);
}

/* MENU ACTION HERE */
.open ~ .menu-list {
  left: 0;
  transition: all 0.3s linear;
}

/* SUBMENU ACTION HERE */
.menu-list li .open {
  display: block;
}

@media (min-width: 800px) {
  .menu-toggler {
    display: none;
  }

  .menu-list {
    flex-direction: row;
    left: 0;
    /* transition: all 0s; */
  }

  .menu-list li:hover .submenu-container {
    display: block;
  }
}
              
            
!

JS

              
                const toggler = document.getElementById("menu-toggler");
const sublistToggler = document.getElementById("menu-list");

const sublistToggle = (e) => {
    if (window.visualViewport.width<800)  {
    const clickedItemClasslist = e.path[0].children[1].classList
    // COLLAPSE IF EXPANDED
    if (clickedItemClasslist.contains('open')) {clickedItemClasslist.remove("open"); return;}
    // ACCORDION
    for (let i = 0; i < sublistToggler.children.length; i++) {sublistToggler.children[i].children[1].classList.remove("open")}
    // EXPAND
    clickedItemClasslist.toggle("open");
    }
  };

const listToggle = () => {
    toggler.classList.toggle("open");
    console.log("WARNING")
    for (let i = 0; i < sublistToggler.children.length; i++) {sublistToggler.children[i].children[1].classList.remove("open")}
}

sublistToggler.addEventListener("click", (e) => sublistToggle(e));
toggler.addEventListener("click", () => listToggle());

// CLOSE EXPANDED IF RESIZED UP
const closeExpanded = () => {
      if (window.visualViewport.width>799) {
        toggler.classList.remove("open");
    for (let i = 0; i < sublistToggler.children.length; i++) {sublistToggler.children[i].children[1].classList.remove("open")}
         }
   }
window.addEventListener('resize', closeExpanded);
              
            
!
999px

Console