3

出于某种原因,我不完全确定为什么,但以下不起作用。背景位置在悬停时保持不变。我不知道为什么。我可以用另一种方式来做,但我想尝试找出它为什么不起作用的原因。

#nav a:link, #nav a:visited {
    background:url(../img/nav-sprite.png) no-repeat;
    display:block;
    float:left;
    height:200px;
    padding:10px;
    text-indent:-9999px;
    border:solid 1px red;
}

    #nav a#home {
        background-position:-10px 0px;
        width:30px;
    }
    #nav a#about-us {
        background-position:-85px 0px;
        width:45px;
    }

#nav a:hover    {
    background-position:1px -15px;
}

有人知道是什么原因造成的吗?

提前致谢!

瑞安

4

4 回答 4

7

ID 选择器优先于伪类选择器。

因此该#规则不会被:规则覆盖..

要么使用 !important 指令

#nav a:hover    {
    background-position:1px -15px!important;
}

或使规则更具体

#nav a#home:hover, #nav a#about-us:hover    {
    background-position:1px -15px;
}
于 2010-04-15T10:48:40.070 回答
3

#nav a#home并且#nav a#about-us#nav a:hover(id > pseudo-class) 具有更高的特异性,所以后者永远不会被应用。#nav a#home:hover#nav a#about-us:hover会在这里工作。

级联

于 2010-04-15T10:46:06.487 回答
1

尝试这个:

#nav a#home:hover, #nav a#about-us:hover    {
    background-position:1px -15px;
}
于 2010-04-15T10:47:15.737 回答
0

I know it's not relevant anymore but if someone will get here so there is one more option:

Change the Ids to Classes. Intead of <a id="home"> use <a class="home">. Now change your styles from a#home to a.home and it will work.

Full code:

<div id="nav">
    <a href="#" class="home">Home</a>
    <a href="#" class="about-us">About Us</a>
</div>

<style>
        #nav a:link, #nav a:visited
        {
            background: url('smile-icon.jpg') no-repeat;
            display: block;
            float: left;
            height: 140px;
            padding: 10px;
            text-indent: -9999px;
            border: solid 1px red;
        }

        #nav a.home
        {
            background-position: -10px 0px;
            width: 30px;
        }

        #nav a.about-us
        {
            background-position: -85px 0px;
            width: 45px;
        }

        #nav a:hover
        {
            background-position: 1px -15px;
        }
    </style>
于 2013-06-27T18:54:52.303 回答