Positioning
element at center of screen

后端 未结 13 1436
死守一世寂寞
死守一世寂寞 2020-11-28 18:34

I want to position a

(or a ) element at the center of the screen irrespective of screen size. In other words, the space le
13条回答
  •  生来不讨喜
    2020-11-28 19:12

    With transforms being more ubiquitously supported these days, you can do this without knowing the width/height of the popup

    .popup {
        position: fixed;
        top: 50%;
        left: 50%;
        -webkit-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
    }
    

    Easy! JSFiddle here: http://jsfiddle.net/LgSZV/

    Update: Check out https://css-tricks.com/centering-css-complete-guide/ for a fairly exhaustive guide on CSS centering. Adding it to this answer as it seems to get a lot of eyeballs.

提交回复
热议问题