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

              
                <button type="button" id="menu-button" aria-expanded="false" aria-controls="menu" aria-label="メニュー">
  <span class="bar"></span>
  <span class="menu-label" id="menu-label" aria-hidden="true">MENU</span>
</button>
<div id="overlay"></div>
<nav id="menu" inert>
  <ul class="test">
    <li><a href="#">メニュー項目1</a></li>
    <li><a href="#">メニュー項目2</a></li>
    <li><a href="#">メニュー項目3</a></li>
    <li><a href="#">メニュー項目4</a></li>
    <li><a href="#">メニュー項目5</a></li>
  </ul>
</nav>
<main>
  <section class="section">section1</section>
  <section class="section">section2</section>
</main>
              
            
!

CSS

              
                /* ハンバーガーメニュー */
#menu-button {
  position: fixed;
  right: 8px;
  display: grid;
  place-items: center;
  place-content: center;
  width: 60px;
  height: 60px;
  background: #ddd;
  border: none;
  cursor: pointer;
  z-index: 999;
}

/* バー */
.bar,
.bar::before,
.bar::after {
  width: 25px;
  height: 3px;
  background-color: #333;
  transition: transform 0.3s;
}

.bar {
  display: grid;

  &::before,
  &::after {
    content: "";
    grid-area: 1 / 1;
  }

  &::before {
    transform: translateY(-8px);
  }

  &::after {
    transform: translateY(8px);
  }
}

/* オープン時のバー */
.menu-open {
  .bar {
    background-color: transparent;

    &::before {
      transform: rotate(45deg);
    }

    &::after {
      transform: rotate(-45deg);
    }
  }
}

/* メニューラベル */
.menu-label {
  transform: translateY(10px);
  font-size: 10px;
  color: #333;
}

/* オーバーレイ */
#overlay {
  visibility: hidden;
  opacity: 0;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 997;
  transition: opacity 0.3s;

  /* オープン時のオーバーレイ */
  .menu-open & {
    visibility: visible;
    opacity: 1;
  }
}

/* メニュー */
#menu {
  position: fixed;
  height: 100%;
  width: 300px;
  background-color: #fff;
  right: 0;
  top: 0;
  z-index: 998;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.3s ease-out;

  & ul {
    list-style: none;
    padding: 0;
    margin: 80px 0 0 0;
  }

  & li {
    padding: 20px;
  }

  & a {
    color: #333;
  }
}

/* オープン時のメニュー */
.menu-open #menu {
  transform: translateX(0);
}

/* body */
body {
  overflow-x: clip;

  /* オープン時のbody */
  .menu-open& {
    overflow: clip;
  }
}

/* コンテンツ */
.section {
  width: 100%;
  height: 70vh;
  background-color: #bbb;
  display: grid;
  place-items: center;
  font-size: 2em;

  &:nth-child(2) {
    background-color: #ccc;
  }
}

              
            
!

JS

              
                const menuButton = document.getElementById("menu-button");
const menuLabel = document.getElementById("menu-label");
const menu = document.getElementById("menu");
const body = document.body;
const overlay = document.getElementById("overlay");

function toggleMenu() {
  if (!body.classList.contains("menu-open")) {
    body.classList.add("menu-open");
    menuButton.setAttribute("aria-expanded", "true");
    menuButton.setAttribute("aria-label", "閉じる");
    menu.inert = false;
    if (menuLabel) {
      menuLabel.textContent = "CLOSE";
    }
  } else {
    body.classList.remove("menu-open");
    menuButton.setAttribute("aria-expanded", "false");
    menuButton.setAttribute("aria-label", "メニュー");
    menu.inert = true;
    if (menuLabel) {
      menuLabel.textContent = "MENU";
    }
  }
}

if (menuButton) {
  menuButton.addEventListener("click", toggleMenu);
}

if (overlay) {
  overlay.addEventListener("click", toggleMenu);
}

              
            
!
999px

Console