CSS Sprites获得了太多的sprite,或者根本没有

时间:2012-12-29 03:06:42

标签: html css sprite

编辑:试图将高度改为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;
}

带有精灵的第一个位置,显示前两个半图标,并且溢出容器,而其余部分则没有显示任何内容。请帮助并尽可能多地给我关于css sprites和css的建议!学习是最好的方法!不只是修复你知道吗?

2 个答案:

答案 0 :(得分: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: ????
    }

答案 1 :(得分:0)

好的,这有很多问题,从糟糕的css选择器(案例事项,id和类不同!)到背景图像没有应用到正确的元素......

我调试了它,因为我认为它应该来自原始代码:updated 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;
}