Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Modal Dynamic Width

I am trying to define a bootstrap modal that gets a dynamic width regarding its content and if necessary a vertical scrollbar.

The vertical scrollbar works but the horizontal width seems to be a fixed width. Could you please help?

Modal:

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header"><h4 class="modal-title">Dynamic Modal Title</h4></div>
      <div class="modal-body">Dynamic Modal Body</div>
      <div class="modal-footer">Dynamic Modal Footer</div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

CSS:

.modal .modal-body { /* Vertical scrollbar if necessary */
    max-height: 480px;
    overflow-y: auto;
}

body .modal-dialog { /* Width */
    max-width: 100%;
}
like image 672
njlqay Avatar asked Dec 16 '15 08:12

njlqay


People also ask

How do I change the width of a bootstrap modal?

Change the size of the modal by adding the . modal-sm class for small modals, . modal-lg class for large modals, or . modal-xl for extra large modals.

How can I make my modal height responsive?

Making your Modal Responsive The most obvious way is to write a bunch of media queries like a chump. I instead suggest using max-width and max-height. When combined with calc() this allows you to have a consistent padding between the modal and the edge of the screen on smaller devices.


Video Answer


4 Answers

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;
}
like image 161
tmg Avatar answered Sep 17 '22 07:09

tmg


fit-content worked well for me with no ill side effects like a misplaced modal.

.modal {
    padding: 1%;
}
.modal-lg {
    min-width: auto;
    max-width: fit-content;
}
like image 33
Richard Ayotte Avatar answered Sep 19 '22 07:09

Richard Ayotte


@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;
}
like image 36
Vael Victus Avatar answered Sep 17 '22 07:09

Vael Victus


Works in Bootstrap 4-

Here I have added min width to 300px, max width to 90vw, modal can be scrolled vertically using screen scrollbar, and horizontal scrollbar shows up on modal if content width is > 90vw

.modal-dialog {
  position: relative;
  display: table;
  overflow: auto;
  width: auto;
  min-width: 300px;
}
.modal-body { /* Restrict Modal width to 90% */
  overflow-x: auto !important;
  max-width: 90vw !important;
}
like image 24
Shantanu Mahakale Avatar answered Sep 19 '22 07:09

Shantanu Mahakale