html - 调整图像显示的大小

标签 html css

我没有将它显示为缩略图,但是 CSS 可以帮助将尺寸为 1000x1000 的图像压缩为尺寸为 500x500 的图像吗?

我的图像 CSS 如下所示:

.images {
  background:url("image.png") no-repeat scroll; // this is a 1000x1000 sized image
}

如何覆盖它?我不想生成另一个尺寸为 500x500 的图像。

最佳答案

您可以使用 CSS3 background-size :

.images{
    background:url("image.png") no-repeat scroll;
    background-size:500px 500px;
}

但这将调整图片的大小 AFTER 它已被下载。最好的方法是使用 PHP(或类似工具)在用户下载之前调整图像大小。

如果你不能在服务器端调整它的大小并且你希望它是跨浏览器的,你也可以使用

HTML:

<div class="images">
    <img class="bg" src="image.png" alt="My image" />
    Content
</div>

CSS:

.images{
   position:relative;
}
.images>.bg{
   height:500px;
   width:500px;
   position:absolute;
   top:0;
   left:0;
   z-index:-1;
}

(您可以同时设置heightwidth,但只需要设置其中一个)。

关于html - 调整图像显示的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231578/

相关文章:

html - 通过 HTTPS 获得的 CSS 中的 HTTP 请求

css - 将元素拉伸(stretch)到其父元素之外

html - 将 flexbox 容器缩小到内容大小

html - Css3 flex 属性元素在布局上对齐

Javascript 故障破坏了选择输入的 POST 函数

html - 为什么 z-index 在这里不起作用

javascript - 创建图像下载按钮支持所有浏览器

html - 在CSS中将元素右对齐

html - 如果 parent 的 div 也是 100% 宽度/高度,如何给 child div 宽度/高度 100%

html - 如何设置 Angular ng-template 选择器的样式