这是我的代码:
<div style="width: 75px; height: 75px; text-align: center; overflow: hidden;">
<img src"myimg.png"/>
</div>
我想在这个div中从左到右裁剪我的图像。
但它只从右边裁剪了一部分。
我想做这样的事情(在IE 8-10中也可以)。
提前感谢!
发布于 2013-07-15 21:49:00
您可以保持HTML不变,并将图像的position:relative
和位置设置为left:-50%
(或margin-left:-50%
)。
您的HTML:
<div id="cropper">
<img src="http://lorempixel.com/output/sports-q-c-900-600-3.jpg" />
</div>
你的CSS:
#cropper{
width:450px;
height:600px;
overflow:hidden;
}
img{
position:relative;
left:-50%;
}
编辑
要以任何div
大小精确地居中图像,您需要以像素而不是百分比来定位图像,除非容器正好是图像大小的一半。因此,900x600像素图像的最终CSS为:
img{
position:relative;
left:-450px;
}
发布于 2013-07-15 20:23:40
为什么不将图像设置为背景呢?
HTML
<div id="mydiv" style="width: 75px; height: 75px; text-align: center; overflow: hidden;">
</div>
CSS
#mydiv
{
background-image:url('myimg.png');
background-repeat:no-repeat;
background-attachment:fixed;
background-position:center;
}
发布于 2018-03-08 21:37:16
这是裁剪一个div的小窍门。将想要裁剪的div放入另一个div中。设置溢出的外部div隐藏他们简单地移动内部div,因为你想要裁剪它使用margin_left,右,上,下属性...
<div style="overflow:hidden;">
<div id="myDiv" style="overflow:hidden;margin-top:-30px"></div>
</div>
简单:)
https://stackoverflow.com/questions/17654000
复制相似问题