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 Overflow
🌐
MDBootstrap
mdbootstrap.com › standard › modal size
Bootstrap Modal Sizing - free examples & tutorial
Responsive popup window sizing with Bootstrap 5. Modal width, modal height, fullscreen modal, large modal with lg & xl modal classes and more. Modals have three optional sizes, available via modifier classes to be placed on a .modal-dialog.
Top answer
1 of 16
140

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;   
}
2 of 16
117

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).
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-change-the-default-width-of-bootstrap-modal-box.php
How to Change the Default Width of Bootstrap Modal Box
<!DOCTYPE html> <html lang="en"> ...n.js"></script> <script src="js/bootstrap.min.js"></script> <style> @media screen and (min-width: 676px) { .modal-dialog { max-width: 600px; /* New width for default modal */ } } </style> </head> ...
🌐
Bootstrap
getbootstrap.com › docs › 4.2 › components › modal
Modal · Bootstrap
Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more. See this helpful Stack Overflow post for more information. Modals have three optional sizes, available via modifier classes to be placed on a .modal-dialog.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-configure-modal-width-in-bootstrap
How to configure modal width in Bootstrap? | GeeksforGeeks
September 6, 2021 - Bootstrap containers are content holders that are used to align, add margins and padding to your content. Bootstrap has three different container classes, namely: .container .container-fluid .container-{breakpoint} Width of a container varies in accordance with its class. We can create a full width ... Modal Closing Event in Twitter Bootstrap | Set 1 Prerequisite Knowledge: Bootstrap 4 | Modal Twitter Bootstrap’s modal class offer a few events that are fired at the standard Bootstrap modal class.
🌐
CodePen
codepen.io › scottpdawson › pen › nwXqEz
Bootstrap 3 Variable-Width Modal
<h1>Bootstrap 3 Variable-Width Modal</h1> <p>I wanted to override the default Bootstrap modal so it's variable-width, and height-optimized.</p> <p>Add "modal-wide" to the main modal div, and adjust the width in the CSS.
Find elsewhere
🌐
W3Schools
w3schools.com › bootstrap5 › bootstrap_modal.php
Bootstrap 5 Modal
By default, modals are "medium" in size (500px max-width).
🌐
CodePen
codepen.io › cemg › pen › LQrxLV
Bootstrap 4 Modal Auto Height
$(function() { if ($(".modal-autoheight").length > 0) { $(".modal").on("show.bs.modal", resize); $(window).on("resize", resize); } }); function resize() { var winHeight = $(window).height(); $(".modal-autoheight .modal-body").css({ width: "auto", height: (winHeight - 200) + "px" }); }...
🌐
GitHub
github.com › bootstrap-vue › bootstrap-vue › issues › 632
[modal] option to set a width · Issue #632 · bootstrap-vue/bootstrap-vue
July 4, 2017 - This is an enhancement request. There is size option (i.e. sm, md) for the modal component, but it would be great if there is an option to set the exact width / max-width. For example, so that the modal wid...
Author   yhosun
🌐
MDBootstrap
mdbootstrap.com › standard › material design for bootstrap 5 & vanilla javascript
Resize Modal - Material Design for Bootstrap
Hi Alfian, I'm glad you like it, thank you. height property has an auto value, so it will always adjust to the content. If you want to have it with a fixed height, use the code below: .modal { height: 500px; //set a desired number } About the ...
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › components › modal
Modal · Bootstrap v5.0
Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more. See this helpful Stack Overflow post for more information. Modals have three optional sizes, available via modifier classes to be placed on a .modal-dialog.
🌐
GitHub
github.com › mdbootstrap › bootstrap-modal-size
GitHub - mdbootstrap/bootstrap-modal-size: Responsive popup window sizing with Bootstrap 5. Modal width, modal height, fullscreen modal, large modal with lg & xl modal classes and more. · GitHub
Responsive popup window sizing with Bootstrap 5. Modal width, modal height, fullscreen modal, large modal with lg & xl modal classes and more. - mdbootstrap/bootstrap-modal-size
Author   mdbootstrap
Top answer
1 of 2
4

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">&times;</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>

2 of 2
0

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.