How to center div vertically inside of absolutely positioned parent div

前端 未结 9 1857
闹比i
闹比i 2020-12-02 04:48

I am trying to get blue container in the middle of pink one, however seems vertical-align: middle; doesn\'t do the job in that case.

相关标签:
9条回答
  • 2020-12-02 04:50

    An additional simple solution

    HTML:

    <div id="d1">
        <div id="d2">
            Text
        </div>
    </div>
    

    CSS:

    #d1{
        position:absolute;
        top:100px;left:100px;
    }
    
    #d2{
        border:1px solid black;
        height:50px; width:50px;
        display:table-cell;
        vertical-align:middle;
        text-align:center;  
    }
    
    0 讨论(0)
  • 2020-12-02 05:00

    You can do it by using display:table; in parent div and display: table-cell; vertical-align: middle; in child div

    <div style="display:table;">
          <div style="text-align: left;  height: 56px;  background-color: pink; display: table-cell; vertical-align: middle;">
              <div style="background-color: lightblue; ">test</div>
          </div>
      </div>

    0 讨论(0)
  • 2020-12-02 05:03

    Here is simple way using Top object.

    eg: If absolute element size is 60px.

    .absolute-element { 
        position:absolute; 
        height:60px; 
        top: calc(50% - 60px);
    }
    
    0 讨论(0)
  • 2020-12-02 05:05

    You may use display:table/table-cell;

    .a{
       position: absolute; 
      left: 50px; 
      top: 50px;
      display:table;
    }
    .b{
      text-align: left; 
      display:table-cell;
      height: 56px;
      vertical-align: middle;
      background-color: pink;
    }
    .c {
      background-color: lightblue;
    }
    <div class="a">
      <div  class="b">
        <div class="c" >test</div>
      </div>
    </div>

    0 讨论(0)
  • 2020-12-02 05:11

    Use flex blox in your absoutely positioned div to center its content.

    See example https://plnkr.co/edit/wJIX2NpbNhO34X68ZyoY?p=preview

    .some-absolute-div {    
      display: -webkit-box;
      display: -webkit-flex;
      display: -moz-box;
      display: -moz-flex;
      display: -ms-flexbox;
      display: flex;
    
      -webkit-box-pack: center;
      -ms-flex-pack: center;
      -webkit-justify-content: center;
      -moz-justify-content: center;
      justify-content: center;
    
      -webkit-box-align: center;
      -ms-flex-align: center;
      -webkit-align-items: center;
      -moz-align-items: center;
      align-items: center;
    }
    
    0 讨论(0)
  • 2020-12-02 05:12

    For only vertical center

        <div style="text-align: left; position: relative;height: 56px;background-color: pink;">
            <div style="background-color: lightblue;position:absolute;top:50%;    transform: translateY(-50%);">test</div>
        </div>

    I always do like this, it's a very short and easy code to center both horizontally and vertically

    .center{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    <div class="center">Hello Centered World!</div>

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