This tutorial aims to equip you with the best practices for using Bootstrap modals. We will be delving into the intricacies of creating a user-friendly, accessible, and mobile-responsive modal using Bootstrap.
By the end of this tutorial, you will understand how to:
Prerequisites: Basic knowledge of HTML, CSS, JavaScript, and Bootstrap.
A modal is a dialog box/popup window that is displayed on top of the current page. Modals are used to provide additional information, warnings, or actions in a different window. Bootstrap modals are built with HTML, CSS, and JavaScript.
aria-*
attributes. For instance, the role="dialog"
attribute should be present on the modal itself..modal-title
class in Bootstrap to achieve this..close
class, and you can also close modals by clicking outside the modal or pressing the ESC key.Let's look at a few examples of Bootstrap modals.
<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Open Modal
</button>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal Body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
In the above example, we have a basic modal with a header, body, and footer. The modal is activated by clicking the button.
In this tutorial, we've covered the best practices for using Bootstrap modals, including ensuring accessibility, using clear titles, limiting content, ensuring responsiveness, and providing clear exit methods.
To continue learning about Bootstrap modals, you may wish to explore how to add forms within modals or how to modify modal transition effects.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>