如何让这个<span>元素跨越100%的LI?

时间:2015-05-21 21:34:25

标签: html css navigation

我正在使用这个导航栏,我差不多完成了,但我只有一个问题需要解决。我的导航使用jQuery,代码以<SPAN>元素为目标打开子菜单。我的问题是,由于某种原因,跨度不包括整个LI区域。你可以点击一大堆“空”空间,一切都没有发生。如何让span覆盖整个LI区域?我已经尝试弄乱宽度,填充和边距,但没有一个工作。

这是jsFiddle

CSS

#navbar {
        display: block;
        clear: both;
        width: 100%;
        height: auto;
        margin: 0px;
        padding: 0%; 
        background-color: #29568F;
        border-bottom: 3px solid #29568F;
    }

    #menu-icon {
        display: inline-block;
        float: left;
    }

    #menu-icon img {
        height: 16px;
        width: 21px;
        align: center;
        margin: 0px 10px 0px 0px;
        position: relative;
        top: 2px;
    }

    #empty-tab{

    }

    .nav-tabs {
        display: inline-block;
        position: relative;
        width: 100%;
        background-color: #29568F;
        margin: 0px 0px;
        padding: 0px;
        list-style-type: none;
        color: white;
        text-decoration: none;
        text-shadow: 2px 2px #000000;
        font: 18px arial, verdana, sans-serif;
    }

    .nav-tabs li {
        float: left;
        cursor: pointer;
        padding: 10px 20px;
        text-align: center;
        border-right: 1px solid lightgrey;
    }

    .nav-tabs li span {
      border: 1px solid red;
    }

    .nav-tabs li:last-child {
        border: 0px;
    }

    .nav-tabs li:hover,
    .nav-tabs > .open {
        background-color: #3399CC;
    }

    #active {
        background-color: #3399CC;
    }

    .nav-tabs li a {
        color: white;
        text-decoration: none;
        text-shadow: 2px 2px #000000;
        font: 18px arial, verdana, sans-serif;
    }

HTML

