TExcelTAD
Home › Bootstrap › BS5 Modal

Bootstrap 5 Modal

A modal is a dialog box that appears on top of the page, dims everything behind it and keeps focus inside until it's closed. Use modals for confirmations ("Delete this file?"), short forms and important messages. They need Bootstrap's JavaScript.

Note: The preview frame is only about 280px tall, so the examples keep their modals short. If a modal is cut off, drag the frame's bottom-right corner down to make it taller, or scroll inside the frame.

Basic modal

A trigger button uses data-bs-toggle="modal" and data-bs-target="#id". The modal itself has a fixed structure: .modal › .modal-dialog › .modal-content › header, body and footer.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#confirm">Delete file</button>

  <div class="modal fade" id="confirm" tabindex="-1" aria-labelledby="confirmTitle" aria-hidden="true">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header py-2">
          <h1 class="modal-title fs-6" id="confirmTitle">Delete “notes.txt”?</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body py-2">This can't be undone.</div>
        <div class="modal-footer py-1">
          <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-sm btn-danger" data-bs-dismiss="modal">Delete</button>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Close it with the ×, a button with data-bs-dismiss="modal", the Esc key, or a click on the dark backdrop.

Structure and options

Class / attribute Purpose
.modal (+ .fade) Outer wrapper, hidden by default (.fade animates)
.modal-dialog Positions and sizes the box
.modal-content The visible white box
.modal-header / .modal-title Top bar and heading
.modal-body / .modal-footer Content / action buttons
.modal-sm / .modal-lg / .modal-xl Sizes (default is 500px)
.modal-fullscreen / .modal-fullscreen-{bp}-down Full-screen (optionally only below a breakpoint)
.modal-dialog-centered Centre vertically
.modal-dialog-scrollable Scroll the body, keep header/footer in view
data-bs-backdrop="static" Clicking the backdrop doesn't close it
data-bs-keyboard="false" Esc doesn't close it

Centred, static and scrollable

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#terms">Read terms</button>

  <div class="modal fade" id="terms" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="termsTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
      <div class="modal-content" style="max-height: 240px;">
        <div class="modal-header py-2">
          <h1 class="modal-title fs-6" id="termsTitle">Terms of use</h1>
        </div>
        <div class="modal-body small">
          <p>1. Be kind.</p><p>2. Don't share your password.</p><p>3. Back up your work.</p>
          <p>4. Report bugs.</p><p>5. Have fun learning.</p>
        </div>
        <div class="modal-footer py-1">
          <button type="button" class="btn btn-sm btn-success" data-bs-dismiss="modal">I agree</button>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

With a static backdrop and keyboard disabled, the only way out is the "I agree" button; clicking outside just makes the modal "bounce". The body scrolls while the footer stays visible.

One modal, different content

Bootstrap passes the button that opened the modal as event.relatedTarget in the show.bs.modal event. Read a data-* attribute from it to fill in the modal.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#msg" data-name="Linh">Message Linh</button>
  <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#msg" data-name="Omar">Message Omar</button>

  <div class="modal fade" id="msg" tabindex="-1" aria-labelledby="msgTitle" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header py-2">
          <h1 class="modal-title fs-6" id="msgTitle">New message</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body py-2">
          <input class="form-control" id="text" placeholder="Write something…">
        </div>
        <div class="modal-footer py-1">
          <button type="button" class="btn btn-sm btn-primary" id="send">Send</button>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const modalEl = document.getElementById('msg');
    let recipient = '';
    modalEl.addEventListener('show.bs.modal', (e) => {
      recipient = e.relatedTarget.dataset.name;
      document.getElementById('msgTitle').textContent = 'Message to ' + recipient;
    });
    modalEl.addEventListener('shown.bs.modal', () => document.getElementById('text').focus());
    document.getElementById('send').addEventListener('click', () => {
      console.log('to', recipient + ':', document.getElementById('text').value);
      bootstrap.Modal.getInstance(modalEl).hide();
    });
  </script>
</body>
</html>

Modal events

Event Fires
show.bs.modal Right before opening (event.relatedTarget = trigger)
shown.bs.modal After it is visible (good moment to focus an input)
hide.bs.modal Right before closing (call preventDefault() to cancel)
hidden.bs.modal After it is fully hidden
hidePrevented.bs.modal A static modal refused to close

Tip: Place modal markup near the end of <body>, not inside other positioned components, and don't open a second modal on top of the first — Bootstrap only supports one modal at a time.

Test yourself

Which element is the visible box of a modal?

  1. .modal
  2. .modal-dialog
  3. .modal-content
  4. .modal-body

How do you stop a modal from closing when the backdrop is clicked?

  1. data-bs-backdrop="static"
  2. data-bs-dismiss="none"
  3. .modal-locked
  4. data-bs-keyboard="false"

In show.bs.modal, what is event.relatedTarget?

  1. The modal element
  2. The button that opened the modal
  3. The backdrop
  4. The first input