Positioning
element at center of screen

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

    It will work for any object. Even if you don't know the size:

    .centered {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    
    0 讨论(0)
  • 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:

    <div class="parent">
      <div class="child">My center block</div>
    </div>
    

    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

    0 讨论(0)
  • 2020-11-28 19:05

    try this:

    width:360px;
    height:360px;
    top: 50%; left: 50%;
    margin-top: -160px; /* ( ( width / 2 ) * -1 ) */
    margin-left: -160px; /* ( ( height / 2 ) * -1 ) */
    position:absolute;
    
    0 讨论(0)
  • 2020-11-28 19:09

    try this

    <table style="height: 100%; left: 0; position: absolute; text-align: center; width: 100%;">
     <tr>
      <td>
       <div style="text-align: left; display: inline-block;">
        Your Html code Here
       </div>
      </td>
     </tr>
    </table>
    

    Or this

    <div style="height: 100%; left: 0; position: absolute; text-align: center; width: 100%; display: table">
     <div style="display: table-row">
      <div style="display: table-cell; vertical-align:middle;">
       <div style="text-align: left; display: inline-block;">
        Your Html code here
       </div>
      </div>
     </div>
    </div>
    
    0 讨论(0)
  • 2020-11-28 19:10

    The easy way, if you have a fixed width and height:

    #divElement{
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -50px;
        width: 100px;
        height: 100px;
    }​
    

    Please don't use inline styles! Here is a working example http://jsfiddle.net/S5bKq/.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题