Bootstrap Modal Example on CodePen

Ruth Jul 09, 2026

Bootstrap, a popular CSS framework, simplifies responsive web development by providing pre-defined classes for common styles and components. One of these components is the modal, a dialog box that appears on top of the current page. Here, we'll explore a Bootstrap modal example using CodePen, a web development editor that allows you to write HTML, CSS, and JavaScript in one interface.

an image of a computer screen with text
an image of a computer screen with text

Before diving into the CodePen example, let's briefly discuss what a Bootstrap modal is and why it's useful. A Bootstrap modal is a pop-up window that appears when triggered by JavaScript. It's useful for displaying additional content, forms, or messages without disrupting the current page's flow. Now, let's create a simple Bootstrap modal using CodePen.

GoSnippets — Free Code Snippets for Bootstrap, Tailwind, React, CSS, JavaScript & more
GoSnippets — Free Code Snippets for Bootstrap, Tailwind, React, CSS, JavaScript & more

Setting Up the Bootstrap Modal in CodePen

To start, sign up for a free CodePen account and create a new Pen. In the HTML panel, add the following code to set up a basic Bootstrap modal:

Minimal Modal Popup HTML & CSS - Code Snippet
Minimal Modal Popup HTML & CSS - Code Snippet

<div class="modal fade" id="myModal" 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>

This code creates a modal with a title, body, and footer. The `fade` class makes the modal fade in and out, while the `modal-dialog` and `modal-content` classes set the modal's size and style. The `modal-header`, `modal-body`, and `modal-footer` classes divide the modal into sections.

Bootstrap 5 Modal Form
Bootstrap 5 Modal Form

Adding Content to the Modal

Now, let's add some content to the modal's body. In the HTML panel, update the `modal-body` section:

<div class="modal-body"> <p>This is a simple Bootstrap modal created using CodePen.</p> <p>You can add any content you like here, such as text, images, or forms.</p> </div>

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

Here, we've added two paragraphs to the modal's body. You can replace this content with your desired information.

Triggering the Modal

To make the modal appear, we need to trigger it using JavaScript. In the JavaScript panel, add the following code to trigger the modal when a button is clicked:

Upload Modal x MetricBooks
Upload Modal x MetricBooks

<script> const myModal = new bootstrap.Modal(document.getElementById('myModal')); document.getElementById('showModalBtn').addEventListener('click', () => { myModal.show(); }); </script>

This code creates a new `Modal` instance and adds a click event listener to a button with the ID `showModalBtn`. When the button is clicked, the modal is shown.

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Upgrade To Premium Modal | Free Webflow Component
Upgrade To Premium Modal | Free Webflow Component
#CSS Animations Tips and Tricks
#CSS Animations Tips and Tricks
Bootstrap Fieldset Legend: How To + Examples + Template | AZMIND
Bootstrap Fieldset Legend: How To + Examples + Template | AZMIND
Pure CSS3 Modal Example - Code Snippet
Pure CSS3 Modal Example - Code Snippet
Bootstrap Photo Gallery
Bootstrap Photo Gallery
Image Upload Modal | Free Webflow Component
Image Upload Modal | Free Webflow Component
Responsive Delete Confirmation Modal Popup UI Design (HTML, CSS, JavaScript) – Free Download
Responsive Delete Confirmation Modal Popup UI Design (HTML, CSS, JavaScript) – Free Download
an iphone screen with the message set up a strong password
an iphone screen with the message set up a strong password
Bootstrap 5 Classes List Cheat Sheet
Bootstrap 5 Classes List Cheat Sheet
HTML Forms Cheat Sheet for Beginners — Simple Guide
HTML Forms Cheat Sheet for Beginners — Simple Guide
an image of a computer screen with text
an image of a computer screen with text
Bootstrap 4 Cheat Sheet - The ultimate list of Bootstrap classes
Bootstrap 4 Cheat Sheet - The ultimate list of Bootstrap classes
Modal that initiates from origin
Modal that initiates from origin
BOOTSTRAP INPUT FORM (2020) | HOW TO MAKE FORM IN BOOTSTRAP
BOOTSTRAP INPUT FORM (2020) | HOW TO MAKE FORM IN BOOTSTRAP
📝 Create Forms in HTML
📝 Create Forms in HTML
Semantic HTML Explained Visually (Beginner Guide)
Semantic HTML Explained Visually (Beginner Guide)
Codepen Team Member List - Even/Odd Styles
Codepen Team Member List - Even/Odd Styles
Box animation effect using html and css
Box animation effect using html and css

Customizing the Bootstrap Modal

Now that we have a basic Bootstrap modal set up, let's discuss some customization options to make it more engaging and useful.

One way to customize the modal is by changing its size. By default, the modal is 500px wide and 400px tall. You can adjust these values by changing the `modal-dialog` class. For example, to make the modal full-width, use the `modal-dialog modal-dialog-centered` classes:

<div class="modal-dialog modal-dialog-centered">

Adding a Background Overlay

Another customization option is adding a background overlay to the modal. This can be done by adding the `modal-dialog-centered` class to the `modal` div and setting the `modal-open` class on the `body` element:

<div class="modal fade modal-dialog-centered" id="myModal"> ... </div> <script> document.getElementById('showModalBtn').addEventListener('click', () => { myModal.show(); document.body.classList.add('modal-open'); }); </script>

This adds a semi-transparent overlay behind the modal, making it stand out from the rest of the page.

Adding a Data Attribute to the Trigger Element

Instead of using JavaScript to trigger the modal, you can use a data attribute on the trigger element. Update the trigger button's HTML:

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Show Modal</button>

Here, we've added `data-bs-toggle="modal"` and `data-bs-target="#myModal"` attributes to the button. When the button is clicked, the modal with the ID `myModal` is shown automatically.

That's it! You now have a customizable Bootstrap modal set up in CodePen. Experiment with different styles, sizes, and content to create the perfect modal for your project. Happy coding!