Positioning
element at center of screen

后端 未结 13 1369
死守一世寂寞
死守一世寂寞 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:02

    Another easy flexible approach to display block at center: using native text alignment with line-height and text-align.

    Solution:

    .parent {
        line-height: 100vh;        
        text-align: center;
    }
    
    .child {
        display: inline-block;
        vertical-align: middle;
        line-height: 100%;
    }
    

    And html sample:

    My center block

    We make div.parent fullscreen, and his single child div.child align as text with display: inline-block.

    Advantages:

    • flexebility, no absolute values
    • support resize
    • works fine on mobile

    Simple example on JSFiddle: https://jsfiddle.net/k9u6ma8g

提交回复
热议问题