Skip to content

Instantly share code, notes, and snippets.

@xiangst0816
Last active June 3, 2018 08:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiangst0816/149b5f8117b0c7be1523 to your computer and use it in GitHub Desktop.
Save xiangst0816/149b5f8117b0c7be1523 to your computer and use it in GitHub Desktop.
将不同大小的img放在固定大小的div中; 要求img不能被拉伸覆盖、要水平居中放置在中间; 如果图片高度超出容器,则以高度为基准确定宽度,反之亦然; img不固定大小设置为inline,外层用line-height和text-align:center确定水平垂直居中;
<a href="">
<div class="cbCoorBox_e">
<img src="img/index/hanya.png" alt="合作机构">
</div>
</a>
.cbCoorBox_e {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 202px;
height: 53px;
margin:18px 20px;
float: left;
border: 1px solid #e3e3e3;
border-radius: 5px;
line-height: 36px;
overflow: hidden;
padding: 6px 15px;
text-align: center;
}
.cbCoorBox_e img{
display: inline;
max-width: 100%;
max-height: 100%;
}
@reactwebster
Copy link

并没有垂直居中

@Eden-Duke
Copy link

max-width: 100%;
max-height: 100%;

这两行代码是 【点题】的 核心 。
垂直居中:
&:before {
content: '';
display: inline-block;
height: 100%;
line-height: 100%;
vertical-align: middle;
}
img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
其中之一的方法。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment