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

              
                <h2>Modal window example 4</h2>

<button class="modal__show">Show Modal</button>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nostrum error, odit <a href="#">aperiam</a> aliquid distinctio sapiente amet, ratione mollitia expedita tempore necessitatibus <a href="#">odio</a> esse! Doloribus laboriosam voluptate fugit, <a href="#">ullam</a> pariatur ab?</p>

<ul>
  <li><a href="#">Element 1</a></li>
  <li><a href="#">Element 2</a></li>
  <li><a href="#">Element 3</a></li>
  <li><a href="#">Element 4</a></li>
  <li><a href="#">Element 5</a></li>
  <li><a href="#">Element 6</a></li>
  <li><a href="#">Element 7</a></li>
  <li><a href="#">Element 8</a></li>
  <li><a href="#">Element 9</a></li>
  <li><a href="#">Element 10</a></li>
</ul>

<div class="modal" id="modal" tabindex="0" role="dialog" aria-labelled-by="modaltitle">
  <div class="modal__content">
    <h2 id="modal_title">Modal</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit qui, iusto numquam harum est officiis voluptate animi eligendi, incidunt saepe dolore. <a href="#">Adipisci</a> quos odio, optio aspernatur ex quae excepturi repudiandae.</p>
    
    <form action="#">
      <p>
        <label for="firstname">First name</label>
        <input type="text" id="firstname" />
      </p>
      <p>
        <label for="lastname">First name</label>
        <input type="text" id="lastname" />
      </p>
      
      <button>Send</button>
    </form>

    <button class="modal__close">
      close
    </button> <!-- .modal__close -->
  </div> <!-- .modal__content -->
</div> <!-- .modal -->
              
            
!

CSS

              
                .modal {
  background-color: rgba(0,0,0,0.5);

  height: 100vh;
  width: 100vw;

  position: absolute;
  left: 0;
  top: 0;

  display: none;
}

.modal--visible {
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal__content {
  background-color: rgb(255, 255, 255);
  
  height: 70%;
  width: 70%;
  padding: 2rem;

  position: relative;
}

.modal__close {
  background-color: #000000;
  
  position: absolute;
  right: -1rem;
  top: -1rem;
  
  color: #ffffff;
}

:focus {
  outline: 4px solid #FF0000;
}
              
            
!

JS

              
                // Button to trigger the modal window
var button = document.querySelector('.modal__show');
// Variable for storing the last focused element
var lastFocusedElement;
// Close button in modal window
var closeButton = document.querySelector('.modal__close')

// Open modal window on click
button.addEventListener('click', showModal);

// Show Mmdal
function showModal() {
  // Close all open modal windows
  removeModal();
  // Store the last focused element
  lastFocusedElement = document.activeElement;
  // Select the modal window
  var modal = document.getElementById('modal');
  // Show the window
  modal.classList.add('modal--visible');
  // Find all focusable children
  var focusableElementsString = 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]';
  var focusableElements = modal.querySelectorAll(focusableElementsString);
  // Convert NodeList to Array
  focusableElements = Array.prototype.slice.call(focusableElements);

  // The first focusable element within the modal window
  var firstTabStop = focusableElements[0];
  // The last focusable element within the modal window
  var lastTabStop = focusableElements[focusableElements.length - 1];
  // Focus the window
  firstTabStop.focus();
  // Add keydown event
  modal.addEventListener('keydown', function(e) {
    // Listen for the Tab key
    if (e.keyCode === 9) {
      // If Shift + Tab
      if (e.shiftKey) {
        // If the current element in focus is the first focusable element within the modal window...
        if (document.activeElement === firstTabStop) {
          e.preventDefault();
          // ...jump to the last focusable element
          lastTabStop.focus();
        }
      // if Tab
      } else {
        // If the current element in focus is the last focusable element within the modal window...
        if (document.activeElement === lastTabStop) {
          e.preventDefault();
          // ...jump to the first focusable element
          firstTabStop.focus();
        }
      }
    }
    
    // Close the window by pressing the Esc-key
    if(e.keyCode === 27) {
      removeModal();
    }
  });
}

// Close the window by clicking the close button
document.querySelector('.modal__close').addEventListener('click', removeModal);

// Remove the modal window if it's visible
function removeModal() {
  var visibleClass = 'modal--visible';
  if (document.querySelector('.' + visibleClass)) {
    document.querySelector('.' + visibleClass).classList.remove(visibleClass);
    // Return the focus to the last focused element
    lastFocusedElement.focus();
  }
}


              
            
!
999px

Console