agyanadda@gmail.com

Bootstrap Modal

Bootstrap Components 1 min read Updated Dec 1, 2026 Topic 19 of 32

A modal is a dialog box that appears on top of the page — for login forms, confirmations, enquiry forms or video pop-ups.

Structure

  • modal fade — the outer wrapper with an id
  • modal-dialog — positions the box
  • modal-content — the white box
  • modal-header, modal-body, modal-footer

Open and close

  • Open: a button with data-bs-toggle="modal" data-bs-target="#myModal"
  • Close: any button with data-bs-dismiss="modal"

Options

  • Sizes: modal-sm, modal-lg, modal-xl, modal-fullscreen (on modal-dialog)
  • Vertically centred: modal-dialog-centered
  • Long content: modal-dialog-scrollable
  • Do not close on outside click: data-bs-backdrop="static"

Open with JavaScript

const modal = new bootstrap.Modal('#myModal');
modal.show();

Example

Example — Confirm delete modal HTML
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<div class="container my-3">
  <button class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#confirmModal">Delete account</button>
  <button class="btn btn-outline-primary ms-2" id="jsOpen">Open with JS</button>
</div>

<div class="modal fade" id="confirmModal" tabindex="-1" aria-labelledby="confirmTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="confirmTitle">Are you sure?</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">This will permanently delete your account and course progress.</div>
      <div class="modal-footer">
        <button class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
        <button class="btn btn-danger">Yes, delete</button>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
  document.getElementById('jsOpen').onclick = () => new bootstrap.Modal('#confirmModal').show();
</script>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which class vertically centres a modal?

Exercise 2 Fill in the Blank

data-bs-backdrop="___" stops the modal closing when clicking outside.

Exercise 3 True / False

A button with data-bs-dismiss="modal" closes the modal.

Frequently Asked Questions

Near the end of the body, outside other positioned elements such as fixed navbars, to avoid layering problems.