Center content in a absolute positioned div

后端 未结 4 958
慢半拍i
慢半拍i 2021-02-12 18:34

I have an absolutly positioned element with content inside. It can be a

or a few

tag. So I don\'t know the height of the content.

相关标签:
4条回答
  • 2021-02-12 19:14

    This can be done with flexbox too:

    #absolute {
      align-items: center;
      display: flex;
      justify-content: center;
    }
    
    0 讨论(0)
  • 2021-02-12 19:15

    if you add display:table to your #absolute div you should get what you want:

    http://jsfiddle.net/3KTUM/2/

    0 讨论(0)
  • 2021-02-12 19:28

    Horizontal and vertical position absolute middle.

    .center {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50% , -50%);
      -webkit-transform: translate(-50%, -50%);
    }
    <div class="obj">
      <div class="center">Test</div>
    </div>

    0 讨论(0)
  • 2021-02-12 19:28

    Change your #absolute div to also use:

    display:table;
    vertical-align:middle;
    text-align:center;
    

    http://jsfiddle.net/3KTUM/4/

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