Pen Settings



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


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


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.


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.


                <h1>Modals the <em>easy</em> way</h1>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Impedit alias dicta quibusdam cumque ipsa ipsum reiciendis animi, officia provident veniam quis delectus minima earum suscipit ea, beatae vitae ipsam nemo.</p>
<p>Alias, architecto sapiente nostrum id, sed explicabo numquam aspernatur ratione qui excepturi repellat porro culpa molestiae? Culpa dolorem commodi cupiditate, eos deleniti similique repellendus nam velit natus a soluta et.</p>
<p>Fugiat ratione impedit officiis? Eligendi sapiente culpa tenetur esse quo reiciendis illum vero nisi ullam consequatur illo molestiae, nihil eaque. Esse asperiores tenetur natus! Quo asperiores molestias sed assumenda alias.</p>
<p>Sequi quo animi, nostrum laboriosam veritatis consequatur. Eos cumque unde porro explicabo provident, possimus alias magni pariatur quis consequuntur voluptatum? Debitis necessitatibus facilis fugiat repudiandae nobis neque nam, vel odio?</p>
<button class="button open-button">open modal</button>

<dialog class="modal" id="modal">
  <h2>An interesting title</h2>
  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum esse nisi, laboriosam illum temporibus ipsam?</p>
  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Atque, quo.</p>
  <button class="button close-button">close modal</button>
  <form class="form" method="dialog">
    <label>Your name <input type="text"></label>
    <label>Your email <input type="email"></label>
    <button class="button" type="submit">submit form</button>


  <form class="form" method="dialog">
    <label>Your name <input type="text"></label>
    <label>Your email <input type="email"></label>
    <button class="button" type="submit">submit form</button>


                .modal {
  // padding: 1em;
  max-width: 50ch;
  // border: 0;
  // box-shadow: 0 0 1em rgb(0 0 0 / .3);

  & > * {
    margin: 0 0 0.5rem 0;

.modal::backdrop {
  background: rgb(0 0 0 / 0.4);

/* extra styling */

body {
  min-height: 100vh;
  margin: 2rem;
  font-family: system-ui;
  font-size: 1.25rem;
  line-height: 1.5;
  color: var(--gray-8);
  // background-image: url('');
  // background-size: cover;

h1 em {
  color: var(--cyan-9);
  text-decoration: underline;
  text-underline-offset: 0.25em;

.button {
  border: 0;
  cursor: pointer;
  background: var(--gray-8);
  color: var(--gray-2);
  font-weight: 700;
  padding: var(--size-2) var(--size-4);

.button:focus {
  background: var(--cyan-9);

form {
  display: grid;
  gap: 1em;
} {
  overflow: hidden;

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



                const modal = document.querySelector("#modal");
const openModal = document.querySelector(".open-button");
const closeModal = document.querySelector(".close-button");
const body = document.querySelector("body");
const main = document.querySelector("main");

openModal.addEventListener("click", () => {

closeModal.addEventListener("click", () => {

// Would need to add listener for escape key to remove class on body

document.addEventListener("keyup", (e) => {
  if (e.keyCode == 27) {

// And to click outside of modal to close it

modal.addEventListener("click", (event) => {
  const rect = modal.getBoundingClientRect();
  if (
    event.clientY < ||
    event.clientY > rect.bottom ||
    event.clientX < rect.left ||
    event.clientX > rect.right
  ) {