Center div in parent with only min-height, and child may without height & with relative position

后端 未结 1 967
栀梦
栀梦 2021-01-24 18:00

so I have a div which is min-height 100%; (100% of the screen or more). The child holds the content and needs to be centered vertically, but it might extend the parent to more t

相关标签:
1条回答
  • 2021-01-24 18:28

    Use the display: table; and display: table-cell; properties.

    The outer DIV will need to have display: table; and the inner DIV display: table-cell; along with vertical-align: middle;. Now you will be mimicing the default display of a td.

    CSS

    .parent {
      min-height: 100%;
      display: table;
    }
    .child {
      display: table-cell;
      vertical-align: middle; 
    }
    

    HTML

    <div class="parent">
         <div class="child">child</div>
    </div>
    

    This question has been asked often. A simple search here on SO or Google will get you plenting of results.

    • How to vertically center divs?
    • Align vertically using CSS 3
    0 讨论(0)
提交回复
热议问题