[英]Vertically center image if height less than parent.height, else limit height to 100%
我在全屏父div上使用html5-fullscreen模式,在这种模式下,我想垂直居中放置图像,但前提是图像小于父div(即屏幕尺寸)。 我还想将图像限制为父级的max-height
= 100%,以避免在图像大于屏幕的情况下放大图像。
我可以使用两个div(一个display:table
,另一个display:table-cell
)来居中图像,但是我不能同时将图像的max-height
为不大于父div。 我可以使用position:absolute; max-height:100%;
来完成后者position:absolute; max-height:100%;
position:absolute; max-height:100%;
但是居中在较小的图像上不起作用。
标记示例:
<div id="fullscreen-container">
<div id="fullscreen-img-wrapper">
<img id="fullscreen-img" src="/images/test-6000x4000.png" />
</div>
</div>
任何想法如何实现将不胜感激!
<style>
#fullscreen-img-wrapper { display: table-cell;
margin-left: auto;
margin-right: auto;
vertical-align: middle;
width: 100%; }
#fullscreen-img { width:100%; }
</style>
<div id="fullscreen-container">
<div id="fullscreen-img-wrapper">
<img id="fullscreen-img" src="/images/test-6000x4000.png" />
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.