如何找到 CSS 精灵的正确偏移量?

发布于 2024-08-03 16:31:54 字数 52 浏览 5 评论 0原文

我将精灵图像设置为页面上元素的背景,但是如何找到正确的偏移量以便我可以在屏幕上实际看到它?

I have a sprite image set as the background of an element on my page, but how do I find the proper offsets so I can actually see it on the screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

給妳壹絲溫柔 2024-08-10 16:31:54

当您在图形程序中构建精灵图像时,您必须记下每个元素的偏移量并在 CSS 中使用它们。

When you build your sprite image in your graphics program you have to write down the offsets for each element and use those in your CSS.

晚雾 2024-08-10 16:31:54

您可以使用这样的工具并获取精灵中图标的背景位置。

您需要首先上传图像,然后从精灵中选择一个图标。 CSS将会生成,只需复制生成的CSS并在您的类中使用它即可。

它将生成这样的 CSS 样式,您需要将其包含在 CSS 类中

background-position: -213px -135px; 
width: 97px;
height: 65px;

在此处输入图像描述

You can use a tool like this and get background positions of the icons in the sprite.

You need to first upload your image, then select an icon from the sprite. CSS will be generated, just copy the generated CSS and use it in your class.

It will generate CSS styles like this, you need to include that in your CSS class

background-position: -213px -135px; 
width: 97px;
height: 65px;

enter image description here

ι不睡觉的鱼゛ 2024-08-10 16:31:54

有一些网络工具可以创建精灵并为您提供带有位置的 CSS。 http://css-sprit.es/ 是一个示例。

There are web tools that will create the sprite and give you CSS with positions for you. http://css-sprit.es/ is an example.

梦在深巷 2024-08-10 16:31:54

在线 CSS Sprite Generator 值得研究一下,它应该可以消除这种方法的一些乏味。

The online CSS Sprite Generator is worth looking into, it should take some of the tedium out of this approach.

孤寂小茶 2024-08-10 16:31:54

要记住的主要一点是偏移量将为。如果您的图像尺寸为 100x500,精灵为 100x100,则偏移量将为:

.img1 { background-position: 0 0; }
.img2 { background-position: 0 -100px; }
.img3 { background-position: 0 -200px; }
.img4 { background-position: 0 -300px; }
.img5 { background-position: 0 -400px; }

The main thing to remember is that the offsets will be negative. If you have an image that's 100x500 with 100x100 sprites, then the offsets will be:

.img1 { background-position: 0 0; }
.img2 { background-position: 0 -100px; }
.img3 { background-position: 0 -200px; }
.img4 { background-position: 0 -300px; }
.img5 { background-position: 0 -400px; }
看海 2024-08-10 16:31:54

由于 png 文件的空白区域占用很少的字节,因此只需按固定间隔放置所有“图像”,例如每 50 或 100 像素。这样你就可以简单地找到第一个正确的值并从中删除 50/100 像素(vim 中的 50 ctrl-x)。

Since empty areas of a png file takes up very few bytes, just put all "images" at a regular interval, say every 50 or 100 pixels. That way you can simply find the first proper value and remove 50/100 pixels from that (50 ctrl-x in vim).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文