inline-block elements get dynamic width regarding their content.
body .modal-dialog { /* Width */
max-width: 100%;
width: auto !important;
display: inline-block;
}
Note: width: auto !important; is required to overwrite bootstrap css.
Finally to place it in middle of viewport you to display: flex; on the parent element
.modal {
z-index: -1;
display: flex !important;
justify-content: center;
align-items: center;
}
.modal-open .modal {
z-index: 1050;
}
Answer from tmg on Stack Overflowinline-block elements get dynamic width regarding their content.
body .modal-dialog { /* Width */
max-width: 100%;
width: auto !important;
display: inline-block;
}
Note: width: auto !important; is required to overwrite bootstrap css.
Finally to place it in middle of viewport you to display: flex; on the parent element
.modal {
z-index: -1;
display: flex !important;
justify-content: center;
align-items: center;
}
.modal-open .modal {
z-index: 1050;
}
@tmg's answer will only work for Bootstrap 3. For Bootstrap 4, use this:
.modal {
text-align: center;
}
Alongside his suggestion:
.modal-dialog {
text-align: left; /* you'll likely want this */
max-width: 100%;
width: auto !important;
display: inline-block;
}
Applying width: auto; to the .modal-dialog element should do it.
.modal-dialog {
width: auto;
max-width: 960px; // Optional, maximum width
}
JSFiddle Working Example
Based on content modal width auto adjusted, plz followed by below code:
.modal-dialog{
margin: auto;
max-width: none;
width: fit-content;
}
This worked for me, none of the above worked.
.modal-dialog{
position: relative;
display: table; /* This is important */
overflow-y: auto;
overflow-x: auto;
width: auto;
min-width: 300px;
}
Since your content must be dynamic you can set the css properties of the modal dynamically on show event of the modal which will re-size the modal overriding its default specs. Reason being bootstrap applies a max-height to the modal body with the css rule as below:
.modal-body {
position: relative;
overflow-y: auto;
max-height: 400px;
padding: 15px;
}
So you can add inline styles dynamically using jquery css method:
For newer versions of bootstrap use show.bs.modal
$('#modal').on('show.bs.modal', function () {
$(this).find('.modal-body').css({
width:'auto', //probably not needed
height:'auto', //probably not needed
'max-height':'100%'
});
});
For older versions of bootstrap use show
$('#modal').on('show', function () {
$(this).find('.modal-body').css({
width:'auto', //probably not needed
height:'auto', //probably not needed
'max-height':'100%'
});
});
or use a css rule to override:
.autoModal.modal .modal-body{
max-height: 100%;
}
and add this class autoModal to your target modals.
Change the content dynamically in the fiddle, you will see the modal getting resized accordingly. Demo
Newer version of bootstrap see the available event names.
- show.bs.modal This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.
- shown.bs.modal This event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event.
- hide.bs.modal This event is fired immediately when the hide instance method has been called.
- hidden.bs.modal This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
- loaded.bs.modal This event is fired when the modal has loaded content using the remote option.
Older version of bootstrap modal events supported.
- Show - This event fires immediately when the show instance method is called.
- shown - This event is fired when the modal has been made visible to the user (will wait for css transitions to complete).
- hide - This event is fired immediately when the hide instance method has been called.
- hidden - This event is fired when the modal has finished being hidden from the user (will wait for css transitions to complete).
In your code, for the modal-dialog div, add another class, modal-lg:
<div class="modal-dialog modal-lg">
Or if you wanna centre your modal dialog, use:
.modal-ku {
width: 750px;
margin: auto;
}
The easiest way can be inline style on modal-dialog div :
<div class="modal" id="myModal">
<div class="modal-dialog" style="width:1250px;">
<div class="modal-content">
...
</div>
</div>
</div>
This worked for me
.modal-dialog,
.modal-content {
/* 80% of window height */
height: 80%;
}
.modal-body {
/* 100% = dialog height, 120px = header + footer */
max-height: calc(100% - 120px);
overflow-y: scroll;
}
Fiddle: http://jsfiddle.net/mehmetatas/18dpgqpb/2/
Instead of using a %, the units vh set it to a percent of the viewport (browser window) size.
I was able to set a modal with an image and text beneath to be responsive to the browser window size using vh.
If you just want the content to scroll, you could leave out the part that limits the size of the modal body.
/*When the modal fills the screen it has an even 2.5% on top and bottom*/
/*Centers the modal*/
.modal-dialog {
margin: 2.5vh auto;
}
/*Sets the maximum height of the entire modal to 95% of the screen height*/
.modal-content {
max-height: 95vh;
overflow: scroll;
}
/*Sets the maximum height of the modal body to 90% of the screen height*/
.modal-body {
max-height: 90vh;
}
/*Sets the maximum height of the modal image to 69% of the screen height*/
.modal-body img {
max-height: 69vh;
}
Always have handy the un-minified CSS for bootstrap so you can see what styles they have on their components, then create a CSS file AFTER it, if you don't use LESS and over-write their mixins or whatever
This is the default modal css for 768px and up:
@media (min-width: 768px) {
.modal-dialog {
width: 600px;
margin: 30px auto;
}
...
}
They have a class modal-lg for larger widths
@media (min-width: 992px) {
.modal-lg {
width: 900px;
}
}
If you need something twice the 600px size, and something fluid, do something like this in your CSS after the Bootstrap css and assign that class to the modal-dialog.
@media (min-width: 768px) {
.modal-xl {
width: 90%;
max-width:1200px;
}
}
HTML
<div class="modal-dialog modal-xl">
Demo: http://jsbin.com/yefas/1
If you need this solution for only few types of modals just use
style="width:90%" attribute.
example:
div class="modal-dialog modal-lg" style="width:90%"
note: this will change only this particular modal
use dispaly:table for your .modal-content
JsFiddle
var modalItems = document.getElementById('itemsModal');
var btnItems = document.getElementById("btn_items");
var btnCloseModalItems = document.getElementById("btn_closeItemsModal");
btnItems.onclick = function() {
modalItems.style.display = "block";
}
btnCloseModalItems.onclick = function() {
modalItems.style.display = "none";
}
#btn_closeItemsModal {
float: right;
}
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.8);
}
.close {
color: white;
background-color: white;
}
/* Modal Content */
.modal-content {
position: relative;
color: black;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: auto;
display: table;
}
.modal-header {
background-color: #0099cc;
color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div id="itemsModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="btn btn-default btn-lg" id="btn_closeItemsModal"><span aria-hidden="true">×</span></button>
<h3>Items</h3>
</div>
<div class="modal-body">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action">Africa</a>
<a href="#" class="list-group-item list-group-item-action">Antarctica</a>
<a href="#" class="list-group-item list-group-item-action">Asia</a>
<a href="#" class="list-group-item list-group-item-action">Eurpoe</a>
<a href="#" class="list-group-item list-group-item-action">North America</a>
<a href="#" class="list-group-item list-group-item-action">Oceania</a>
<a href="#" class="list-group-item list-group-item-action">South America</a>
</div>
</div>
</div>
</div>
<button class="btn-lg btn-primary" id="btn_items">My Items</button>
As you know that by default almost all elements(like <div> and <p> and <h1> etc) are having display:block CSS property so such all elements will take the full width of parent. So if you won't define any width for .modal-content and . .modal then due to display:block it will inherit the width of parent.
You should add a max-width constrain to your .modal-content to achieve this. Also you can add margin:0 auto to align the .modal-contentcenter.