Center Align on a Absolutely Positioned Div

后端 未结 9 413
攒了一身酷
攒了一身酷 2020-12-07 14:18
div#thing {
  position: absolute;
  top: 0px;
  z-index: 2;
  margin: 0 auto;
}

text text text with no fixed size, variable fon

相关标签:
9条回答
  • 2020-12-07 14:30

    .contentBlock {
        width: {define width}
        width: 400px;
        position: absolute;
        left: 0;
        right: 0;
        margin-left: auto;
        margin-right: auto;
     
    }

    0 讨论(0)
  • 2020-12-07 14:32

    Yes:

    div#thing { text-align:center; }
    
    0 讨论(0)
  • 2020-12-07 14:36
    div#thing
    {
        position: absolute;
        width:400px;
        right: 0;
        left: 0;
        margin: auto;
    }
    
    0 讨论(0)
  • 2020-12-07 14:38

    I was having the same issue, and my limitation was that i cannot have a predefined width. If your element does not have a fixed width, then try this

    div#thing 
    { 
      position: absolute; 
      top: 0px; 
      z-index: 2; 
      left:0;
      right:0;
     }
    
    div#thing-body
    {
      text-align:center;
    }
    

    then modify your html to look like this

    <div id="thing">
     <div id="thing-child">
      <p>text text text with no fixed size, variable font</p>
     </div>
    </div>
    
    0 讨论(0)
  • 2020-12-07 14:44

    To center it both vertically and horizontally do this:

    div#thing {
       position: absolute;
       left: 50%;
       top: 50%;
       transform: translate(-50%, -50%);
    }
    
    0 讨论(0)
  • 2020-12-07 14:46

    Your problem may be solved if you give your div a fixed width, as follows:

    div#thing {
        position: absolute;
        top: 0px;
        z-index: 2;
        width:400px;
        margin-left:-200px;
        left:50%;
    }
    
    0 讨论(0)
提交回复
热议问题