📜  bootstrap5 modal (1)

📅  最后修改于: 2023-12-03 15:29:39.455000             🧑  作者: Mango

Bootstrap 5 Modal

Bootstrap 5 Modal is a UI component that displays content on top of a page or user interface, with an overlay over the background to indicate that the content is a popup. It can be used to display important information, ask for user input, or provide additional functionality to an application.

Features
  • Supports different types of content, including text, images, and forms.
  • Provides options for positioning, sizing, and animation.
  • Can be triggered by buttons, links, or other elements on a page.
  • Responsive design that adapts to different screen sizes and devices.
  • Supports keyboard navigation and screen reader accessibility.
How to Use

To use Bootstrap 5 Modal, you need to include the necessary files in your HTML code:

<!-- CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" integrity="sha384-Z8uE7CJb+n+yYGZs1sKjmmE6aAEzGO3zsppr39GIRFstTkpTcT03+PKsZsTXsZjK" crossorigin="anonymous">

<!-- JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-V/MmHy+wb7V0m0RHU8k6U48MUGmv5okCSbd5WllC8SWvzXDaL09fnMUgk15+MO89"
crossorigin="anonymous"></script>

Once you have included these files, you can create a modal by adding the following HTML code:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" 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">
        ...
      </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 code, the first block defines the trigger button that opens the modal, and the second block defines the modal itself. The modal has a header, body, and footer, as well as two buttons to close or submit the content.

Options

Bootstrap 5 Modal provides a range of options that allow you to customize the appearance and behavior of the modal. Here are some of the most common options:

  • data-bs-backdrop: Determines whether clicking outside the modal should close it. Default is true.
  • data-bs-keyboard: Determines whether the modal can be closed by pressing the Escape key. Default is true.
  • data-bs-focus: Determines whether the modal should be focused on its first tabbable element when opened. Default is true.
  • data-bs-show: When set to true, the modal will be shown immediately on page load. Default is false.
  • data-bs-animation: Determines the animation effect used when opening or closing the modal. Default is true.
Conclusion

Bootstrap 5 Modal is a useful component for displaying content in a popup window. With its flexible options and responsive design, it can be easily customized to fit the needs of any application.