50

I am trying to make a modal with a body that will scroll when the content becomes too large. However, I want the modal to be responsive to the screen size. When I set the max-height to 40% it has no effect. However, if I set the max-height to 400px it works as expected, but is not responsive. I am sure I am just missing something simple, but I can't seem to get it to work.

Here is an example

Doesn't work:

.modal-body {
    max-height:40%; 
    overflow-y: auto;
}

Works:

.modal-body {
    max-height:400px; 
    overflow-y: auto;
}
thraxst
  • 685
  • 3
  • 8
  • 11

6 Answers6

77

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/

Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
37

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;
}
Ryan
  • 541
  • 4
  • 4
  • Why always force scrollbars with `overflow: scroll;`? Curious – Chris May 18 '16 at 21:50
  • 2
    You don't have to use `overflow: scroll;`. You could use `overflow: auto;` if you only want scroll bars to appear if it's too long. – Ryan Jul 22 '17 at 15:37
  • 1
    Scroll should be on the `modal-body` so it doesn't scroll the modal's header away. – Vael Victus Sep 03 '18 at 21:21
  • That makes sense and would be a nice way to implement it too. – Ryan Sep 14 '18 at 01:01
  • 1
    With **Bootstrap 4**, it's better to move the `overflow` from `modal-content` to `modal-body`. This way, you can scroll the body of the modal while the header and the footer stays in place. – Binar Web Aug 28 '20 at 09:25
20

This should work for everyone, any screen resolutions:

.modal-body {
    max-height: calc(100vh - 143px);
    overflow-y: auto; }

First, count your modal header and footer height, in my case I have H4 heading so I have them on 141px, already counted default modal margin in 20px(top+bottom).

So that subtract 141px is the max-height for my modal height, for the better result there are both border top and bottom by 1px, for this, 143px will work perfectly.

In some case of styling you may like to use overflow-y: auto; instead of overflow-y: scroll;, try it.

Try it, and you get the best result in both computer or mobile devices. If you have a heading larger than H4, recount it see how much px you would like to subtract.

If you don't know what I am telling, just change the number of 143px, see what is the best result for your case.

Last, I'd suggest have it an inline CSS.

Brane
  • 3,257
  • 2
  • 42
  • 53
bard
  • 1,053
  • 3
  • 13
  • 22
  • Worked for me. Similarly, using your technique, this works for the width (assuming you're using modal-lg for width): .modal-lg{ width: calc(100vw - 143px); } – Flat Cat Nov 13 '18 at 17:38
4

You've no doubt solved this by now or decided to do something different, but as it has not been answered & I stumbled across this when looking for something similar I thought I'd share my method.

I've taken to using two div sets. One has hidden-xs and is for sm, md & lg device viewing. The other has hidden-sm, -md, -lg and is only for mobile. Now I have a lot more control over the display in my CSS.

You can see a rough idea in this js fiddle where I set the footer and buttons to be smaller when the resolution is of the -xs size.

  <div class="modal-footer">
      <div class="hidden-xs">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary">Save changes</button>
      </div>
      <div class="hidden-sm hidden-md hidden-lg sml-footer">
    <button type="button" class="btn btn-xs btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-xs btn-primary">Save changes</button>
      </div>
  </div>
VictorySaber
  • 3,084
  • 1
  • 27
  • 45
2

The scss solution for Bootstrap 4.0

.modal {
  max-height: 100vh;
  .modal-dialog {
    .modal-content {
      .modal-body {
        max-height: calc(80vh - 140px);
        overflow-y: auto;
      }
    }
  }
}

Make sure the .modal max-height is 100vh. Then for .modal-body use calc() function to calculate desired height. In above case we want to occupy 80vh of the viewport, reduced by the size of header + footer in pixels. This is around 140px together but you can measure it easily and apply your own custom values. For smaller/taller modal modify 80vh accordingly.

demo
  • 6,038
  • 19
  • 75
  • 149
lokers
  • 2,106
  • 2
  • 18
  • 19
1

this is the solution

.modal-dialog{
            height:100%
        }
        .modal-content{
            height:90%
        }
        .modal-body{
            height:100%;
            overflow:auto
        }
Osama
  • 11
  • 2
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 28 '21 at 09:00