How to center align pop up div using Javascript

前端 未结 5 917
借酒劲吻你
借酒劲吻你 2020-12-14 22:58

How to align a pop up division to center of monitor/screen using javascript?

I tried using screen.width and screen.height to get center. But the division gets aligne

5条回答
  •  太阳男子
    2020-12-14 23:42

    I also had this vertical centering problem on any webpage that required scrolling.

    Switching to position: fixed solved it, so:

    position:fixed;
    top:50%;
    left:50%;
    margin:-50px 0 0 -100px; /* [-(height/2)px 0 0 -(width/2)px] */
    

    This worked in firefox, google chrome, safari (pc) and IE9.

    Unfortunately, I wanted it to appear in front of an pdf file - the pop up did appear in front using Firefox, Chrome but went behind in IE9 and Safari....

提交回复
热议问题