How to center modal to the center of screen? This is my html and js code It works in Chrome console, but when I refresh this page - it doesn't work
$('.modal').css('top', $(window).outerHeight() / 2 - ($(".modal-dialog").outerHeight()) / 2 + 'px');
In this example first, use the find() method to find out the modal dialog. Then subtract the modal height from the window height and divide that into half and will place the modal that will be the centered(vertically). This solution will dynamically adjust the alignment of the modal.
display: block; max-width: 100%; height: auto; display:block means text-align:centre which is inherited from text-center will have no effect. If you remove the . img-responsive class, your image will be centred.
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.
Easy way to solve
.center { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With