奇怪的 CSS 行为(带有翻转的精灵导航)

发布于 2024-07-30 18:39:32 字数 2520 浏览 1 评论 0原文

这是有问题的导航图像:

http://img.skitch.com/20090807-t8e6d9ymrtpdifqy88xpu6x36.png

我想做的事情非常基本,而且我过去已经做过很多次了,我只是不明白为什么它现在不起作用。 基本上使用上面的图像作为导航背景并相应地调整宽度和背景位置。 这是我的 CSS:

#navigation { width: 960px; height: 28px; clear: both; background: url('../images/nav-bg.png') repeat-x; }

        #navigation ul { margin: 0 0 0 20px; padding: 0; }
            #navigation ul li { float: left; list-style: none; }
            #navigation ul li a { display: block; height: 28px; background: url('../images/nav-tabs.png') no-repeat; text-indent: -9999px;}

                #nav-home { width: 62px; }
                    #nav-home.active, #nav-home:hover { background-position: 0 -28px; }

                #nav-cp { width: 130px; background-position: -62px 0; }
                    #nav-cp.active, #nav-cp:hover { background-position: -62px -28px; }

                #nav-web { width: 106px; background-position: -192px 0; }
                    #nav-web.active, #nav-web:hover { background-position: -192px -28px; }

                #nav-clix { width: 106px; background-position: -298px 0; }
                    #nav-clix.active, #nav-clix:hover { background-position: -298px -28px; }

                #nav-dna { width: 90px; background-position: -405px 0; }
                    #nav-dna.active, #nav-dna:hover { background-position: -405px -28px; }

这是页面代码,带有通用 HTML5 文档类型 ,指定用于将来的校样:

<div id="navigation">

    <ul id="nav-tabs">
        <li><a href="#" id="nav-home">Home</a></li>
        <li><a href="#" id="nav-cp">Client Portal</a></li>
        <li><a href="#" id="nav-web">Weboptima</a></li>
        <li><a href="#" id="nav-clix">Clixfactor</a></li>
        <li><a href="#" id="nav-dna">Lead DNA</a></li>
    </ul>

</div>

我遇到的奇怪的事情是: 第一个选项卡“主页”运行完美。 除非我指定 !important,否则其余四个选项卡不遵守初始背景位置属性,但翻转效果很好。 以下分别是这两种情况的图像:

http://img.skitch.com/20090807- fybag852bbbi6ut751w167y1hp.png

http://img.skitch.com/20090807-rmn9b2tu54q4agyta2idfra5x5.png

只是想深入了解这个(希望)简单的问题。 提前致谢!

Here's the navigation image in question:

http://img.skitch.com/20090807-t8e6d9ymrtpdifqy88xpu6x36.png

What I want to do is pretty basic and I've done it numerous times in the past, I just can't understand why it isn't working right now. Basically use the above image as the nav background and adjust the widths and background-positions accordingly. Here is my CSS:

#navigation { width: 960px; height: 28px; clear: both; background: url('../images/nav-bg.png') repeat-x; }

        #navigation ul { margin: 0 0 0 20px; padding: 0; }
            #navigation ul li { float: left; list-style: none; }
            #navigation ul li a { display: block; height: 28px; background: url('../images/nav-tabs.png') no-repeat; text-indent: -9999px;}

                #nav-home { width: 62px; }
                    #nav-home.active, #nav-home:hover { background-position: 0 -28px; }

                #nav-cp { width: 130px; background-position: -62px 0; }
                    #nav-cp.active, #nav-cp:hover { background-position: -62px -28px; }

                #nav-web { width: 106px; background-position: -192px 0; }
                    #nav-web.active, #nav-web:hover { background-position: -192px -28px; }

                #nav-clix { width: 106px; background-position: -298px 0; }
                    #nav-clix.active, #nav-clix:hover { background-position: -298px -28px; }

                #nav-dna { width: 90px; background-position: -405px 0; }
                    #nav-dna.active, #nav-dna:hover { background-position: -405px -28px; }

And here is the on-page code, with the generic HTML5 doctype, <!DOCTYPE html>, specified for future proofing:

<div id="navigation">

    <ul id="nav-tabs">
        <li><a href="#" id="nav-home">Home</a></li>
        <li><a href="#" id="nav-cp">Client Portal</a></li>
        <li><a href="#" id="nav-web">Weboptima</a></li>
        <li><a href="#" id="nav-clix">Clixfactor</a></li>
        <li><a href="#" id="nav-dna">Lead DNA</a></li>
    </ul>

</div>

The weird things I've come across are: The first tab, Home, works perfectly. The remaining four tabs don't obey the initial background-position property unless I specify !important, but the rollovers work just fine. Here are images of these two situations, respectively:

http://img.skitch.com/20090807-fybag852bbbi6ut751w167y1hp.png

http://img.skitch.com/20090807-rmn9b2tu54q4agyta2idfra5x5.png

Just looking for a little insight into this (hopefully) simple problem. Thanks in advance!

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

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

发布评论

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

评论(2

嘿看小鸭子会跑 2024-08-06 18:39:32

您指定背景图像的样式比选项卡的样式更具体,因此它具有优先性。

不要使用复合样式 background(默认情况下也会将 background-position 设置为 0% 0%),而是指定单独的组件:

background-image: url('../images/nav-tabs.png'); background-repeat: no-repeat;

您可以此处了解特异性。

The style where you specify the background image is more specific than the styles for the tabs, so it takes presedence.

Instead of using the composite style background, that also sets background-position by default to 0% 0%, specify the separate components:

background-image: url('../images/nav-tabs.png'); background-repeat: no-repeat;

You can read about specificity here.

枫以 2024-08-06 18:39:32

我不知道问题到底是什么,但这听起来像是 CSS 规则的相对优先级的问题。

对于此类问题, Firebug 非常出色 - 它会告诉您对于任何给定元素,到底触发了哪些 CSS 规则它。 这应该可以帮助您了解问题所在。

(如果您已经了解 Firebug,请原谅我,但令人惊讶的是,数量惊人的 Web 开发人员并不了解。)

I don't know exactly what the problem is, but is sounds like an issue with the relative priority of your CSS rules.

For that sort of problem, Firebug is excellent - it will tell you for any given element exactly which CSS rules are firing for it. That should help you see where the problem lies.

(Forgive me if you already know about Firebug, but a surprising number of web developers don't.)

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