How to center a modal window on a page?

Raghu picture Raghu · Jan 6, 2014 · Viewed 87.6k times · Source

I am trying center a modal window in the browser page. I just want to center it, so that it should be responsive for all the screens.

Answer

Waqar Alamgir picture Waqar Alamgir · Jan 6, 2014

With position:absolute Assuming your modal is 300x300

.modal {
   width: 300px;
   height: 300px;
   position: absolute;
   left: 50%;
   top: 50%; 
   margin-left: -150px;
   margin-top: -150px;
}

With display:table An alternative way for that is to make display table

<div class="modal">
    <div class="body">
        <div class="content">
            Content goes here
        </div>
    </div>
</div>
<style>
    .modal {display:table;}
    .body {display:table-cell; vertical-align:middle; text-align:center;}
</style>