Is it possible to customize style of bootstrap $modal
Categories:
Customizing Bootstrap Modals: A Comprehensive Guide

Learn how to effectively customize the appearance and behavior of Bootstrap modals using CSS, JavaScript, and Bootstrap's built-in options.
Bootstrap modals are powerful and flexible components for displaying dialogs, alerts, or custom content on top of the current page. While they come with a clean, default design, most projects require some level of customization to match branding guidelines or specific UI/UX requirements. This article will guide you through various techniques to style your Bootstrap modals, from simple CSS overrides to more advanced JavaScript manipulations.
Understanding Bootstrap Modal Structure
Before diving into customization, it's crucial to understand the basic HTML structure of a Bootstrap modal. A typical modal consists of a few key elements, each with specific classes that you can target for styling. The main container is .modal
, which includes the backdrop. Inside, .modal-dialog
controls the modal's positioning and size. The actual content is within .modal-content
, which further breaks down into .modal-header
, .modal-body
, and .modal-footer
.
graph TD A[Modal Container (.modal)] --> B[Modal Dialog (.modal-dialog)] B --> C[Modal Content (.modal-content)] C --> D[Modal Header (.modal-header)] C --> E[Modal Body (.modal-body)] C --> F[Modal Footer (.modal-footer)] D --> G[Modal Title (.modal-title)] D --> H[Close Button (data-bs-dismiss="modal")]
Bootstrap Modal HTML Structure
<div class="modal fade" id="myCustomModal" tabindex="-1" aria-labelledby="myCustomModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="myCustomModalLabel">Custom Modal Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
This is the custom modal body content.
</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>
Basic Bootstrap 5 Modal Structure
Customizing with CSS Overrides
The most straightforward way to customize Bootstrap modals is by overriding its default CSS styles. You can target specific classes or add your own custom classes to the modal elements. Remember to place your custom CSS after Bootstrap's CSS to ensure your styles take precedence. Using more specific selectors or !important
(sparingly) can help, but generally, adding custom classes is a cleaner approach.
/* Targeting specific Bootstrap classes */
.modal-content {
background-color: #f8f9fa; /* Light gray background */
border-radius: 15px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
.modal-header {
background-color: #007bff; /* Blue header */
color: white;
border-bottom: 1px solid #0056b3;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
}
.modal-title {
font-weight: bold;
font-size: 1.25rem;
}
.modal-body {
padding: 20px;
color: #333;
}
.modal-footer {
background-color: #e9ecef;
border-top: 1px solid #dee2e6;
border-bottom-left-radius: 15px;
border-bottom-right-radius: 15px;
}
/* Using a custom class for a specific modal */
.my-custom-modal .modal-dialog {
max-width: 800px; /* Wider modal */
}
.my-custom-modal .modal-header {
background-color: #28a745; /* Green header for this specific modal */
}
CSS overrides for Bootstrap modal styles
Modifying Modal Size and Position
Bootstrap provides utility classes to easily change the size of your modals. For more granular control over positioning or custom sizes, you'll need to use CSS. The .modal-dialog
element is key here, as it controls the modal's dimensions and alignment within the viewport.
Bootstrap 5 offers the following size classes for .modal-dialog
:
1. Small Modal
Add modal-sm
to .modal-dialog
for a smaller modal.
2. Large Modal
Add modal-lg
to .modal-dialog
for a larger modal.
3. Extra Large Modal
Add modal-xl
to .modal-dialog
for an extra-large modal.
4. Full-width Modal
Add modal-fullscreen
to .modal-dialog
to make the modal span the entire viewport.
<!-- Example of a large modal -->
<div class="modal-dialog modal-lg">
...
</div>
<!-- Example of a full-screen modal -->
<div class="modal-dialog modal-fullscreen">
...
</div>
Using Bootstrap's built-in size classes
For custom widths or vertical alignment, you can target .modal-dialog
directly with CSS:
.modal-dialog.custom-width {
max-width: 90%; /* Custom width */
width: auto; /* Allow content to dictate width up to max-width */
}
.modal-dialog.modal-top {
margin-top: 50px; /* Position modal closer to the top */
}
.modal-dialog.modal-center {
display: flex;
align-items: center;
min-height: calc(100% - 1rem); /* Center vertically */
}
Custom CSS for modal width and vertical alignment
Dynamic Content and JavaScript Customization
Sometimes, styling isn't enough; you might need to dynamically change modal content or behavior based on user interaction. Bootstrap modals provide several events that you can hook into using JavaScript to perform actions before, during, or after the modal is shown or hidden.
sequenceDiagram participant User participant Button participant Modal User->>Button: Clicks button to open modal Button->>Modal: Triggers 'show.bs.modal' event Modal->>Modal: Prepares content (e.g., AJAX call) Modal->>Modal: Triggers 'shown.bs.modal' event Modal-->>User: Modal is visible User->>Button: Clicks close button Button->>Modal: Triggers 'hide.bs.modal' event Modal->>Modal: Cleans up content Modal->>Modal: Triggers 'hidden.bs.modal' event Modal-->>User: Modal is hidden
Bootstrap Modal Event Lifecycle
const myModal = document.getElementById('myCustomModal');
myModal.addEventListener('show.bs.modal', event => {
// Do something before the modal is shown
const button = event.relatedTarget; // Button that triggered the modal
const recipient = button.getAttribute('data-bs-whatever'); // Extract info from data-* attributes
const modalTitle = myModal.querySelector('.modal-title');
const modalBodyInput = myModal.querySelector('.modal-body');
modalTitle.textContent = `New message to ${recipient}`;
modalBodyInput.textContent = `Preparing content for ${recipient}...`;
// Example: Fetch dynamic content via AJAX
// fetch(`/api/data?user=${recipient}`)
// .then(response => response.json())
// .then(data => {
// modalBodyInput.textContent = data.message;
// });
});
myModal.addEventListener('shown.bs.modal', event => {
// Do something once the modal is fully visible
console.log('Modal is now fully open!');
});
myModal.addEventListener('hidden.bs.modal', event => {
// Do something after the modal is completely hidden
console.log('Modal has been closed and is hidden.');
// Clean up any dynamic content or form fields here
const modalTitle = myModal.querySelector('.modal-title');
const modalBodyInput = myModal.querySelector('.modal-body');
modalTitle.textContent = 'Custom Modal Title'; // Reset title
modalBodyInput.textContent = 'This is the custom modal body content.'; // Reset body
});
JavaScript event listeners for dynamic modal content