InterviewSolution
Saved Bookmarks
| 1. |
Center the Bootstrap modal vertically and horizontally within the page |
|
Answer» Use the .modal-dialog-centered class to center the Bootstrap modal vertically and horizontally within the page. <div class="modal-dialog modal-dialog-centered">Generally, by DEFAULT, a modal appears like this. You can see it is not vertically and horizontally centered: But, the modal after using the .modal-dialog-centered class will look vertically and horizontally centered: Let us now see an example: <!DOCTYPE html> <html lang="en"> <head> <TITLE>Bootstrap Modal</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <H2>Modal in Bootstrap</h2> <p>Fading effect in Modal...</p> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#newModal"> Open </button> <div class="modal fade" id="newModal"> <div class="modal-dialog modal-dialog-centered"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title">Modal Heading</h4> <button type="button" class="close" data-dismiss="modal">×</button> </div> <div class="modal-body"> Body of the modal... </div> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </body> </html>The above centers the Bootstrap modal vertically and horizontally within the page: |
|