CSS:调整大小
我是否可以调整精灵中
的背景图像的大小?我有这个 HTML:
<li class="bg_image"></li>
这是 CSS:
.bg_image {background:url('sprite.png') no-repeat;
background-position:-422px -46px;width:32px;height:32px;}
精灵上的图像的宽度和高度为 48px,因为在网站的其他地方使用。我宁愿使用相同的方法,而不是对尺寸较小的相同图像发出新的 HTTP 请求,或者增加精灵的大小。
我可以将其大小从 48px 宽度和高度调整为 32px 吗?
多谢
Is it possible I can resize a background image of a <li>
of which is in a sprite?
I have this HTML:
<li class="bg_image"></li>
and this is the CSS:
.bg_image {background:url('sprite.png') no-repeat;
background-position:-422px -46px;width:32px;height:32px;}
The image on the sprite has width and height 48px because is used elsewhere in the site. I'd rather use the same instead of making a new HTTP request of the same image with smaller size, or increase the size of the sprite.
Is it possible I can resize it from 48px width and height to 32px ?
Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 CSS3 背景大小属性可以轻松完成此操作。
创建一个新的 CSS 类,它也使用相同的(更大的)精灵图像,但背景尺寸减小了。例如,如果您的原始精灵是 640 像素 x 320 像素,您可以在 CSS 中执行此操作:
注意:由于您有效地将背景精灵减小到新的大小,因此背景位置将发生变化。然后您需要重新计算新的位置。对于您的情况,您将减少 33.33% (48px->32px),因此职位也会减少 -33.33%。
最后,在您的 html 中,只需执行以下操作:
这应该可以解决问题,至少对于现代浏览器而言。
This can be easily done using the CSS3 background-size property.
Create a new CSS class which is also using the same (larger) sprite image, but with the background size reduced. E.g. If your original sprite is 640px by 320px, you might do this in the CSS:
Note: Since you're effectively reducing the background sprite to a new size, the background positions will therefore change. You'd then need to re-calculate the new positions. For your case you're doing a 33.33% (48px->32px) reduction and thus the positions are going to be -33.33% off as well.
Finally, in your html, simply do:
That should do the trick, at least for modern browsers.
AFAIK 在当前浏览器中不可能。至少你的 HTML 不是这样。您可以在
li
中设置一个具有适当宽度和高度的背景(z 索引)。图像还需要具有位置:绝对,并以带有位置:相对的跨度为父级,以及溢出:隐藏...以及更多的调整。
我想说你应该制作更多具有正确尺寸图标的图像,或者将具有不同尺寸图标的行添加到你的 sprite.png 中。
AFAIK not possible in current browsers. Not with your HTML at least. You could have a background (z-indexed)
<img src="sprite.png" />
in yourli
that would have appropriate width and height. The image would also need to have position:absolute and be parented with span with position:relative, and overflow:hidden... And a bit more tweaking.I would say you should make more images with correct sizes of icons or add rows with different sizes of icons to your sprite.png.