在图像下方垂直和水平居中显示标题

Vertically and horizontally center a heading underneath an image

本文关键字:水平居中 显示 标题 垂直 图像 方垂直      更新时间:2023-09-26

我在正在构建的网站上创建了以下jsfidd

第一个标题有3行,而其他2行只有一行。我在方框中添加了一个min-height,所以它们都是相等的。

我现在想把标题放在垂直轴和水平轴的中心。

我试过用flexbox实现这一点,但它只是水平对齐。我在这里做错了什么?

.container {
  width: 600px;
  max-width: 90%;
  margin: 0 auto;
}
.third {
  float: left;
  width: 32%;
  min-height: 227px;
  margin: 0 2% 2% 0;
  background: #fff;
}
.last {
  margin: 0 0 2% 0;
}
header {
  padding: 12px;
}
h2 {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
img {
  display: block;
  height: auto;
  max-width: 100%;
}
<div class="container">
  <section class="third">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title one which is slightly longer goes here</h2>
    </header>
  </section>
  <section class="third">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title two</h2>
    </header>
  </section>
  <section class="third last">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title three</h2>
    </header>
  </section>
</div>

更新:

我正在寻找一个支持IE包括9的解决方案。

我建议使用嵌套的flexbox,将floatmin-height放在section上。柔性布局足够智能,可以自动获得相等的高度。有关IE9支持,请参阅底部。

.container {
  width: 600px;
  max-width: 90%;
  margin: 0 auto;
  display: flex;
}
.third {
  /* float: left; */
  width: 32%;
  /* min-height: 227px; */
  margin: 0 2% 2% 0;
  background: gold;
  display: flex;
  flex-direction: column;
}
.last {
  margin: 0 0 2% 0;
}
header {
  padding: 12px;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
h2 {
  margin: 0;
}
img {
  display: block;
  height: auto;
  max-width: 100%;
}
<div class="container">
  <section class="third">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title one which is slightly longer goes here</h2>
    </header>
  </section>
  <section class="third">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title two</h2>
    </header>
  </section>
  <section class="third last">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title three</h2>
    </header>
  </section>
</div>

编辑:IE9支持我会使用一些Javascript来做等高的东西,下面的例子使用jQuery+CSS表单元格来做垂直中心。

$(document).ready(function() {
  var maxHeight = 0;
  $("h2").each(function() {
    if ($(this).height() > maxHeight) {
      maxHeight = $(this).height();
    }
  });
  $("h2").height(maxHeight);
});
.container {
  width: 600px;
  max-width: 90%;
  margin: 0 auto;
}
.third {
  float: left;
  width: 32%;
  /* min-height: 227px; */
  margin: 0 2% 2% 0;
  background: gold;
}
.last {
  margin: 0 0 2% 0;
}
header {
  padding: 12px;
}
h2 {
  margin: 0;
  display: table-cell;
  vertical-align: middle;
}
img {
  display: block;
  height: auto;
  max-width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container">
  <section class="third">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title one which is slightly longer goes here</h2>
    </header>
  </section>
  <section class="third">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title two</h2>
    </header>
  </section>
  <section class="third last">
    <img src="http://bit.ly/1OTNPkt" alt="Kitten">
    <header>
      <h2>Title three</h2>
    </header>
  </section>
</div>

尝试使用display: table,它在某种程度上起作用,但事实证明您不能对border-spacing 使用百分比

演示:https://jsfiddle.net/2dkycc1L/

我使用flexbox制作了整个网格,使其高度相等,并删除了最小高度。header元素也在弯曲,以使标题在中间垂直对齐:https://jsfiddle.net/5yq37fha/.