CSS Sprite 未设置样式

发布于 2024-10-18 12:11:19 字数 968 浏览 4 评论 0原文

我的 CSS 精灵有问题。看起来它正在工作(嗯,对我来说它看起来应该工作)。所有精灵只显示第一个图标,而不显示其他图标。

查了很多地方都没有找到答案。

谢谢你帮助我。

为了节省时间,我把它放在一个单独的页面上。

[死链接]

对于那些因任何原因无法访问该页面的人,这里是代码:

<div class="iconDiv">
    <a href="http://facebook.com/" title="Facebook Page">
        <img src="./1px.png" class="iconFB linkIcon" alt=""/><span>Facebook</span>
    </a>
</div>
<div class="iconDiv">
    <a href="http://www.flickr.com/" title="Flickr Page">
        <img src="./1px.png" class="iconFL linkIcon" alt=""/><span>Flickr</span>
    </a>
</div>

CSS 在这里(缩短):

.iconFB {background-position:0 -40px;}
.iconFL {background-position:0 -82px;}
.iconRSS {background-position:0 -164px;}
.iconY {background-position:0 -246px;}
.linkIcon {
width: 36px;
height: 36px;
background: url(iconSprite.png) no-repeat top left;
}

非常感谢您的帮助!

I am having a problem with my css sprites. It looks like it is working (well, to me it looks like it should work). All the sprites just show the first icon but not the other ones.

Looked at many places and could not find an answer.

Thanks for helping me.

I put it up on a seperate page to save time.

[dead link]

And for those who aren't able to go to that page for any reason, here is the code:

<div class="iconDiv">
    <a href="http://facebook.com/" title="Facebook Page">
        <img src="./1px.png" class="iconFB linkIcon" alt=""/><span>Facebook</span>
    </a>
</div>
<div class="iconDiv">
    <a href="http://www.flickr.com/" title="Flickr Page">
        <img src="./1px.png" class="iconFL linkIcon" alt=""/><span>Flickr</span>
    </a>
</div>

And the css is here (shortened):

.iconFB {background-position:0 -40px;}
.iconFL {background-position:0 -82px;}
.iconRSS {background-position:0 -164px;}
.iconY {background-position:0 -246px;}
.linkIcon {
width: 36px;
height: 36px;
background: url(iconSprite.png) no-repeat top left;
}

Thank you very much for helping!

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

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

发布评论

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

