[英]How to vertically center text adjacent to image in grid alignment?
Trying to vertically center align text next to an image in a three across grid alignment. 尝试使文本旁边的文本垂直居中对齐(三格交叉对齐)。 Have tried many things but either one of two things happens:
尝试了很多事情,但发生了以下两种情况之一:
- text vertically center aligns for one row of text, but wraps under the image if there is more than one row of text -文本垂直居中对齐一行文本,但是如果有一行以上文本,则将其包裹在图像下方
- text wraps properly for multiple rows of text, but won't vertically center align, as shown in the provided code -文本可正确换行以容纳多行文本,但不会垂直居中对齐,如提供的代码所示
Any suggestions? 有什么建议么?
.tile-third { font-size: 18px; text-transform: uppercase; float: left; width: 25%; margin-right: 1%; margin-bottom: 1%; } .tile-image { vertical-align:middle; float:left; margin-right: 1%; background-color:#000; } .tile-text { }
<div class="tile-third"> <a href="#"> <img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-outside.png"/> <div class="tile-text">Department Description One (DD1)</div> </a> </div> <div class="tile-third"> <a href="#"> <img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-lowimpact.png"/> <span class="tile-text">Department Description Two</span> </a> </div> <div class="tile-third"> <a href="#"> <img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-fun.png"/> <span class="tile-text">Department Description Three (DD3)</span> </a> </div>
the image and the text is inside in an anchor tag, change the style of the anchor tag to achieve the required. 图片和文字位于锚标签内,请更改锚标签的样式以实现所需的效果。
example, 例,
.tile-third a {
height: 100%;
display: flex;
align-items: center;
}
Is this what you are looking for?? 这是你想要的??
.tile-third { display: inline-block; font-size: 18px; text-transform: uppercase; width: 30%; } .tile-third > a { align-items: center; display: flex; } .tile-image { background-color:#000; margin-right: 2%; } .tile-text { }
<div class="tile-third"> <a href="#"> <img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-outside.png"/> <div class="tile-text">Department Description One (DD1)</div> </a> </div> <div class="tile-third"> <a href="#"> <img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-lowimpact.png"/> <span class="tile-text">Department Description Two</span> </a> </div> <div class="tile-third"> <a href="#"> <img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-fun.png"/> <span class="tile-text">Department Description Three (DD3)</span> </a> </div>
I set the display
value for the <a>
tag under the <div class="tile-third">
to flex
which is an easy way to get things to properly align. 我将
<div class="tile-third">
下的<a>
标记的display
值设置为flex
,这是使内容正确对齐的一种简便方法。
This allows the <img>
and <div>
inside the <a>
to properly align. 这允许
<a>
的<img>
和<div>
正确对齐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.