Skip to content

Commit b1623c4

Browse files
authored
Add .modal-dialog-centered for optional vertically centered modal (#24510)
* Add .modal-dialog-centered for optional vertically cenetered modal Fixes #23638 * adds modal-dialog-centered class to docs and removes margin to avoid generating a vertical scrolling * mention limitations * fix aria attr * Add `width: 100%` to the .modal-content for the centered version. Adding it here to avoid adding another selector by limiting it to the centered modal modifier.
1 parent fceffd1 commit b1623c4

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

docs/4.0/components/modal.md

+58
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,64 @@ When modals become too long for the user's viewport or device, they scroll indep
208208
</div>
209209
{% endhighlight %}
210210

211+
### Vertically centered
212+
213+
Add `.modal-dialog-centered` to `.modal-dialog` to vertically center the modal. **Do not use this with long modals**—it will overflow the viewport and potentially hide parts of your modal.
214+
215+
<div id="exampleModalCenter" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
216+
<div class="modal-dialog modal-dialog-centered" role="document">
217+
<div class="modal-content">
218+
<div class="modal-header">
219+
<h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
220+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
221+
<span aria-hidden="true">&times;</span>
222+
</button>
223+
</div>
224+
<div class="modal-body">
225+
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
226+
</div>
227+
<div class="modal-footer">
228+
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
229+
<button type="button" class="btn btn-primary">Save changes</button>
230+
</div>
231+
</div>
232+
</div>
233+
</div>
234+
235+
<div class="bd-example">
236+
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
237+
Launch demo modal
238+
</button>
239+
</div>
240+
241+
{% highlight html %}
242+
<!-- Button trigger modal -->
243+
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
244+
Launch demo modal
245+
</button>
246+
247+
<!-- Modal -->
248+
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
249+
<div class="modal-dialog" role="document">
250+
<div class="modal-content">
251+
<div class="modal-header">
252+
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
253+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
254+
<span aria-hidden="true">&times;</span>
255+
</button>
256+
</div>
257+
<div class="modal-body">
258+
...
259+
</div>
260+
<div class="modal-footer">
261+
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
262+
<button type="button" class="btn btn-primary">Save changes</button>
263+
</div>
264+
</div>
265+
</div>
266+
</div>
267+
{% endhighlight %}
268+
211269
### Tooltips and popovers
212270

213271
[Tooltips]({{ site.baseurl }}/docs/{{ site.docs_version }}/components/tooltips/) and [popovers]({{ site.baseurl }}/docs/{{ site.docs_version }}/components/popovers/) can be placed within modals as needed. When modals are closed, any tooltips and popovers within are also automatically dismissed.

scss/_modal.scss

+15
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@
5050
}
5151
}
5252

53+
.modal-dialog-centered {
54+
display: flex;
55+
align-items: center;
56+
height: 100%;
57+
margin-top: 0;
58+
margin-bottom: 0;
59+
}
60+
5361
// Actual modal
5462
.modal-content {
5563
position: relative;
5664
display: flex;
5765
flex-direction: column;
66+
width: 100%; // Ensure `.modal-content` extends the full width of the parent `.modal-dialog`
5867
// counteract the pointer-events: none; in the .modal-dialog
5968
pointer-events: auto;
6069
background-color: $modal-content-bg;
@@ -144,11 +153,17 @@
144153
margin: $modal-dialog-margin-y-sm-up auto;
145154
}
146155

156+
.modal-dialog-centered {
157+
margin-top: 0;
158+
margin-bottom: 0;
159+
}
160+
147161
.modal-content {
148162
@include box-shadow($modal-content-box-shadow-sm-up);
149163
}
150164

151165
.modal-sm { max-width: $modal-sm; }
166+
152167
}
153168

154169
@include media-breakpoint-up(lg) {

0 commit comments

Comments
 (0)