How to resize Twitter Bootstrap modal dynamically based on the content

JavascriptJqueryHtmlCssTwitter Bootstrap

Javascript Problem Overview


I have database content which has different types of data, such as Youtube videos, Vimeo videos, text, Imgur pictures, etc. All of them have different heights and widths. All I have found while searching the Internet is changing the size to only one parameter. It has to be same as the content in the popup.

This is my HTML code. I also use Ajax to call the content.

<div id="modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="ModalLabel"></h3>
    </div>
    <div class="modal-body">

    </div>
</div> 

Javascript Solutions


Solution 1 - Javascript

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;	
}

Solution 2 - Javascript

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).

Solution 3 - Javascript

I couldn't get PSL's answer working for me so I found all I have to do is set the div holding the modal content with style="display: table;". The modal content itself says how big it wants to be and the modal accommodates it.

Solution 4 - Javascript

There are many ways to do this if you want to write css then add following

.modal-dialog{
  display: table
}

In case if you want to add inline

<div class="modal-dialog" style="display:table;">
//enter code here
</div>

Don't add display:table; to modal-content class. it done your job but disposition your modal for large size see following screenshots. first image is if you add style to modal-dialog In case if you add style to modal-dialog if you add style to modal-content. it looks dispositioned. enter image description here

Solution 5 - Javascript

for bootstrap 3 use like

$('#myModal').on('hidden.bs.modal', function () {
// do something…
})

Solution 6 - Javascript

Simple Css work for me

.modal-dialog { 
max-width : 100% ;
}

Solution 7 - Javascript

I simply override the css:

.modal-dialog {
	max-width: 1000px;
}

Solution 8 - Javascript

set width:fit-content on the modal div.

Solution 9 - Javascript

You can do that if you use jquery dialog plugin from gijgo.com and set the width to auto.

$("#dialog").dialog({
  uiLibrary: 'bootstrap',
  width: 'auto'
});

<html>
<head>
  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
  <link href="http://code.gijgo.com/1.0.0/css/gijgo.css" rel="stylesheet" type="text/css">
  <script src="http://code.gijgo.com/1.0.0/js/gijgo.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
 <div id="dialog" title="Wikipedia">
   <img src="https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1122px-Wikipedia-logo-v2.svg.png" width="320"/>
 </div>
</body>
</html>

You can also allow the users to control the size if you set resizable to true. You can see a demo about this at http://gijgo.com/Dialog/Demos/bootstrap-modal-resizable

Solution 10 - Javascript

A simple CSS solution that will vertically and horizontally center the modal:

.modal.show {
  display: flex !important;
  justify-content: center;
}

.modal-dialog {
  align-self: center;
}

Solution 11 - Javascript

For Bootstrap 2 Auto adjust model height dynamically

  //Auto adjust modal height on open 
  $('#modal').on('shown',function(){
     var offset = 0;
	 $(this).find('.modal-body').attr('style','max-height:'+($(window).height()-offset)+'px !important;');
  });

Solution 12 - Javascript

I had the same problem with bootstrap 3 and the modal-body div's height not wanting to be greater than 442px. This was all the css needed to fix it in my case:

.modal-body {
    overflow-y: auto;
}

Solution 13 - Javascript

Mine Solution was just to add below style. <div class="modal-body" style="clear: both;overflow: hidden;">

Solution 14 - Javascript

As of Bootstrap 4 - it has a style of:

@media (min-width: 576px)
.modal-dialog {
    max-width: 500px;
}

so if you are looking to resize the modal width to some-width larger, I would suggest using this in your css for example:

.modal-dialog { max-width: 87vw; }

Note that setting width: 87vw; will not override bootstrap's max-width: 500px;.

Solution 15 - Javascript

My search led me here so might as well add my two cents worth of idea. If you are using Monkey friendly Twitter Bootstrap modal then you can do something like this:

    var dialog = new BootstrapDialog({
		id: 'myDialog',
		title: 'Title',
		closable: false,
		// whatever you like configuration here...
	});

	dialog.realize();
	if (isContentAYoutubeVideo()) {
		dialog.getModalDialog().css('width', '80%'); // set the width to whatever you like
	}
	dialog.open();

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErdem EceView Question on Stackoverflow
Solution 1 - JavascriptAmit ShahView Answer on Stackoverflow
Solution 2 - JavascriptPSLView Answer on Stackoverflow
Solution 3 - JavascriptLuminousView Answer on Stackoverflow
Solution 4 - Javascriptwaqas aliView Answer on Stackoverflow
Solution 5 - Javascriptuser2678868View Answer on Stackoverflow
Solution 6 - JavascriptPascalView Answer on Stackoverflow
Solution 7 - JavascriptAndrew GaleView Answer on Stackoverflow
Solution 8 - JavascriptSubhashis PalView Answer on Stackoverflow
Solution 9 - JavascriptAtanas AtanasovView Answer on Stackoverflow
Solution 10 - JavascriptKirk RossView Answer on Stackoverflow
Solution 11 - JavascriptSudarPView Answer on Stackoverflow
Solution 12 - JavascriptBrett DrakeView Answer on Stackoverflow
Solution 13 - JavascriptFurquanView Answer on Stackoverflow
Solution 14 - JavascriptZulkifilView Answer on Stackoverflow
Solution 15 - JavascriptjpllosaView Answer on Stackoverflow