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

              
                <h1>Public Library</h1>
<h2>Checked out Books</h2>
<ul>
  <li>
    <p>
      Fahrenheit 451
      <span>Ray Bradbury</span>
    </p>
    <button id="menu-btn-1" aria-label="Actions for: Fahrenheit 451" popovertarget="menu-content-1" style="anchor-name: --menu-btn-1">
      <span class="material-symbols-outlined">
          more_vert
      </span>
    </button>
    <div id="menu-content-1" role="menu" aria-labelledby="menu-btn-1" popover tabindex="-1" style="position-anchor: --menu-btn-1">
      <button role="menuitem" tabindex="-1" onclick="alert('More information')">More information</button>
      <button role="menuitem" tabindex="-1" onclick="alert('Extend loan period')">Extend loan period</button>
      <button role="menuitem" tabindex="-1" onclick="alert('Download as ePUB')">Download as ePUB</button>
    </div>
  </li>
  <li>
    <p>
      The Hitchhiker's Guide to the Galaxy
      <span>Douglas Adams</span>
    </p>
    <button id="menu-btn-2" aria-label="Actions for: The Hitchhiker's Guide to the Galaxy" popovertarget="menu-content-2" style="anchor-name: --menu-btn-2">
      <span class="material-symbols-outlined">
          more_vert
      </span>
    </button>
    <div id="menu-content-2" role="menu" aria-labelledby="menu-btn-2" popover tabindex="-1" style="position-anchor: --menu-btn-2">
      <button role="menuitem" tabindex="-1" onclick="alert('More information')">More information</button>
      <button role="menuitem" tabindex="-1" onclick="alert('Extend loan period')">Extend loan period</button>
      <button role="menuitem" tabindex="-1" onclick="alert('Download as ePUB')">Download as ePUB</button>
    </div>
  </li>
  <li>
    <p>
      The Martian
      <span>Andy Weir</span>
    </p>
    <button id="menu-btn-3" aria-label="Actions for: The Martian" popovertarget="menu-content-3" style="anchor-name: --menu-btn-3">
      <span class="material-symbols-outlined">
          more_vert
      </span>
    </button>
    <div id="menu-content-3" role="menu" aria-labelledby="menu-btn-3" popover tabindex="-1" style="position-anchor: --menu-btn-3">
      <button role="menuitem" tabindex="-1" onclick="alert('More information')">More information</button>
      <button role="menuitem" tabindex="-1" onclick="alert('Extend loan period')">Extend loan period</button>
      <button role="menuitem" tabindex="-1" onclick="alert('Download as ePUB')">Download as ePUB</button>
    </div>
  </li>
</ul>
<p>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="not-supported">
  <p>You're <span>browser doesn't support</span> the Popover API with Anchor Positioning.</p>
</div>

              
            
!

CSS

              
                * {
  font-family: sans-serif;
}

body {
  margin: 1rem;
}

ul {
  list-style: none;
  padding: 1rem;
  max-width: 20rem;
  background: #eafaec;
  
  & li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    
    &:not(:first-child) {
      border-top: 1px dashed black;
      padding-top: 0.75rem;
    }
    
    &:not(:last-child) {
      padding-bottom: 0.75rem;
    }
    
    & p {
      display: flex;
      flex-direction: column;
      gap: 0.25rem;
      margin: 0;
      
      & span {
        font-size: 0.75rem;
        font-style: italic;
      }
    }
    
    & > button {
      flex-shrink: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #00514c;
      border: none;
      border-radius: 50%;
      color: white;
      height: 2.5rem;
      width: 2.5rem;
      outline-offset: 0.125rem;
    }
  }
}

