我创建了以下 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,将 float
和 min-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/