无法让 CSS Sprite 工作..我做错了什么?
我正在使用 CSS Sprite Generator 为我正在处理的网页创建精灵,但它没有似乎不起作用,我不知道为什么......我想这是显而易见的事情,但是......!
所以,我选择了 3 个图像,压缩,生成了 PNG 文件(我检查了结果,看起来不错),然后我得到了以下 css 类:
.sprite-welcom1 { background-position: 0 -30px; }
.sprite-welcom3 { background-position: 0 -109px; }
.sprite-3 { background-position: 0 -188px; }
所以这是我正在测试的 HTML,出于某种原因,我所有的得到的是一个漂亮的空白页:
<html>
<head>
<style>
.sprite-welcom1 { background-position: 0 -30px; }
.sprite-welcom3 { background-position: 0 -109px; }
.sprite-3 { background-position: 0 -188px; }
.sprite-bg {
background: url(csg-495a902b04181.png) no-repeat top left;
}
</style>
</head>
<body>
<div class="sprite-bg sprite-3"></div>
</body>
</html>
有什么提示吗?
I am using CSS Sprite Generator to create sprites for a web page I am working on, but it doesn't seem to work, and I don't know why...I guess it's something obvious but..!
So, I picked up 3 images, zipped, generated the PNG file (I checked out the result it is seems fine) and I got the following css classes back:
.sprite-welcom1 { background-position: 0 -30px; }
.sprite-welcom3 { background-position: 0 -109px; }
.sprite-3 { background-position: 0 -188px; }
So here is the HTML I am testing on, and for some reason all I get is a nice blank page:
<html>
<head>
<style>
.sprite-welcom1 { background-position: 0 -30px; }
.sprite-welcom3 { background-position: 0 -109px; }
.sprite-3 { background-position: 0 -188px; }
.sprite-bg {
background: url(csg-495a902b04181.png) no-repeat top left;
}
</style>
</head>
<body>
<div class="sprite-bg sprite-3"></div>
</body>
</html>
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不要使用发电机。 花时间从头开始自己做。 然后下次你使用这个方法时,你就会知道当出现问题时要寻找什么,并且你会回答你自己的问题,而且,也许其他人在 Stack Overflow 上。
该生成器不会为您的样式生成任何有意义的类名称,这意味着如果您稍后去编辑它们,您将不会知道周二的两个类发生了什么,除非您记住了这些数字。 当您稍后返回样式表时,有意义的名称将让您省去麻烦。
打开 Photoshop、GIMP 或几乎所有现代图像编辑软件。 确保您的程序已打开标尺选项,并以这种方式测量元素的背景图像坐标。 在没有统治者的情况下 - 这可能是罕见的,你总是可以使用 MeasureIt Firefox 扩展和在选项卡中打开的 .png。
Don't use a generator. Take the time to do it yourself from scratch. Then next time you use this method, you'll know what to look for when something has gone wrong and you'll answer your own question, and heck, maybe someone else's on Stack Overflow.
This generator isn't producing any meaningful class names for your styles, which means if you go to edit them later you're not going to know two classes from Tuesday what's going on unless you've memorized the numbers. Meaningful names will save you headaches when you return to your stylesheet later on.
Open up Photoshop, GIMP, or almost any modern image editing software. Make sure your program has the rulers option switched on, and measure your element's background image coordinates this way. In the absence of rulers - which is probably a rarity, you could always fudge around with the MeasureIt Firefox extension and the .png opened in a tab.
定义
的宽度和高度。
Define a width and height for
<div class="sprite-bg sprite-3">
.background-position
的.sprite-bg
规则,设置为background
复合规则的一部分(左上角
code> 部分),比.sprite-3
的独立background-position
设置具有更高的优先级,因为它稍后出现在样式表中。首先放置
.sprite-bg
的规则。Your
.sprite-bg
rule forbackground-position
, set as part of the composite rule forbackground
(thetop left
part), has higher precedence than the stand-alonebackground-position
setting for.sprite-3
because it appears later in the stylesheet.Place the rule for
.sprite-bg
first.div 是空的。 把东西放进去。 就像空间(
)。
the div is empty. put something inside. like space (
).
您必须为
div
元素声明高度
和宽度
。 就是这样。You have to declare a
height
andwidth
for thediv
element. That's it.嗯,不太确定你想在这里实现什么目标。 多类似乎有点混乱。 我在下面发布了我的精灵图方法,看看这是否满足您的需要。 通常这涉及元素的组合,最常见的是带有导航链接的无序列表,但为此目的它只是 div。
另外不要忘记背景位置的顺序。
背景位置:|顶部| |左|;
这已经把我搞砸了好几次了。 最后,A List Apart 有一篇关于 Sprite 地图的精彩文章 (http://www.alistapart.com/文章/精灵/)
Hrm, not quite sure what you're trying to achieve here. The multiclassing seems a bit messy. I've posted my method of spritemaps below, see if this does what you need. Usually this involves a combination of elements, the most common being an unordered list with links for navigation, but for this purpose it's just divs.
Also don't forget the order of background-position.
background-position: |top| |left|;
That's screwed me up a couple of times. Finally, A List Apart has a great article on Sprite Maps (http://www.alistapart.com/articles/sprites/)
正如其他人提到的,您的 sprite-bg 元素需要有一些内容来赋予它高度/宽度,或者在 css 中指定这些属性,因此:
正如其他人提到的,我会移动 .sprite 的规则-welcom1、.sprite-welcom3 和
.sprite-3 位于样式表中主 .sprite-bg 的下方。
As others have mentioned, your sprite-bg element needs to have either some content to give it a height / width, or have those properties specified in the css, thusly:
As someone else mentioned, I'd move the rules for the .sprite-welcom1, .sprite-welcom3 and
.sprite-3 to beneath the main .sprite-bg in the stylesheet.
由于某些原因,Firefox 3 有时需要 CSS 选择器中的 2 个类来使精灵图正常工作。 我的预感是,在加载精灵地图时,特异性规则会导致问题。 通过添加附加类,它可以正常工作。
用父类“命名空间”你的 CSS 总是好的,以避免在其他地方意外地设置 DOM 标签的样式。 以下是对上面每个人的想法的一些额外增强。
此代码适用于:IE 7、Firefox 3、Google Chrome 1、Opera 9 和 Safari 3.
提示:在 URL 中避免使用 http:// 将允许通过 http:// 和 https:// 连接提供 sprite 地图。
(向右滚动可看到“
no-repeatscroll 0 0;
”)For some reason, Firefox 3 sometimes wants 2 classes in the CSS Selector to make the sprite map work. My hunch is that the rules of specificity are causing problems while the sprite map loads. By adding the additional class, it works correctly.
It's always good to “namespace” your CSS with a parentClass, to avoid unexpectedly styling a DOM tag someplace else. Here are some additional enhancements to everyones ideas from above.
This code works in: IE 7, Firefox 3, Google Chrome 1, Opera 9 & Safari 3.
Tip: Avoiding http:// in the URL will allow the sprite map to be served up from both http:// and https:// connections.
(Scroll to the right to see the “
no-repeat scroll 0 0;
” )