How to vertically center a Bootstrap modal?
要垂直居中Bootstrap模态框,推荐使用Bootstrap内置的.modal-dialog-centered类,该方法适用于Bootstrap 4及以上版本,只需将该类添加到modal-dialog元素即可实现垂直居中;对于Bootstrap 3或需要自定义控制的情况,可采用自定义CSS方法,其中方法一使用Flexbox布局,通过设置display:flex、align-items:center和justify-content:center实现居中,同时调整min-height和margin以确保响应式效果;方法二针对Bootstrap 3使用:before伪元素技巧,将.modal设置为text-align:center,利用:inline-block和vertical-align:middle实现垂直居中;总结:Bootstrap 5和4应使用modal-dialog-centered类,Bootstrap 3则需使用自定义CSS方案,现代项目建议直接使用内置类,简单且无需额外样式。

Centering a Bootstrap modal vertically on the page—especially when you want it perfectly in the middle regardless of screen size—can be tricky because the default behavior only centers it horizontally. Here’s how to vertically center it, using modern and reliable methods.

✅ Method 1: Use Bootstrap’s Built-in Class (Recommended for Bootstrap 4+)
Starting from Bootstrap 4, there's a built-in way to vertically center modals using the .modal-dialog-centered class.
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Title</h5>
<button type="button" class="close" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<div class="modal-body">
<p>Your content here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>✅ Just add
modal-dialog-centeredto themodal-dialogdiv.
This works out of the box and is fully supported in Bootstrap 4 and 5.
✅ Method 2: Custom CSS (Useful for older versions or fine control)
If you're using Bootstrap 3 or want more control, you can override the default styles with custom CSS.

Add this CSS:
.modal-dialog {
display: flex;
align-items: center;
justify-content: center;
min-height: calc(100% - 1rem); /* Adjust for padding/margin */
margin: 0.5rem auto; /* Keeps it responsive */
}
.modal-content {
box-shadow: 0 0.5rem 1rem rgba(0,0,0,0.15);
}
/* Optional: Ensure full height for small content */
.modal {
overflow-y: auto;
}HTML (Bootstrap 3 style):
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal content -->
</div>
</div>
</div>This uses Flexbox to center the modal both vertically and horizontally within the viewport.
✅ Method 3: For Bootstrap 3 (Legacy)
Bootstrap 3 doesn’t have modal-dialog-centered, so you need a workaround.
CSS:
.modal {
text-align: center;
padding: 0 !important; /* Prevent default padding */
}
.modal:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjust spacing */
}
.modal-dialog {
display: inline-block;
text-align: left;
vertical-align: middle;
max-width: 500px;
width: 100%;
}This method uses the
:beforepseudo-element trick to vertically align an inline-block element.
Summary
| Bootstrap Version | Recommended Method |
|---|---|
| Bootstrap 5 | modal-dialog-centered |
| Bootstrap 4 | modal-dialog-centered |
| Bootstrap 3 | Custom CSS with :before or Flexbox override |
For most modern projects, just use
modal-dialog-centered. It's clean, supported, and requires no extra CSS.
Basically, it's not hard—just add one class and you're done.
The above is the detailed content of How to vertically center a Bootstrap modal?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
What should I do if the modal in react does not take effect?
Dec 30, 2022 am 09:22 AM
Solution to modal in react not taking effect: 1. Remove "<React.StrictMode>" in the "main.jsx" file; 2. Where the modal component is used, add two attributes "transitionName="" maskTransitionName to Modal ="""; 3. Upgrade "ant-design".
How to create a modal or dialog component in Vue?
Aug 02, 2025 am 03:00 AM
Create the Modal.vue component, use the Composition API to define the props that receive modelValue and title, and use emit to trigger the update:modelValue event to achieve v-model bidirectional binding; 2. Use slot to distribute content in the template, supporting the default slot and named slot header and footer; 3. Use @click.self to close the pop-up window by clicking the mask layer; 4. Import the Modal in the parent component and use ref to control the display and hide it, and use it in combination with v-model; 5. Optional enhancements include listening to the Escape key close, adding transition animation and focus lock. This modal box component has good
css modal popup example
Jul 28, 2025 am 04:33 AM
Use pure CSS to implement modal pop-up windows to control the visible and hidden checkbox. 1. Use input[type="checkbox"] as the status switch; 2. Use: checked .modal to control the display of modal boxes; 3. Use label[for] to trigger checking to achieve opening and closing; 4. Add @keyframes animation to achieve fade-in pop-up effect; 5. The close button or mask click area in the modal box can be bound to label control hidden. The entire process does not require JavaScript, is very compatible and has strong accessibility, and is suitable for static pages or lightweight interactive scenarios.
Explain how to create a modal with CSS
Jul 20, 2025 am 01:04 AM
To create a modal box, HTML structure requires that it includes a mask layer, content area and close button; 2. CSS is used for style design, and key points include full-screen hiding of the mask layer, content centering and close button positioning; 3. JavaScript controls display and hide, and supports clicking to close; 4. Add transition animation to improve the experience. Use HTML to define structures, CSS to set styles to achieve visual effects, JavaScript to manage interaction logic, and enhance user experience by adding animations.
How to create a modal with CSS
Aug 12, 2025 pm 12:48 PM
To create a modal box, you need to use HTML to build a structure, including trigger buttons, modal box containers and content areas; 2. Set the fixed positioning, hierarchy, background mask and centering style of the modal box through CSS, and use display:none to hide by default; 3. Use JavaScript to control display and hide, set display to flex when clicking the button, and restore to none when clicking the mask or close button; 4. You can improve user experience and accessibility by adding transition animations, aria attributes, keyboard events and prohibiting background scrolling. The entire implementation relies on the combination of HTML structure, CSS visual style and a small amount of JavaScript interactive logic to finally form a modal box with complete functions.
How to vertically center a Bootstrap modal?
Sep 12, 2025 am 01:47 AM
To vertically center the Bootstrap modal box, it is recommended to use the .modal-dialog-centered class built in Bootstrap. This method is suitable for Bootstrap4 and above. Just add the class to the modal-dialog element to achieve vertical centering. For Bootstrap3 or when custom control is required, you can use a custom CSS method. In the first place, use the Flexbox layout, and set the display:flex, align-items:center and justify-content:center, and adjust the min-height and margin to ensure that
How to open a Bootstrap modal with a button click?
Nov 17, 2025 am 12:56 AM
To open a Bootstrap modal box by clicking a button, you need to set the HTML structure of the modal box and use the data attribute or JavaScript to trigger it. 1. Use data attributes: add data-bs-toggle="modal" and data-bs-target="#modalId" on the button (Bootstrap4 uses data-toggle and data-target); 2. Use JavaScript: get the modal instance through bootstrap.Modal.getOrCreateInstance() and call the .show() method
How to create a modal or popup box with JavaScript
Oct 04, 2025 am 02:18 AM
Create an HTML structure, including a modal box container and a trigger button; 2. Hide and center the modal box with CSS, add a background mask; 3. Control display and hide through JavaScript, and respond to click events.





