我在我正在构建的网站上创建了以下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
优秀的解决方案,但我需要支持包括9的IE。还有其他解决方案吗? – Squideyes
你打开jQuery解决方案吗? – Stickers
如果可能,我宁愿纯粹使用HTML和CSS。如果在HTML中很麻烦,我会对JQuery开放。 – Squideyes