How to center align pop up div using Javascript

前端 未结 5 922
借酒劲吻你
借酒劲吻你 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:39

    Try fixed-positioning:

    #box {
      position: fixed;
      width: 40%;
      margin: 200px 30%;
    }
    

    It's only horizontally centered. Vertical will take some playing with. I have no idea how browsers act differently with the vertical alignment.

提交回复
热议问题