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

大憨熊 提交于 2019-12-20 05:33:05

问题


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 than 100% of its size. This child is variable in height.

So is there a way with only css(3) to do this?


回答1:


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


来源:https://stackoverflow.com/questions/17658870/center-div-in-parent-with-only-min-height-and-child-may-without-height-with-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!