I want to show an image from an URL with a certain width and height even if it has a different size ratio. So I want to resize (maintaining the ratio) and then cut the image to the size I want. I can resize with html img property and I can cut with background-image. How can I do both? Example: This image: Has the size 800x600 pixels and I want to show like an image of 200x100 pixels With img I can resize the image 200x150px: <img style="width: 200px; height: 150px;" src="http://i.stack.imgur.com/wPh0S.jpg"> That gives me this: <img style="width: 200px; height: 150px;" src="https://i.stack.imgur.com/wPh0S.jpg"> And with background-image I can cut the image 200x100 pixels. <div style="background-image: url('https://i.stack.imgur.com/wPh0S.jpg'); width:200px; height:100px; background-position:center;"> </div> Gives me: <div style="background-image:url('https://i.stack.imgur.com/wPh0S.jpg'); width:200px; height:100px; background-position:center;"> </div> How can I do both? Resize the image and then cut it the size I want?
当前回答
你可以把两种方法结合起来使用。
.crop { 宽度:200 px; 身高:150 px; 溢出:隐藏; } .crop img { 宽度:400 px; 身高:300 px; Margin: -75px 00 -100px; } < div class = "作物”> <img src="https://i.stack.imgur.com/wPh0S.jpg" alt="唐老鸭"> < / div >
您可以使用负边距在<div/>内移动图像。
其他回答
你可以使用Kodem的图片调整服务。您可以通过http调用调整任何图像的大小。可以在浏览器中随意使用,也可以在生产应用程序中使用。
上传图片到你喜欢的地方(S3, imgur等) 将其插入到您的专用API url(从我们的仪表板)
<p class="crop"><a href="http://templatica.com" title="Css Templates">
<img src="img.jpg" alt="css template" /></a></p>
.crop {
float: left;
margin: .5em 10px .5em 0;
overflow: hidden; /* this is important */
position: relative; /* this is important too */
border: 1px solid #ccc;
width: 150px;
height: 90px;
}
.crop img {
position: absolute;
top: -20px;
left: -55px;
}
img {
position: absolute;
clip: rect(0px,60px,200px,0px);
}
我最近需要这样做。我想做一个缩略图链接到NOAA的图表。由于他们的图形可以随时改变,我希望我的缩略图也能随之改变。但他们的图表有一个问题:它在顶部有一个巨大的白色边框,所以如果你只是缩放它来制作缩略图,你最终会在文档中出现多余的空白。
以下是我的解决方法:
http://sealevel.info/example_css_scale_and_crop.html
首先我需要做一点算术。来自NOAA的原始图像是960 × 720像素,但前70个像素是一个多余的白色边界区域。我需要一个348 × 172的缩略图,顶部没有额外的边界区域。这意味着原始图像的期望部分是720 - 70 = 650像素高。我需要将其缩小到172像素,即172 / 650 = 26.5%。这意味着需要从缩放图像的顶部删除70 = 19行的26.5%的像素。
So…
设置高度= 172 + 19 = 191像素: 身高= 191 设置底部边距为-19像素(将图像缩短为172像素高): margin-bottom: -19像素 设置顶部位置为-19像素(将图像向上移动,以便顶部19像素的行溢出并隐藏,而不是底部的行): 前:-19像素
生成的HTML如下所示:
<a href="…" style="display:inline-block;overflow:hidden">
<img width=348 height=191 alt=""
style="border:0;position:relative;margin-bottom:-19px;top:-19px"
src="…"></a>
如您所见,我选择样式包含<a>标记,但您可以样式包含<div>。
这种方法的一个缺点是,如果您显示边界,顶部的边界将会丢失。因为我使用border=0,所以这对我来说不是问题。
我所做的是创建一个服务器端脚本,它将在服务器端调整大小和裁剪图片,这样它就会在互联网上发送更少的数据。
它相当简单,但如果有人感兴趣,我可以挖掘并发布代码(asp.net)
推荐文章
- 使伸缩项目正确浮动
- 如何取消最大高度?
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何选择在最后一个子元素之前的元素?
- 如何触发自动填充在谷歌Chrome?
- CSS变换,锯齿边缘在铬
- 创建圈div比使用图像更容易的方法?
- 强迫孩子服从父母的弯曲边界在CSS
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在CSS中@apply是什么?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?