Introduction
This article will demonstrate you modal dialogs in bootstrap 4. The Modal component is a dialog box/popup window that is displayed on top of the current page.
Important points to be remember before using bootstrap 4 modal dialogs.
- Modals are built with HTML, CSS, and JavaScript. They’re positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead.
- Clicking on the modal “backdrop” will automatically close the modal.
- Bootstrap only supports one modal window at a time. Nested modals aren’t supported.
- Modals use position: fixed, which can sometimes be a bit particular about its rendering. Whenever possible, place your modal HTML in a top-level position to avoid potential interference from other elements. You’ll likely run into issues when nesting a .modal within another fixed element.
- Once again, due to position: fixed, there are some caveats with using modals on mobile devices.
- Due to how HTML5 defines its semantics, the autofocus HTML attribute has no effect in Bootstrap modals.
To achieve autofocus effect, use some custom JavaScript,
- <script type="text/javascript">
- $('#myModal').on('shown.bs.modal', function () {
- $('#myInput').trigger('focus')
- })
- </script>
To use bootstrap 4 modal in your project. You need have following downloaded or cdn link scripts.
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
Modal CSS Classes
.modal
|
Creates a modal
|
.modal-content
|
Styles the modal properly with border, background-color, etc. Use this class to add the modal's header, body, and footer
|
.modal-dialog-centered
|
Centers the modal vertically and horizontally within the page
|
.modal-header
|
Defines the style for the header of the modal
|
.modal-body
|
Defines the style for the body of the modal
|
.modal-footer
|
Defines the style for the footer in the modal. This area is right-aligned by default. To change this, add the justify-content-start or justify-content-center together with the .modal-footer class
|
.modal-sm
|
Specifies a small modal
|
.modal-lg
|
Specifies a large modal
|
.fade
|
Adds an animation/transition effect which fades the modal in and out
|
Trigger the Modal Via data-* Attributes
Add data-toggle="modal" and data-target="#modalID" to any element.
Note
For <a> elements, omit data-target, and use href="#modalID" instead
Modal with Overlay Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>Modal</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- <script type="text/javascript">
- (function () {
- 'use strict';
- window.addEventListener('load', function () {
- var form = document.getElementById('needs-validation');
- form.addEventListener('submit', function (event) {
- if (form.checkValidity() === false) {
- event.preventDefault();
- event.stopPropagation();
- }
- form.classList.add('was-validated');
- }, false);
- }, false);
- })();
- </script>
- </head>
- <body>
- <div class="container py-5">
- <h4 class="text-center text-uppercase">Bootstrap 4 modal</h4>
- <button type="button" class="btn btn-primary rounded-0" data-toggle="modal" data-target="#Employee">Add New Employee</button>
- <div class="modal fade" id="Employee">
- <div class="modal-dialog">
- <div class="modal-content">
- <form id="needs-validation" novalidate>
- <div class="modal-header">
- <h5 class="modal-title text-uppercase text-center">Employee Information</h5>
- </div>
- <div class="modal-body">
-
- <div class="form-row">
- <div class="col-sm-6 col-md-6 col-xs-12">
- <label for="firstName">First Name</label>
- <input id="firstName" type="text" placeholder="First Name" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter first name.
- </div>
- </div>
- <div class="col-sm-6 col-md-6 col-xs-12">
- <label for="lastName">Last Name</label>
- <input id="lastName" type="text" placeholder="Last Name" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter last name.
- </div>
- </div>
- </div>
- <div class="form-row">
- <div class="col-sm-4 col-md-4 col-xs-12">
- <label for="city">City</label>
- <input id="city" type="text" placeholder="City" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter city.
- </div>
- </div>
- <div class="col-sm-4 col-md-4 col-xs-12">
- <label for="state">State</label>
- <input id="state" type="text" placeholder="State" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter state.
- </div>
- </div>
- <div class="col-sm-4 col-md-4 col-xs-12">
- <label for="country">State</label>
- <input id="country" type="text" placeholder="Country" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter country.
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-danger rounded-0" data-dismiss="modal">Cancel</button>
- <button type="submit" class="btn btn-primary rounded-0">Register</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Output
Screenshot-1
Screenshot-2
Modal without Overlay Example
Note
You cannot click outside of this modal to close it.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Modal</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- <script type="text/javascript">
- (function () {
- 'use strict';
- window.addEventListener('load', function () {
- var form = document.getElementById('needs-validation');
- form.addEventListener('submit', function (event) {
- if (form.checkValidity() === false) {
- event.preventDefault();
- event.stopPropagation();
- }
- form.classList.add('was-validated');
- }, false);
- }, false);
- })();
- </script>
- </head>
- <body>
- <div class="container py-5">
- <h4 class="text-center text-uppercase">Bootstrap 4 modal</h4>
- <button type="button" class="btn btn-primary rounded-0" data-backdrop="false" data-toggle="modal" data-target="#Employee">Add New Employee</button>
- <div class="modal fade" id="Employee" role="dialog">
- <div class="modal-dialog">
- <div class="modal-content">
- <form id="needs-validation" novalidate>
- <div class="modal-header">
- <h5 class="modal-title text-uppercase text-center">Employee Information</h5>
- <button type="button" class="close" data-dismiss="modal">×</button>
- </div>
- <div class="modal-body">
-
- <div class="form-row">
- <div class="col-sm-6 col-md-6 col-xs-12">
- <label for="firstName">First Name</label>
- <input id="firstName" type="text" placeholder="First Name" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter first name.
- </div>
- </div>
- <div class="col-sm-6 col-md-6 col-xs-12">
- <label for="lastName">Last Name</label>
- <input id="lastName" type="text" placeholder="Last Name" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter last name.
- </div>
- </div>
- </div>
- <div class="form-row">
- <div class="col-sm-4 col-md-4 col-xs-12">
- <label for="city">City</label>
- <input id="city" type="text" placeholder="City" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter city.
- </div>
- </div>
- <div class="col-sm-4 col-md-4 col-xs-12">
- <label for="state">State</label>
- <input id="state" type="text" placeholder="State" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter state.
- </div>
- </div>
- <div class="col-sm-4 col-md-4 col-xs-12">
- <label for="country">State</label>
- <input id="country" type="text" placeholder="Country" class="form-control" aria-describedby="inputGroupPrepend" required />
- <div class="invalid-feedback">
- Please enter country.
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-danger rounded-0" data-dismiss="modal">Cancel</button>
- <button type="submit" class="btn btn-primary rounded-0">Register</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Output
Screenshot 1
Screenshot 2