gpt4 book ai didi

css - 响应式 Sprite 背景图片,如何

转载 作者:太空狗 更新时间:2023-10-29 15:05:54 26 4
gpt4 key购买 nike

您好,我在一个容器中有两列内容,第一列是文本,第二列是带有背景 Sprite 图像的 span。问题是当我使用较小的屏幕分辨率时,我希望背景 Sprite 图像的宽度百分比能够与 H5 一起缩放百分比宽度,有没有办法做到这一点?

h5{
float:left;
display:block;
width:800px;
}

.sprite{
background-image: url("assets/img/website_sprite_a.png");
background-position: -60px -60px;
float:left;
display:block;
width:64px;
}

<div class="container">
<h5>Title
</h5>
<span class="sprite">
</span>
</div>

最佳答案

在你的情况下,我会使用单个 background-image,但如果你有很多图像或者你真的想这样做,你可以使用 background-大小属性。

来自 MDN :

The background-size CSS property specifies the size of the background images. The size of the image can be fully constrained or only partially in order to preserve its intrinsic ratio.

.sprite{
background-image: url("assets/img/website_sprite_a.png");
background-position: -30% -30%; //use % instead pixels
float:left;
display:block;
width:64px;
background-size: 100%; //play with this
}

你还应该阅读这个:

我玩过这个 on JSFIddle .调整浏览器大小以查看效果。

关于css - 响应式 Sprite 背景图片,如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476210/

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