Run
Run

Reputation: 57286

Bootstrap modal responsive vertical centering?

How can I vertically centerise the bootstrap modal? I have looked around for a solution here but they are not responsive, or just not working at all. I am using Bootsrap 3.

The modal is not responsive anymore to a smaller screen or when you resize the browser window after using the solution below.

jquery,

$('.modal').on('shown.bs.modal', function() {
            $(this).find('.modal-dialog').css({
                'margin-top': function () {
                    return -($(this).outerHeight() / 2);
                },
                'margin-left': function () {
                    return -($(this).outerWidth() / 2);
                }
            });
        });

css,

.modal-dialog {
        margin: 0;
        position: absolute;
        top: 50%;
        left: 50%;
    }

bootstrap,

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="false">
  <div class="modal-dialog custom-class">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Any idea?

EDIT:

I tried with this css,

@media screen and (min-width: 768px) {
        .custom-class {
            width: 800px; /* either % (e.g. 60%) or px (400px) */
            top: 25%;
        }
    }

The modal just falls back to default position on the smaller screen, but I would like it to be vertically centred.

also, top: 25%; does not make it fully vertically centred at all.

Upvotes: 1

Views: 3338

Answers (2)

Scarecrow
Scarecrow

Reputation: 4137

Using css we can place Bootstrap modal at vertical center.

    .modal {
        text-align: center;
        padding: 0!important;
    }

    .modal:before {
        content: '';
        display: inline-block;
        vertical-align: middle;
        margin: -2px;
        height: 100%;
    }

    .modal-dialog {
        display: inline-block;
        vertical-align: middle;
    }

Upvotes: 2

Run
Run

Reputation: 57286

I think this is it,

css,

.modal-dialog {
        top: 0;
        margin-top:0;
        display: none;
        border:1px solid red;
    }

jquery + bootstrap,

$('#myModal').on('shown.bs.modal', function (e) {

            var object = $('.modal-dialog');
            var object_outerheight = object.outerHeight();
            var window_height = $(window).height();
            var window_scrolltop = $(window).scrollTop();
            var object_top = ((window_height - object_outerheight)/2) + window_scrolltop;
            console.log(object_top);

            // Set the object's position.
            object.css({
                marginTop: object_top + 'px'
            }).fadeIn();

            // Add responsive when the window is being resized.
            $( window ).resize(function() {

                // Redo the calc on each resize's action.
                var window_height = $(window).height();
                var window_scrolltop = $(window).scrollTop();
                var object_top = ((window_height - object_outerheight)/2) + window_scrolltop;
                console.log(object_top);

                // Reset the object's position.
                object.css({
                    marginTop: object_top + 'px'
                });

            });
        });

Upvotes: 2

Related Questions