div[role="menu"]:popover-open {
  position: absolute;
  inset-area: span-x-start bottom;
  position-try-options: flip-block;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0.25rem 0;
  border: none;
  border-radius: 0.25rem;
  box-shadow: 8px 8px 15px -5px rgb(0 0 0 / 0.75);
  
  & button {
    appearance: none;
    background: none;
    border: none;
    font-size: 0.875rem;
    padding: 0.75rem 1rem;
    text-align: left;

    &:focus {
      outline: none;
    }
 
    &:hover {
      background: lightgray;
    }
  }
  
  &.keyboard-only button:focus {
      background: lightgray;
  }
}

.not-supported {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgb(0 0 0 / 50%);
  height: 100vh;
  width: 100vw;
}
  
.not-supported p {
    border: 2px solid black;
    background: white;
    margin: 2rem;
    padding: 2rem;
}
    
.not-supported p span {
  font-weight: bold;
}

@supports (top: anchor(top)) {
  .not-supported {
    display: none;
  }
}
              
            
!

JS

              
                'use strict';

class MenuNavigationHandler {
  keyboardOnlyClass = 'keyboard-only';

  constructor(menuEl) {
    this.menuEl = menuEl;
    this.menuItems = Array.from(menuEl.children);
    this.selectedItem = null;
    this.selectedItemIndex = 0;
    // Get the toggle button which is preceding the menu in the DOM to determine if
    // the menu is opened via keyboard or mouse/touch.
    const toggleBtn = this.menuEl.previousElementSibling;
    toggleBtn.addEventListener("click", (event) => this.onToggleButtonActivation(event));
    // Handle interaction with menu
    this.menuEl.addEventListener("toggle", (event) => this.onMenuOpen(event));
    this.menuEl.addEventListener("keydown", (event) => this.onMenuKeydown(event));
  }

  onToggleButtonActivation(event) {
    // If toggle button was activated via keyboard, then `pointerType` is empty.
    if (!event.pointerType) {
      this.menuEl.classList.add(this.keyboardOnlyClass);
    }
  }

  onMenuOpen(event) {
    if (event.newState === 'open') {
      // Select first item when menu is opened
      this.selectAndFocusMenuItem(0);
    } else {
      // Cleanup when menu is closed
      this.selectedItem.tabIndex = -1;
      this.menuEl.classList.remove(this.keyboardOnlyClass);
    }
  }

  onMenuKeydown(event) {
    if (event.key === 'ArrowDown') {
      this.menuEl.classList.add(this.keyboardOnlyClass);
      this.selectNextMenuItem(event);
    } else if (event.key === 'ArrowUp') {
      this.menuEl.classList.add(this.keyboardOnlyClass);
      this.selectPreviousMenuItem(event);
    }
  }

  selectNextMenuItem(event) {
    // Remove currently selected menu item from tab order
    this.selectedItem.tabIndex = -1;
    // Focus next menu item. If we're at the last item, then loop back to first.
    if (this.selectedItemIndex < this.menuItems.length - 1) {
      this.selectAndFocusMenuItem(this.selectedItemIndex + 1);
    } else {
      this.selectAndFocusMenuItem(0);
    }
    event.preventDefault();
  }

  selectPreviousMenuItem(event) {
    // Remove currently selected menu item from tab order
    this.selectedItem.tabIndex = -1;
    // Focus previous menu item. If we're at the first item, then loop back to last.
    if (this.selectedItemIndex > 0) {
      this.selectAndFocusMenuItem(this.selectedItemIndex - 1);
    } else {
      this.selectAndFocusMenuItem(this.menuItems.length - 1);
    }
    event.preventDefault();
  }

  selectAndFocusMenuItem(index) {
    this.selectedItemIndex = index;
    this.selectedItem = this.menuItems[index];
    this.selectedItem.tabIndex = 0;
    this.selectedItem.focus();
  }
}

// Initialize menus
window.addEventListener('load', function () {
  const menus = document.querySelectorAll('div[role="menu"]');
  menus.forEach(item => {
    console.log(item);
    new MenuNavigationHandler(item);
  });
});
              
            
!
999px

Console