Absolute Positioning & Text Alignment

后端 未结 5 886
我在风中等你
我在风中等你 2021-01-30 20:19

I would like some text to be centered in the bottom of the screen.

I tried this, but it doesn\'t work. It looks like absolute positioning conflicts with the alignment.

相关标签:
5条回答
  • 2021-01-30 20:45

    Try this:

    http://jsfiddle.net/HRz6X/2/

    You need to add left: 0 and right: 0 (not supported by IE6). Or specify a width

    0 讨论(0)
  • 2021-01-30 20:53

    The div doesn't take up all the available horizontal space when absolutely positioned. Explicitly setting the width to 100% will solve the problem:

    HTML

    <div id="my-div">I want to be centered</div>​
    

    CSS

    #my-div {
       position: absolute;
       bottom: 15px;
       text-align: center;
       width: 100%;
    }
    

    0 讨论(0)
  • 2021-01-30 20:55
    position: absolute;
    color: #ffffff;
    font-weight: 500;
    top: 0%;
    left: 0%;
    right: 0%;
    text-align: center;
    
    0 讨论(0)
  • 2021-01-30 20:57

    This should work:

    #my-div { 
      left: 0; 
      width: 100%; 
    }
    
    0 讨论(0)
  • 2021-01-30 21:08

    Maybe specifying a width would work. When you position:absolute an element, it's width will shrink to the contents I believe.

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