简体   繁体   English

溢出内的垂直对齐图像:隐藏的div

[英]vertical-align image within overflow:hidden div

I am trying to align an image vertically within a div with overflow set to hidden so that the container has the same height for each post. 我正在尝试将div中的图像垂直对齐,并将溢出设置为隐藏,以便每个帖子的容器高度相同。 I have tried a lot of other solutions, but it is not working with the overflow element. 我已经尝试了许多其他解决方案,但是它不能与溢出元素一起使用。 Anybody? 任何人? This is my CSS code so far: 到目前为止,这是我的CSS代码:

.featured-image-blog{
  height: 220px;
  width: 600px;
  overflow: hidden;
}
.featured-image-blog img{
  height: auto;    
  width: 600px;
}

and the HTML: 和HTML:

<div class="featured-image-blog">
    <?php the_post_thumbnail('featured-image'); ?>
</div>

Thanks in advance! 提前致谢!

As vertical alignment has always been a pain in legacy HTML and stuff I suggest you give the div: 由于垂直对齐一直是遗留HTML和其他内容的痛苦,因此建议您使用div:

position: relative;

And give the img: 并给出img:

position: absolute;
top: 50%;
transform: translate(0, -50%);
-webkit-transform: translate(0, -50%);
-moz-transform: translate(0, -50%);
-ms-transform: translate(0, -50%);

That should do it.. 那应该做的..

Have you tried using the vertical-align CSS property? 您是否尝试过使用vertical-align CSS属性?

Give this a try: 试试看:

.featured-image-blog img{
  height: auto;    
  width: 600px;
  vertical-align: middle;
}

It should align it to the middle of the parent container. 它应该与父容器的中间对齐。

Read more here: https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align 在此处阅读更多信息: https : //developer.mozilla.org/en-US/docs/Web/CSS/vertical-align

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM