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.
If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).
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.
Setting sizes in percentages is a good option, another option is to use transform
:
.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This will move the modal to 50% from top and left, and then adjust it back based upon the dimensions of the modal itself. This isn't ideal for every situation, but it works well in many modal use cases.
To make it responsive, instead try setting the width
using %
or vw
units. Also, to center it horizontally you can use margin: 0 auto
. Centering it vertically is a little more tricky, check out this answer for more on that.
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