【发布时间】:2017-07-22 06:04:10
【问题描述】:
我需要实现的是将.btn p 垂直对齐到.btn img 高度的中间。如果.btn p 元素向右浮动,是否可以做到这一点?我需要它们在同一行,但是当将它们的显示更改为 display: inline block; 时,它们会垂直堆叠(我不知道为什么)。
我知道我可以很容易地用flexbox 做到这一点,但我需要它被 IE 9+ 浏览器支持,而flexbox 只是partially-supported by IE 11。
html, body {
margin: 0;
padding: 0;
}
.slide1 {
background-color: lightblue;
height: 100vh;
position: relative;
text-align: center;
}
.slide1 h1 {
display: block;
width: 100%;
padding: 0;
margin: 0 auto;
position: absolute;
top: 4.75em;
}
.slider-btn {
position: absolute;
width: 100%;
bottom: 120px;
left: 50%;
transform: translateX(-50%);
text-align: center;
}
.slider-btn a {
display: inline-block;
}
.slider-btn p {
background-color: blue;
color: white;
margin: 0;
padding: 0.5em;
float: right;
}
.btn {
display: inline-block;
}
/*# sourceMappingURL=main.css.map */
<!DOCTYPE html>
<html>
<head>
<title> test </title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<section class="heading">
<div class="slide1">
<h1> Lorem Ipsum sit dolor amet </h1>
</div>
<div class="slider-btn">
<div class="btn">
<a href="http://google.com">
<img src="http://placehold.it/81x81" />
<p> Slide no. 1 </p>
</a>
</div>
<div class="btn">
<a href="http://google.com">
<img src="http://placehold.it/81x81" />
<p> Slide no. 2 </p>
</a>
</div>
<div class="btn">
<a href="http://google.com">
<img src="http://placehold.it/81x81" />
<p> Slide no. 3 </p>
</a>
</div>
</div>
</section>
</body>
</html>
【问题讨论】:
-
如果图像大小是固定的并且已知,您可以简单地在
标签上应用 margin-top 并将其与图像垂直对齐(除非
高度可能发生变化)。
-
不幸的是它不是。
-
那么你有另一个选择。应用显示:表格;在 标签上并显示:table-cell;垂直对齐:中间;在它的孩子身上。
标签: html css vertical-alignment centering