Master Bootstrap From Scratch

Clear, interactive, and structured Bootstrap lessons designed for beginners and developers.

Bootstrap Modals

Learn how to display popup dialogs using Bootstrap 5 modals.

What is a Modal?

A modal is a popup dialog displayed on top of the current webpage. It is useful for login forms, confirmation messages, alerts, and displaying additional information without navigating to another page.

1. Basic Modal

A basic modal contains a title, body content, and a close button. The data-bs-toggle="modal" attribute opens the modal, and data-bs-target identifies the modal element.

Program Code
<button class="btn btn-primary"
        data-bs-toggle="modal"
        data-bs-target="#basicModal">
    Open Modal
</button>

<div class="modal fade" id="basicModal" tabindex="-1"
     aria-labelledby="basicModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title" id="basicModalLabel">
                    Welcome
                </h5>
                <button type="button" class="btn-close"
                        data-bs-dismiss="modal"
                        aria-label="Close"></button>
            </div>

            <div class="modal-body">
                Welcome to Bootstrap Modals!
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-secondary"
                        data-bs-dismiss="modal">
                    Close
                </button>
            </div>

        </div>
    </div>
</div>
Program Output

Click the button to open the actual modal.

2. Modal with Header, Body and Footer

The modal header displays the title, the body displays content, and the footer contains action buttons.

Program Code
<div class="modal-content">
    <div class="modal-header">
        <h5 class="modal-title">Student Details</h5>
    </div>

    <div class="modal-body">
        Name: Rahul Sharma
        Course: .NET Full Stack
    </div>

    <div class="modal-footer">
        <button class="btn btn-success">Confirm</button>
        <button class="btn btn-secondary"
                data-bs-dismiss="modal">Close</button>
    </div>
</div>
Program Output

3. Large Modal

Use modal-lg to create a wider modal for larger forms or additional information.

Program Code
<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title">Course Information</h5>
        </div>
        <div class="modal-body">
            Detailed course information goes here.
        </div>
    </div>
</div>
Program Output

4. Confirmation Modal

Confirmation modals ask users to confirm an action before continuing. This example displays a message when the user confirms.

Program Code
<button class="btn btn-danger"
        data-bs-toggle="modal"
        data-bs-target="#confirmModal">
    Delete Student
</button>

<div class="modal fade" id="confirmModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                Are you sure you want to delete this student?
            </div>
            <div class="modal-footer">
                <button class="btn btn-secondary"
                        data-bs-dismiss="modal">
                    Cancel
                </button>
                <button class="btn btn-danger"
                        data-bs-dismiss="modal">
                    Confirm
                </button>
            </div>
        </div>
    </div>
</div>
Program Output
Demo confirmation completed. No actual student data was deleted.

5. Modal with a Form

A modal can contain a form to collect information without leaving the current page.

Program Code
<div class="modal-body">
    <form>
        <label for="studentName" class="form-label">
            Student Name
        </label>
        <input type="text" id="studentName"
               class="form-control"
               placeholder="Enter student name">

        <label for="studentEmail" class="form-label mt-3">
            Email
        </label>
        <input type="email" id="studentEmail"
               class="form-control"
               placeholder="Enter email">
    </form>
</div>
Program Output
Demo form submitted successfully!

Important Modal Classes

Class / Attribute Purpose
modal Defines the modal container.
modal-dialog Defines the modal dialog wrapper.
modal-content Contains the modal's visible content.
modal-header Contains the title and close button.
modal-body Contains the main content.
modal-footer Contains action buttons.
modal-lg Creates a larger modal.
data-bs-dismiss="modal" Closes the modal when activated.

Common Mistakes

  • The button's data-bs-target must match the modal's ID.
  • Use Bootstrap 5 attributes such as data-bs-toggle, not Bootstrap 4's data-toggle.
  • Make sure the Bootstrap JavaScript bundle is loaded in the layout.
  • Use unique IDs when adding multiple modals to the same page.
  • A confirmation popup alone does not delete database records. Connect it to the appropriate backend action to perform real deletion.

Summary

  • Modals display popup content without navigating away.
  • data-bs-toggle="modal" opens a modal.
  • data-bs-target specifies which modal to open.
  • data-bs-dismiss="modal" closes a modal.
  • Modals can contain text, forms, and confirmation actions.