CodeMIRAI
0 XP

Modals and alerts

3 min read 6 runnable examples Quiz included

Alerts

HTML + Bootstrap
<div class="p-3">
  <div class="alert alert-success" role="alert">Your changes were saved.</div>
  <div class="alert alert-warning" role="alert">Your trial ends in 3 days.</div>
  <div class="alert alert-danger" role="alert">
    <h4 class="alert-heading h6">Payment failed</h4>
    The card was declined. <a href="#" class="alert-link">Update your card</a>.
  </div>
</div>

An alert is a coloured message box. Use the colour that matches the meaning, and role="alert" so screen readers announce it. alert-heading and alert-link style a heading and a link inside it.

Dismissible alerts

HTML + Bootstrap
<div class="p-3">
  <div class="alert alert-info alert-dismissible fade show" role="alert">
    New lessons were added this week.
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  </div>
</div>

alert-dismissible makes room for the close button, fade show animates the closing, and data-bs-dismiss="alert" on the button closes the alert, through Bootstrap's JavaScript. Click the ✕.

Alerts from JavaScript

HTML + Bootstrap
<div class="p-3">
  <button type="button" class="btn btn-primary" id="notify">Show a message</button>
  <div id="messages" class="mt-3"></div>
</div>
<script>
  let n = 0;
  document.querySelector("#notify").addEventListener("click", () => {
    n++;
    const box = document.createElement("div");
    box.className = "alert alert-success alert-dismissible fade show";
    box.setAttribute("role", "alert");
    box.textContent = `Message number ${n}.`;
    const close = document.createElement("button");
    close.type = "button";
    close.className = "btn-close";
    close.setAttribute("data-bs-dismiss", "alert");
    close.setAttribute("aria-label", "Close");
    box.append(close);
    document.querySelector("#messages").append(box);
  });
</script>

Alerts added later work too: Bootstrap listens for clicks on any data-bs-dismiss="alert" button.

Modals

HTML + Bootstrap
<div class="p-3">
  <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#confirmDelete">Delete account</button>
</div>

<div class="modal fade" id="confirmDelete" tabindex="-1" aria-labelledby="confirmTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="confirmTitle">Delete your account?</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">This deletes your progress and badges. It can't be undone.</div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Delete</button>
      </div>
    </div>
  </div>
</div>

A modal is a dialog on top of the page, which keeps focus inside it until it's closed. The button's data-bs-target names the modal's id. Close it with the ✕, either button, the Esc key, or a click on the dark background.

ClassEffect
modal-dialog-centeredcentres the dialog vertically
modal-sm, modal-lg, modal-xlsizes, added to modal-dialog
modal-dialog-scrollablescrolls the body when the content is long
modal-fullscreencovers the whole screen

Opening a modal from JavaScript

HTML + Bootstrap
<div class="p-3">
  <button type="button" class="btn btn-primary" id="start">Start the quiz</button>
</div>

<div class="modal fade" id="ready" tabindex="-1" aria-labelledby="readyTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-sm">
    <div class="modal-content">
      <div class="modal-body text-center">
        <h2 class="h5" id="readyTitle">Ready?</h2>
        <p class="mb-0">The quiz starts in <span id="count">3</span>...</p>
      </div>
    </div>
  </div>
</div>
<script>
  document.querySelector("#start").addEventListener("click", () => {
    const modal = bootstrap.Modal.getOrCreateInstance("#ready");
    let left = 3;
    document.querySelector("#count").textContent = left;
    modal.show();
    const timer = setInterval(() => {
      left--;
      document.querySelector("#count").textContent = left;
      if (left === 0) {
        clearInterval(timer);
        modal.hide();
      }
    }, 1000);
  });
</script>

bootstrap.Modal.getOrCreateInstance(...) gives you the modal's JavaScript object, with show(), hide() and toggle(). Use it when code, not a click, decides when the dialog appears.

Toasts

HTML + Bootstrap
<div class="p-3">
  <button type="button" class="btn btn-success" id="copy">Copy link</button>
</div>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
  <div id="copied" class="toast" role="status" aria-live="polite" aria-atomic="true">
    <div class="toast-body">Link copied to the clipboard.</div>
  </div>
</div>
<script>
  document.querySelector("#copy").addEventListener("click", () => {
    bootstrap.Toast.getOrCreateInstance("#copied").show();
  });
</script>

A toast is a small notification that appears in a corner and hides itself after a few seconds, for messages that don't need a reply.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.