Twitter Bootstrap modal on mobile devices

前端 未结 12 2074
忘了有多久
忘了有多久 2020-12-02 04:20

Bootstrap modals don\'t work correctly on Android and iOS. The issue tracker acknowledges the problem but does not offer a working solution:

Modals in 2.0 are broke

12条回答
  •  长情又很酷
    2020-12-02 05:23

    Gil's answer holds promise (the library he linked to) --- but for the time being, it still doesn't work when scrolled down on the mobile device.

    I solved the issue for myself using just a snippet of CSS at the end of my CSS files:

    @media (max-width: 767px) {
      #content .modal.fade.in {
        top: 5%;
      }
    }
    

    The #content selector is simply an id that wraps my html so I can override Bootstrap's specificity (set to your own id wrapping your modal html).

    The downside: It's not centered vertically on the mobile devices.

    The upside: It's visible, and on smaller devices, a reasonably sized modal will take up much of the screen, so the "non-centering" won't be as apparent.

    Why it works:

    When you're at low screen sizes with Bootstrap's responsive CSS, for devices with smaller screens, it sets .modal.fade.in's 'top' to 'auto'. For some reason the mobile webkit browsers seem to have a hard time with figuring out the vertical placement with the "auto" assignment. So just switch it back to a fixed value and it works great.

    Since the modal is already set to postition: absolute, the value is relative to the viewport's height, not the document height, so it works no matter how long the page is or where you're scrolled to.

提交回复
热议问题