评论(4

追星践月 2024-10-25 12:11:19

您遇到CSS 特异性问题

像这样的CSS:

.iconFB {
    background-position:0 -40px;
}

被这样覆盖:

.linkIcon {
    ..
    background: url(iconSprite.png) no-repeat top left;
    ..
}

因为background是(除其他外)background-position的简写。

解决此问题的最简单方法是交换两个 CSS 块,如下所示:

.linkIcon {
    width: 36px;
    height: 36px;
    cursor: pointer;
    cursor: hand;
    vertical-align:middle;
    background: url(iconSprite.png) no-repeat top left;
}
.iconFB {
    background-position:0 0;
}
.iconFL {
    background-position:0 -82px;
}
.iconRSS {
    background-position:0 -164px;
}
.iconY {
    background-position:0 -246px;
}

(我修复了 Facebook 图标的位置,从 0 -40px0 0< /代码>)

You have a CSS specificity problem.

The CSS like this:

.iconFB {
    background-position:0 -40px;
}

is being overridden by this:

.linkIcon {
    ..
    background: url(iconSprite.png) no-repeat top left;
    ..
}

because background is shorthand for (amongst other things) background-position.

The easiest way to fix this is to swap around the two blocks of CSS, like this:

.linkIcon {
    width: 36px;
    height: 36px;
    cursor: pointer;
    cursor: hand;
    vertical-align:middle;
    background: url(iconSprite.png) no-repeat top left;
}
.iconFB {
    background-position:0 0;
}
.iconFL {
    background-position:0 -82px;
}
.iconRSS {
    background-position:0 -164px;
}
.iconY {
    background-position:0 -246px;
}

(I fixed the position of the Facebook icon, from 0 -40px to 0 0)

陈甜 2024-10-25 12:11:19

.linkIcon.iconFB 和 .linkIcon.iconFL 等将解决这个问题。目前,您的 linkIcon 样式会覆盖您在 .iconFB 中设置的位置,因为它位于 CSS 文件的更下方。

编辑:(或者只是将 iconFB 等样式移到 .linkIcon 样式下方)

.linkIcon.iconFB and .linkIcon.iconFL and so on will solve this. Currently your linkIcon style overwrites the positions you set in .iconFB because it's further down in the CSS file.

Edit: (or just move the iconFB etc. styles below the .linkIcon styles)

往日 2024-10-25 12:11:19

背景图像的位置被 .linkIcon 中的简写背景覆盖。

重新排序 CSS,或者在 .linkIcon 中使用单独的背景图像和背景重复声明(并且没有背景位置)。我认为后者更可取。

The position of the background image is being overridden by the shorthand background in .linkIcon.

Either re-order the CSS, or use separate background-image and background-repeat declarations (and no background-position) in .linkIcon. I'd argue the latter is preferable.

泪意 2024-10-25 12:11:19

另一种方法是使用 id,因为这些图标可能是唯一的并且不会在同一页面上重复使用。由于它用于“识别”某个图标,因此使用 ID 而不是类是有意义的。

<style>
    #iconFB {
        background-position: 0 0px;
    }

    #iconFL {
        background-position: 0 -82px;
    }

    #iconRSS {
        background-position: 0 -164px;
    }

    #iconY {
        background-position: 0 -246px;
    }
    .linkIcon {
    width: 36px;
    height: 36px;
    cursor: pointer;
    cursor: hand;
    vertical-align: middle;
    background: url(iconSprite.png) no-repeat top left;
}
</style>

<div class="iconDiv">
    <a href="http://facebook.com/" title="Facebook Page">
        <img src="./1px.png" class="linkIcon" id="iconFB" alt=""/><span>Facebook</span>
    </a>
</div>
<div class="iconDiv">
    <a href="http://www.flickr.com/" title="Flickr Page">
        <img src="./1px.png" id="iconFL" class="linkIcon" alt=""/><span>Flickr</span>
    </a>
</div>
<div class="iconDiv">
    <a href="#" title="RSS Page">
        <img src="./1px.png" id="iconRSS" class="linkIcon" alt=""/><span>RSS</span>
    </a>
</div>
<div class="iconDiv">
    <a href="http://www.youtube.com/" title="Youtube Page">
        <img src="./1px.png" id="iconY" class="linkIcon" alt=""/><span>YouTube</span>
    </a>
</div>

Another way would be to use id's as these icons are probably unique and not reused on the same page. As it is used to "identify" a certain icon it makes sense to use an ID instead of a class.

<style>
    #iconFB {
        background-position: 0 0px;
    }

    #iconFL {
        background-position: 0 -82px;
    }

    #iconRSS {
        background-position: 0 -164px;
    }

    #iconY {
        background-position: 0 -246px;
    }
    .linkIcon {
    width: 36px;
    height: 36px;
    cursor: pointer;
    cursor: hand;
    vertical-align: middle;
    background: url(iconSprite.png) no-repeat top left;
}
</style>

<div class="iconDiv">
    <a href="http://facebook.com/" title="Facebook Page">
        <img src="./1px.png" class="linkIcon" id="iconFB" alt=""/><span>Facebook</span>
    </a>
</div>
<div class="iconDiv">
    <a href="http://www.flickr.com/" title="Flickr Page">
        <img src="./1px.png" id="iconFL" class="linkIcon" alt=""/><span>Flickr</span>
    </a>
</div>
<div class="iconDiv">
    <a href="#" title="RSS Page">
        <img src="./1px.png" id="iconRSS" class="linkIcon" alt=""/><span>RSS</span>
    </a>
</div>
<div class="iconDiv">
    <a href="http://www.youtube.com/" title="Youtube Page">
        <img src="./1px.png" id="iconY" class="linkIcon" alt=""/><span>YouTube</span>
    </a>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文