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

              
                <html>
  <head>
    <title>Toggle Popover Tutorial</title>
  </head>
  <body>
    <section class="title-section"><h1>Click Button to Open Popover. Then scroll and adjust browser width to see the positioning magic!</h1></section>
    <section id="popper-section">
    <button id="popper-button">Toggle Popover</button>
    <div id="popper-popup">
      <a href="#">menu item</a>
      <a href="#">menu item</a>
      <a href="#">menu item</a>
      <a href="#">menu item</a>
      <div id="popper-arrow"></div>
    </div>
      
    </section>
     <script src="https://unpkg.com/@popperjs/core@2"></script>
  </body>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 95vh 0;
  font-family: "lato";
}
section {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}
.title-section {
  display:block;
  max-width: 500px;
  padding: 3%;
  position: fixed;
  top: 50%;
  right: 0;
  z-index:-1;
}
button {
  font: 500 20px lato !important;
  color: #ffffff;
  border: none;
  background: purple;
  padding: 1em 2em;
}
button:hover {
  cursor:pointer;
}

#popper-popup {
  display: none;

  background: #333;
  color: white;
  font-weight: bold;
  padding: 20px;
  font-size: 13px;
  border-radius: 4px;
  display: none;
  width: 100%;
  max-width: 400px;
}
#popper-popup a {
  font-size: 16px;
  font-weight: 300;
  display: block;
  padding: 20px;
  color: #ffffff;
  text-align: center;
  text-decoration: none;
  text-transform: uppercase;
}
#popper-popup a:hover {
  background: #444444;
}
#popper-popup[data-popper-reference-hidden] {
  visibility: hidden;
  pointer-events: none;
}
#popper-popup[show-popper] {
  display: block;
  z-index: 9999;
}

/*keep popper visible in the Divi Builder for editing*/
#et-fb-app #popper-popup {
  display: block;
}

/*build the arrow targeting the divider with class 'popper-arrow'*/
#popper-arrow,
#popper-arrow::before {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #333333;
}
#popper-arrow {
  visibility: hidden;
}
#popper-arrow::before {
  visibility: visible;
  content: "";
  transform: rotate(45deg);
}

/*position arrow using popper.js data-popper-placement attribute*/
#popper-popup[data-popper-placement^="top"] #popper-arrow {
  bottom: -4px;
}

#popper-popup[data-popper-placement^="bottom"] #popper-arrow {
  top: -4px;
}

#popper-popup[data-popper-placement^="left"] #popper-arrow {
  right: -4px;
}

#popper-popup[data-popper-placement^="right"] #popper-arrow {
  left: -4px;
}

/*hide arrow when reference outside of viewport*/
#popper-popup[data-popper-reference-hidden] #popper-arrow::before {
  visibility: hidden;
}

              
            
!

JS

              
                const popperButton = document.querySelector("#popper-button");
const popperPopup = document.querySelector("#popper-popup");
const popperSection = document.querySelector("#popper-section");
const popperArrow = document.querySelector("#popper-arrow");

let popperInstance = null;

//create popper instance
function createInstance() {
  popperInstance = Popper.createPopper(popperButton, popperPopup, {
    placement: "auto", //preferred placement of popper
    modifiers: [
      {
        name: "offset", //offsets popper from the reference/button
        options: {
          offset: [0, 8]
        }
      },
      {
        name: "flip", //flips popper with allowed placements
        options: {
          allowedAutoPlacements: ["right", "left", "top", "bottom"],
          rootBoundary: "viewport"
        }
      }
    ]
  });
}

//destroy popper instance
function destroyInstance() {
  if (popperInstance) {
    popperInstance.destroy();
    popperInstance = null;
  }
}

//show and create popper
function showPopper() {
  popperPopup.setAttribute("show-popper", "");
  popperArrow.setAttribute("data-popper-arrow", "");
  createInstance();
}

//hide and destroy popper instance
function hidePopper() {
  popperPopup.removeAttribute("show-popper");
  popperArrow.removeAttribute("data-popper-arrow");
  destroyInstance();
}

//toggle show-popper attribute on popper to hide or show it with CSS
function togglePopper() {
  if (popperPopup.hasAttribute("show-popper")) {
    hidePopper();
  } else {
    showPopper();
  }
}
//execute togglePopper function when clicking the popper reference/button
popperButton.addEventListener("click", function (e) {
  e.preventDefault();
  togglePopper();
});

              
            
!
999px

Console