在周围的 div 中,我想将图像居中。此图像应具有强制大小但没有拉伸。所以如果 div 容器是 100px*100px 并且图像是 200px*200px,那么应该在每边裁剪 50px。
在这个问题中,您可以阅读如何强制大小。但我不希望图像从左下角开始,而是将其居中。这是问题:)
这是这样的:
这就是我想要的样子:
给图像一个负的左边距。在这个例子中,margin-left:-50px
。
编辑:或者如果您不知道图像的宽度,您可以将图像用作 div 的背景。
<div style="margin:0 auto; width:100px; height:100px; border:2px solid black;
background:url(yourimagehere) 50% 0 no-repeat">
</div>
即使容器的方向与图像的方向(纵向/横向)不匹配,这也有效。并且意味着如果图像大小达到目标容器的 10 倍zoom(0.1)
,min-width/height:1000%
它就可以工作,我想你必须在某处画一条线:(http://jsfiddle.net/FYnCG/)。动画位只是一个测试,如果你愿意的话,所以你会看到当高度成为限制因素时它是如何缩放的。
<DOCTYPE html>
<html>
<head>
<style>
.container {
width: 100px;
height: 30px;
border: 1px solid black;
overflow:hidden;
position:relative;
transititon: width 5s, height 8s;
}
.container img {
position: absolute;
left:-10000%; right: -10000%;
top: -10000%; bottom: -10000%;
margin: auto auto;
min-width: 1000%;
min-height: 1000%;
-webkit-transform:scale(0.1);
transform: scale(0.1);
}
.container:hover {
width: 800px;
height: 800px;
transition: width 5s, height 8s;
}
</style>
</head>
<body>
<div class="container">
<img
src="https://www.google.de/logos/doodles/2014/european-parliament-election-2014-day-4-5483168891142144-hp.jpg"
/>
<!-- 366x200 -->
</div>
</body>
</html>
这可能是你需要的?