How to Create a basic Modal Component in Bootstrap?


Modal windows are a popular feature in modern web development that enables web designers to display additional content or user input options without redirecting the user to a new page.

Algorithm

Step 1: Set up the HTML Structure

Step 2: Modify the Modal Window

Step 3: Modify the Modal Body

Step 4: Personalize the Modal Body

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Bootstrap Modal Example</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

  <div class="container mt-5">
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
      Open Modal
    </button>
    
    <div class="modal" id="myModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">My Modal</h5>
            <button type="button" class="close" data-dismiss="modal">×</button>
          </div>
          <div class="modal-body">
            <p>This is our modal body. You can add any content you want here.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>

</body>
</html>

Conclusion

We covered how to make a basic modal component in Bootstrap and how to change its header, body, and footer in this article. Modals are a helpful web design element because they allow you to display material in a distinct layer on top of the page, eliminating the need for a new page or a pop−up window. Modals may be used to display messages, forms, photos, videos, or any other sort of material in a user−friendly manner.

Updated on: 18-Aug-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements