gpt4 book ai didi

css - "background-size:cover"会减小文件大小吗?

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:10 26 4
gpt4 key购买 nike

例如,我有 1080x1920px 尺寸的图片,我的 div 是 100x100px。那么“background-size:cover”会加载 100x100px 文件,还是 1080x1920px 文件,然后将其应用于 div?如果我想覆盖我的 div,如何减小图像文件的大小?

最佳答案

background-size:cover = 将背景图像缩放到尽可能大,以便背景区域完全被背景图像覆盖。背景图片的某些部分可能在背景定位区域内不可见

基本上它会放大,直到最里面的边缘接触到侧面,这意味着一些图像可能会被截断,而不像 100% 那样所有图像都可见。

看这个LINK例如


CSS3 背景大小

这可以在 CSS3 中使用 background-size 实现。

所有现代浏览器都支持这一点,因此除非您需要支持旧浏览器,否则就是这样做的方式。

支持的浏览器:
<支持>Mozilla Firefox 4.0+ (Gecko 2.0+)、Microsoft Internet Explorer 9.0+、Opera 10.0+、Safari 4.1+ (webkit 532) 和 Chrome 3.0+。

.stretch{
/* Will stretch to specified width/height */
background-size: 200px 150px;
}
.stretch-content{
/* Will stretch to width/height of element */
background-size: 100% 100%;
}

.resize-width{
/* width: 150px, height: auto to retain aspect ratio */
background-size: 150px Auto;
}
.resize-height{
/* height: 150px, width: auto to retain aspect ratio */
background-size: Auto 150px;
}
.resize-fill-and-clip{
/* Resize to fill and retain aspect ratio.
Will cause clipping if aspect ratio of box is different from image. */
background-size: cover;
}
.resize-best-fit{
/* Resize to best fit and retain aspect ratio.
Will cause gap if aspect ratio of box is different from image. */
background-size: contain;
}

特别是,我喜欢 covercontain 值,它们赋予我们以前没有的新控制权。

圆形

您还可以使用 background-size: round 与 repeat 结合使用:

.resize-best-fit-in-repeat{
/* Resize to best fit in a whole number of times in x-direction */
background-size: round auto; /* Height: auto is to keep aspect ratio */
background-repeat: repeat;
}

这将调整图像宽度,使其适合背景定位区域的整数倍。


补充说明
如果您需要的大小是静态像素大小,那么物理调整实际图像的大小仍然是明智的。这既是为了提高调整大小的质量(假设您的图像软件比浏览器做得更好),也是为了在原始图像大于显示内容时节省带宽。

关于css - "background-size:cover"会减小文件大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26024930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com