BS5 Modal

In Bootstrap 5, the Modal component is used to create a dialog box or popup window that overlays the current page. Modals are commonly used to display additional content, forms, or messages that require user interaction. Here's an example of how to create a basic modal in Bootstrap 5:

html
<!-- Button to trigger the modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-
target="#myModal">
    Open Modal
</button>

<!-- The Modal -->
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel"
aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal Title</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>This is the modal content.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save Changes</button>
            </div>
        </div>
    </div>
</div>

In this example, we have a button element that triggers the modal using the `data-bs-toggle="modal"` and `data-bs-target="#myModal"` attributes. The `#myModal` value should match the `id` of the modal element.


The actual modal is represented by the `<div>` element with the classes `modal` and `fade`. The `id` attribute is set to "myModal" for the button's `data-bs-target` attribute to reference. The `tabindex="-1"` attribute ensures that the modal is not accessible via keyboard navigation when closed. The `aria-labelledby` attribute points to the element that describes the modal title.

Inside the modal, we have the `<div>` element with the class `modal-dialog` that contains the modal's content. The content is wrapped within the `<div>` element with the class `modal-content`. It consists of the modal header, body, and footer sections.

The modal header contains a title (`<h5>` element with the class `modal-title`) and a close button (`<button>` element with the classes `btn-close` and `data-bs-dismiss="modal"`).

The modal body contains the actual content, such as text, forms, or other elements.

The modal footer is optional and can contain buttons or additional content.

You can customize the appearance and behavior of the modal by adding additional classes, using JavaScript to handle events and add dynamic functionality, or modifying the modal's styles.

Bootstrap 5 also provides options for different sizes, scrollable content, and other modal features. You can refer to the Bootstrap documentation for more details and examples on working with modals in Bootstrap 5.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext