How do I position a div at the bottom center of the screen

前端 未结 6 1504
一生所求
一生所求 2020-12-25 11:17

I have this css:

#manipulate
{
  position:absolute;
  width:300px;
  height:300px;
  background:#063;
  bottom:0px;
  right:25%;
}

I have t

相关标签:
6条回答
  • 2020-12-25 11:53

    Using a Flexbox worked for me:

    #manipulate {
      position: absolute;
      width: 100%;
      display: flex;
      justify-content: center; // Centers the item
      bottom: 10px; // Moves it up a little from the bottom
    }
    
    0 讨论(0)
  • 2020-12-25 11:57

    You can center it using negative margins BUT please note that it'll center exactly on the center of the screen IF any containing div is NOT SET to position:relative;

    For example. http://jsfiddle.net/aWNCm/

    So, best way to exactly center this div is to set correct properties position properties for its containing divs too otherwise it will be lost in some random ways.

    0 讨论(0)
  • 2020-12-25 12:08

    Use negative margins:

    #manipulate
    {
      position:absolute;
      width:300px;
      height:300px;
      margin-left:-150px;
      background:#063;
      bottom:0px;
      left:50%;
    }
    

    The key here is the width, left and margin-left properties.

    0 讨论(0)
  • 2020-12-25 12:09

    Here is a solution with two divs:

    HTML:

        <div id="footer">
            <div id="center">
                Text here
            </div>
        </div>
    

    CSS:

    #footer {
        position: fixed;
        bottom: 0;
        width: 100%;
    }
    #center {
        width: 500px;
        margin: 0 auto;
    }
    
    0 讨论(0)
  • 2020-12-25 12:16

    align="center" has no effect.

    Since you have position:absolute, I would recommend positioning it 50% from the left and then subtracting half of its width from its left margin.

    #manipulate {
        position:absolute;
        width:300px;
        height:300px;
        background:#063;
        bottom:0px;
        right:25%;
        left:50%;
        margin-left:-150px;
    }
    
    0 讨论(0)
  • 2020-12-25 12:18

    If you aren't comfortable with using negative margins, check this out.

    HTML -

    <div>
     Your Text
    </div>
    

    CSS -

    div {
      position: fixed;
      left: 50%;
      bottom: 20px;
      transform: translate(-50%, -50%);
      margin: 0 auto;
    }
    

    Especially useful when you don't know the width of the div.

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