Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a modal window on a page?

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.

like image 434
Raghu Avatar asked Jan 06 '14 06:01

Raghu


People also ask

How do you center a modal on a page?

Answer: Use the CSS margin-top Property This solution will dynamically adjust the alignment of the modal and always keep it in the center of the page even if the user resizes the browser window.

How do you position a modal?

Modals use position: fixed , which can sometimes be a bit particular about its rendering. Whenever possible, place your modal HTML in a top-level position to avoid potential interference from other elements. You'll likely run into issues when nesting a . modal within another fixed element.

How do I center a Bootstrap modal vertically?

modal-dialog-centered to . modal-dialog to vertically center the modal. This is the best answer for bootstrap 4.

How do you center a modal in react native?

This is the modal styling: const styles = StyleSheet. create({ modal:{ position:"relative", width: 250, height: 100, backgroundColor: '#FFF', justifyContent: 'center', alignSelf: 'center', } }); react-native.


2 Answers

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>
like image 55
Waqar Alamgir Avatar answered Oct 21 '22 06:10

Waqar Alamgir


Set bootstrap modal center to any size of screen using css only.

.modal {
  text-align: center;
  padding: 0!important;
}
.modal:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -4px;
}
.modal-dialog {
  display: inline-block;
  text-align: left;
  vertical-align: middle;
}

Browsers & Screens Compatible Solution

like image 12
dev Avatar answered Oct 21 '22 06:10

dev