<div id="navbar">
    <ul class="nav-tabs">
        <li id="menu-icon"><span><img src="/images/menu-icon.png">Menu</span></li>
        <li><span>Dogs <div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>Meet the Breeds<div class="arrow-closed"></div></span>
                    <ul class="drop-menu">
                        <li><span>Sort A - Z <div class="arrow-closed"></div></span>
                            <ul class="slide-menu">
                                <li>Breeds A - F</li>
                                <li>Breeds G - L</li>
                                <li>Breeds M - R</li>
                                <li>Breeds S - Z</li>
                            </ul>
                        </li>
                        <li><span>Sort by AKC Group <div class="arrow-closed"></div></span>
                            <ul class="slide-menu">
                                <li>Sporting Group</li>
                                <li>Working Group</li>
                                <li>Herding Group</li>
                                <li>Hound Group</li>
                                <li>Terrier Group</li>
                                <li>Non-Sporting Group</li>
                                <li>Toy Group</li>
                            </ul>
                        </li>
                        <li><span>Sort by Size <div class="arrow-closed"></div></span>
                            <ul class="slide-menu">
                                <li>X-Small (&le 10in)</li>
                                <li>Small (10in &gt &lt 15in)</li>
                                <li>Medium (15in &ge &lt 21in)</li>
                                <li>Large (21in &ge &lt 28in)</li>
                                <li>X-Large (28in +)</li>
                            </ul>
                        </li>
                        <li><span>Sort by Coat <div class="arrow-closed"></div></span>
                            <ul class="slide-menu">
                                <li>Very Short/Hairless</li>
                                <li>Short Coat</li>
                                <li>Medium Coats</li>
                                <li>Long Coats</li>
                                <li>Non-Shedding Coats</li>
                                <li>Curly Coats</li>
                            </ul>
                        </li>
                        <li><span>Sort by Trait <div class="arrow-closed"></div></span>
                            <ul class="slide-menu">
                                <li>Apartment Suitable</li>
                                <li>Laid Back</li>
                                <li>Athletic</li>
                                <li>Protective</li>
                                <li>Extroverted</li>
                                <li>Pet Friendly</li>
                                <li>Cuddle-Buddies</li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li><span>Supplies<div class="arrow-closed"></div></span>
                    <ul class="drop-menu">
                        <li><span>Crates & Kennels</span></li>
                        <li><span>Bowls & Dishes</span></li>
                        <li><span>Collars & Leashes</span></li>
                        <li><span>Toys & Games</span></li>
                        <li><span>Grooming</span></li>
                        <li><span>Apparal & Accessories</span></li>
                    </ul>
                </li>
                <li><span>Finding a Dog<div class="arrow-closed"></div></span></li>
            </ul>

        </li>
        <li><span>Cats<div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>Cat Links<div class="arrow-closed"></div></span></li>
                <li><span>Cat Links<div class="arrow-closed"></div></span></li>
                <li><span>Cat Links<div class="arrow-closed"></div></span></li>
                <li><span>Cat Links<div class="arrow-closed"></div></span></li>
                <li><span>Cat Links<div class="arrow-closed"></div></span></li>
                <li><span>Cat Links<div class="arrow-closed"></div></span></li>
            </ul>
        </li>
        <li><span>Birds<div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>Bird Links<div class="arrow-closed"></div></span></li>
                <li><span>Bird Links<div class="arrow-closed"></div></span></li>
                <li><span>Bird Links<div class="arrow-closed"></div></span></li>
                <li><span>Bird Links<div class="arrow-closed"></div></span></li>
                <li><span>Bird Links<div class="arrow-closed"></div></span></li>
                <li><span>Bird Links<div class="arrow-closed"></div></span></li>
            </ul>
        </li>
        <li><span>Small Mammals<div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>Sm.Mammal Links<div class="arrow-closed"></div></span></li>
                <li><span>Sm.Mammal Links<div class="arrow-closed"></div></span></li>
                <li><span>Sm.Mammal Links<div class="arrow-closed"></div></span></li>
                <li><span>Sm.Mammal Links<div class="arrow-closed"></div></span></li>
                <li><span>Sm.Mammal Links<div class="arrow-closed"></div></span></li>
                <li><span>Sm.Mammal Links<div class="arrow-closed"></div></span></li>
            </ul>
        </li>
        <li><span>Articles<div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>Article Links<div class="arrow-closed"></div></span></li>
                <li><span>Article Links<div class="arrow-closed"></div></span></li>
                <li><span>Article Links<div class="arrow-closed"></div></span></li>
                <li><span>Article Links<div class="arrow-closed"></div></span></li>
                <li><span>Article Links<div class="arrow-closed"></div></span></li>
                <li><span>Article Links<div class="arrow-closed"></div></span></li>
            </ul>
        </li>
        <li><span>Videos<div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>Video Links<div class="arrow-closed"></div></span></li>
                <li><span>Video Links<div class="arrow-closed"></div></span></li>
                <li><span>Video Links<div class="arrow-closed"></div></span></li>
                <li><span>Video Links<div class="arrow-closed"></div></span></li>
                <li><span>Video Links<div class="arrow-closed"></div></span></li>
                <li><span>Video Links<div class="arrow-closed"></div></span></li>
            </ul>
        </li>
        <li><span>Updates<div class="arrow-closed"></div></span>
            <ul class="sub-menu">
                <li><span>More Links<div class="arrow-closed"></div></span></li>
                <li><span>More Links<div class="arrow-closed"></div></span></li>
                <li><span>More Links<div class="arrow-closed"></div></span></li>
                <li><span>More Links<div class="arrow-closed"></div></span></li>
                <li><span>More Links<div class="arrow-closed"></div></span></li>
                <li><span>More Links<div class="arrow-closed"></div></span></li>
            </ul>
        </li>
    </ul>
</div>

我用红色概述了跨度。这仅用于可视化目的,因此我(我们)可以看到跨度的位置。当跨度实际覆盖整个区域时,它将被移除。我还隐藏了箭头的CSS,因为它导致菜单行为时髦。

2 个答案:

答案 0 :(得分:1)

很简单:

.nav-tabs li {
    float: left;
    /* padding: 10px 20px; */ /* remove */
    cursor: pointer;
    text-align: center;
    border-right: 1px solid lightgrey;
}

.nav-tabs li span {
    display:block; /* add */
    padding: 10px 20px; /* add */
    border: 1px solid red;
}

您需要删除li上的填充内容,因为它不允许孩子span完全展开li。而是将填充应用于span并添加display:block;,因为span默认情况下是内联元素,并且只会扩展到足以包含其内容。

演示 http://jsfiddle.net/m65b10ph/1/

另外,要正确定位第4级菜单,由于其中有浮动范围,因此需要clear修复。这是因为包含li的计算高度为0:

.drop-menu li:after {
    display: table;
    clear: both;
    content: "";
}

演示 http://jsfiddle.net/mh7waw46/1/

答案 1 :(得分:1)

<span>display: inline;元素,默认情况下仅占用内容的区域。 display: block;元素占用可用宽度的100%。您可以将<span>设置为display: block;或使用<div>,但这样您还需要将<li>元素的填充设置为0,以便内部元素( <span>)将获得<li>元素的全宽。然后,您可以将填充设置为<span>