【问题标题】:Vertically center align content of floated, dynamically heighted dv [duplicate]浮动,动态高度dv的垂直居中对齐内容[重复]
【发布时间】:2015-08-09 12:29:33
【问题描述】:

我不想为任何元素设置任何固定高度,这可能吗?

<div class="floated">
  <div class="Content">
     <p>text</p>
  </div>
</div>

【问题讨论】:

  • 需要HTML/CSS相关代码。

标签: css


【解决方案1】:

body {
  height : 1000px; /* needed to let the height percentage "MyDiv" correctly work */
}
#myDiv {
  position : relative;
   height : 20%; 
  display : table;
  border : solid 1px black; /* to see the behaviour of the div, can be removed */
  
}

#myContainer {
  display : table-cell;
  vertical-align : middle;
}
<body>
<div id="myDiv" >
  <div id="myContainer">
    my super content !
   </div>
</div>
  </body>

在第一个 div 容器上使用 display : table,在包含您的文本或图像的子 div 中使用 display : table-cell ....

【讨论】:

  • 你没读过操作吗?