在图像下方垂直和水平居中显示标题
Vertically and horizontally center a heading underneath an image
我在正在构建的网站上创建了以下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,将float
和min-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/.
相关文章:
- 将高图饼图中的文本居中显示为响应
- 使用CSS嵌入HTML中的水平居中jQuery
- 如何在弹出窗口中居中显示图像
- 如何将弹出窗口居中显示在屏幕上
- 单击水平滚动库中的图像以居中显示无法工作的功能
- JQuery mobile:使用网格将多个图像垂直和水平居中
- CSS3-如何交叉显示未知高度的图像,并将其垂直和水平居中
- 在没有额外功能的所有现代浏览器中居中显示JavaScript窗口
- 如何将 swf 文件居中显示在不同的屏幕分辨率上
- 水平居中高度变化的图像,而不会扭曲图像
- jQuery:水平居中绝对定位对象
- 如何在禁用的输入类型=“time”元素上居中显示数字
- 水平居中各种宽度的slick.js幻灯片
- 在图像下方垂直和水平居中显示标题
- 使用CSS在屏幕上居中显示图像-随机屏幕/图像尺寸
- 可以't似乎将已翻译的文本水平居中270度
- 如何使用d3.js在SVG图表上居中显示标题
- 如何将多个按钮水平居中显示
- 尝试使用outerWidth()和$(window).resize()将动态宽度的元素水平居中时出现意外行为
- 自动缩放和自动居中显示窗口中的所有标记