中心图像大于包含div

问题描述 投票:-1回答:2

这就是我所拥有的:

.thumbnail {
    height: 600px;
    width: 60%;
    overflow: hidden;
}
.thumbnail img {
    height: 600px;
     width: auto;
}

图像的宽度可以是任何大小,也可以大于容器div。我该如何居中呢?

css image center
2个回答
0
投票

使用Flex简单,作为您的问题,假设图像可用大小(没有CSS设置)

.thumbnail {
    height: 600px;
    width: 60%;
    overflow: hidden;  
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumbnail {
    height: 600px;
    width: 60%;
    overflow: hidden;  
  display: flex;
  align-items: center;
  justify-content: center;
}
<div style="width: 300px;">
<div class="thumbnail"><img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" /></div>
</div>

0
投票

尝试添加这个

.thumbnail img {
    height: 600px;
     width: auto;
     display:block;
     margin: 0 auto;
}
© www.soinside.com 2019 - 2024. All rights reserved.