Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical alignment of bootstrap modal on mobiles

This question seems to have been asked a number of times on github but I haven't come across any solutions: https://github.com/twitter/bootstrap/issues/1399

how can the modal be vertically aligned properly on small screens?

like image 556
raklos Avatar asked Mar 23 '12 16:03

raklos


People also ask

How do you make a Bootstrap modal vertically centered?

Answer: Use the CSS margin-top Property But you can align it in the middle of the page vertically using a simple JavaScript trick as described in the example below. 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 I vertically align an item in Bootstrap?

Change the alignment of elements with the vertical-alignment utilities. Please note that vertical-align only affects inline, inline-block, inline-table, and table cell elements. Choose from .align-baseline , .align-top , .align-middle , .align-bottom , .align-text-bottom , and .align-text-top as needed.

How do I center a vertical line in Bootstrap?

One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.


1 Answers

Edit: As of Bootstrap 2.0.2, this is no longer an issue. Basically, bootstrap now implements the solution proposed below, which I'll leave here for reference purposes.

Generally, on small screens modals will fill out most of your window, so positioning relative to the button that triggered the modal doesn't make terribly much sense. What I usually do is just overwriting the modal position for phones:

@media (max-width: 767px) {
  .modal {
      top: 20px;
      // negative left margin to position horizontally.
      margin: 0 0 0 -280px; 
      // already in modals.less, just copied for clarification:
      left: 50%;  
      position: fixed;
   }
}

(This is in LESS, pure CSS solutions looks similar).

like image 86
Manuel Ebert Avatar answered Oct 02 '22 14:10

Manuel Ebert