javascript - 在图像下方垂直和水平居中标题

标签 javascript jquery html css

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

第一个标题有 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 上。 flex 布局足够智能,可以自动获得相等的高度。对于 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>

关于javascript - 在图像下方垂直和水平居中标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696309/

相关文章:

javascript - jQuery 中的重建元素与批量显示/隐藏

javascript - Parse.com JavaScript SDK - 如何刷新缓存的用户对象?

javascript - 仅使用 babel 构建 typescript vue 应用程序?

javascript - 我如何定位 div 以使其在点击功能期间不可见?

javascript - 修改 javascript var 值而不重新加载页面

javascript - 在不同的屏幕尺寸下动态改变另一个 div 的位置

html - CSS - 段落将无法在 div 中正确显示

javascript - 如何在命令行中测试 AJAX?

javascript - 将 tinyMCE 添加到动态添加的文本区域

python - 如何识别并跟踪链接,然后使用 BeautifulSoup 从新网页打印数据