【问题标题】:Vertically centering content in an element with no height, but padding在没有高度但有填充的元素中垂直居中内容
【发布时间】:2015-10-17 02:37:01
【问题描述】:

小提琴: http://jsfiddle.net/dbwvx42s

问题描述:我正在使用 Bootstrap 在一个部分中创建一些列。该部分的高度为零(例如height: 0),但底部填充为 56.25%,以保持页面上其他内容的纵横比 (16:9)。

我遇到的问题是如何在这个零高度的填充元素中垂直居中内容。我尝试了一个 flexbox 解决方案,但无济于事。我尝试过内联阻止内容并使用垂直对齐,这也是不行的。我的技巧用完了!

任何帮助表示赞赏。

代码:

#section-1 {
    background-color: #0099cc;
    color: #fff;
    height: 0;
    padding-bottom: 56.25%;
}

【问题讨论】:

  • 是什么样的内容?文本?标记?图片?桌子?

标签: html twitter-bootstrap css vertical-alignment


【解决方案1】:

您可以创建一个包含内容的绝对定位子元素:

#section-1 {
  
  position: relative;
  
  background-color: #0099cc;
  color: #fff;
  height: 0;
  padding-bottom: 56.25%;
}

#section-1 div.content {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  
}
<div id="section-1">
  <div class="content">
    Content goes here  
  </div>
</div>

现在,div.content 可以用作正常大小的 div。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 2013-01-09
    相关资源
    最近更新 更多