0

编辑:尝试将高度更改为 27px,因为 kennypu 说这是一个问题,还没有运气。

我有一个图标精灵,我正在尝试获取其中的一部分,因此我可以将它们用于导航。但是,它似乎获得了太多的精灵,或者根本没有进入导航栏,我阅读了很多文章,观看了很多视频,阅读了很多 SO 问题,但似乎无法修复它。

JSfiddle 链接是:http: //jsfiddle.net/m5kbX/

精灵的链接是:https ://www.dropbox.com/s/t4fowiw1zda3qcq/icons.png

html也是:

<div id="header-right">
            <ul id="navigation">
                <li>
                    <div class="nav-button">
                        <div id="schools">
                        </div>
                    </div>
                    <div class="nav-text">
                        Schools
                    </div>
                </li>
                <li>
                    <div class="nav-button">
                        <div id="professors">
                        </div>
                    </div>
                    <div class="nav-text">
                        Professors
                    </div>
                </li>
                <li>
                    <div class="nav-button">
                        <div class="Programs">
                        </div>
                    </div>
                    <div class="nav-text">
                        Programs
                    </div>
                </li>
                <li>
                    <div class="nav-button">
                        <div class="account">
                        </div>
                    </div>
                    <div class="nav-text">
                        My Account
                    </div>
                </li>
            </ul>
        </div>

CSS是:

#header-right{
    float: right;
    width: 381px;
    height: 64px;
    background-image:url('http://localhost/gradebyme/gradebyme/public/img/midtile2.png');
    background-repeat:repeat-x;
}

#navigation{
    position: relative;
    background: url('http://localhost/gradebyme/gradebyme/public/img/icontest.png') no-repeat;
    margin: 0;
    padding: 0;
    list-style: none;
}

#navigation li{
    width: 88px;
    height: 64px;
    display: inline-block;
    text-align: center;
}

.nav-button{
    width: 88px;
    height: 40px;
}

.nav-text{
    width: 88px;
    height: 24px;
    color:white;
}

#schools{
    float: left;
    width: 37px;
    height: 26.75px;
    background-position: 0 0;
}

#professors{
    float: left;
    width: 37px;
    height: 26.75px;
    background-position: 0 -27px;
}

#programs{
    float: left;
    width: 37px;
    height: 26.75px;
    background-color: green;
    background-position: 0 -55px;
}

#account{
    float: left;
    width: 37px;
    height: 26.75px;
    background-color: purple;
    background-position: 0 -83px;
}

精灵的第一个点是显示前 2 个半图标,并溢出容器,而其余的则什么也不显示。请帮助并尽可能多地给我关于 css sprites 和 css 的建议!学习是最好的方法!不只是修理你知道吗?

4

2 回答 2

1

你还没有给他们背景。

    #schools{
        float: left;
        width: 37px;
        height: 26.75px;
        background-position: 0 0;
background: ????
    }

    #professors{
        float: left;
        width: 37px;
        height: 26.75px;
        background-position: 0 -27px;
background: ????
    }

    #programs{
        float: left;
        width: 37px;
        height: 26.75px;
        background-color: green;
        background-position: 0 -55px;
background: ????
    }

    #account{
        float: left;
        width: 37px;
        height: 26.75px;
        background-color: purple;
        background-position: 0 -83px;
background: ????
    }
于 2012-12-29T03:54:44.730 回答
0

好的,这有很多问题,从糟糕的 css 选择器(大小写很重要,id 和类不同!)到背景图像没有应用于正确的元素......

我已经将它调试为我认为它应该从原始代码中工作:更新的 Fiddle,要让小提琴工作,下载海报提供的精灵并使用本地服务器放入计算机上的“localhost/img”目录运行,如 WAMP。

HTML:

<div id="header-right">
    <ul id="navigation">
        <li id="schools" class="nav-text">
            <div class="nav-button">
            </div>
            Schools
        </li>
        <li id="professors" class="nav-text">
            <div class="nav-button">
            </div>
            Professors
        </li>
        <li id="programs" class="nav-text">
            <div class="nav-button">
            </div>
            Programs
        </li>
        <li id="account" class="nav-text">
            <div class="nav-button">
            </div>
            My Account
        </li>
    </ul>
</div>​

CSS:

#header-right{
    float: right;
    width: 381px;
    height: 64px;
    background-image:url('http://localhost/img/midtile2.png');
    background-repeat:repeat-x;
    background-color: grey;
    border: 1px solid;
}

#navigation{
    position: relative;
    margin: 0;
    padding: 0;
    list-style: none;
}

#navigation li{
    display: inline-block;    
    float: left;
    text-align: center;    
    width: 88px;
    height: 64px;
}

#navigation li .nav-button{    
    background-image: url('http://localhost/img/icons.png');
    background-repeat: no-repeat;    
    width: 37px;
    height: 27px;
    margin: 0 auto 13px;

}
.nav-text{
    color: white;
}

#schools .nav-button{
    background-position: 0 0;
}

#schools .nav-button:hover{
    background-position: -37px 0;
}

#schools .nav-button:active{
    background-position: -74px 0;
}

#professors .nav-button{
    background-position: 0 -27px;
}

#professors .nav-button:hover{
    background-position: -37px -27px;
}

#professors .nav-button:active{
    background-position: -74px -27px;
}

#programs .nav-button{
    background-position: 0 -55px;
}

#programs .nav-button:hover{
    background-position: -37px -55px;
}

#programs .nav-button:active{
    background-position: -74px -55px;
}

#account .nav-button{
    background-color: black;
    background-position: 0 -83px;
}

#account .nav-button:hover{
    background-position: -37px -83px;
}

#account .nav-button:active{
    background-color: #1ca4de;
    background-position: -74px -83px;
}
于 2012-12-29T04:15:53.020 回答