如何将菜单子项显示为块

时间:2018-05-09 12:57:55

标签: css menu horizontallist

我有一个菜单,可以在垂直列表中显示子项。我想水平显示它们,但看不到怎么做。这是我的jsfiddle。所以不要显示为

First sub-item
Second sub-item

应该是

First sub-item  Second sub-item

列表的宽度应该是可用的任何内容,以便项目在手机和桌面中正确显示。这是我正在使用的代码。有人请解释一下如何做到这一点吗?

    <style>
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }

    ul li {
        display: inline-block;
        width: 10em;
        height: 2em;
        line-height: 2em;
        position: relative;
        border-bottom: 2px solid #ccc;
    }

    ul li:hover {
        background-color: #ffa;
    }

    ul li ul {
        display: none;
        position: absolute;
        top: 2em;
        left: 0;
    }

    ul li:hover ul {
        display: block;
    }

    ul li ul li {
        display: list-item;
        position: relative;
    }

    ul li ul li ul {
        display: none;
        position: absolute;
        top: 0;
        left: 10em;
    }

    ul li ul li ul li {
        display: none;
    }

    ul li ul li:hover ul {
        display: block;
    }

    ul li ul li:hover ul li {
        display: list-item;
    }
    </style>

    <ul>
        <li>First list item</li>
        <li>Second list item</li>
        <li>Third, with a dropdown
            <ul>
                <li>First sub-item</li>
                <li>Second sub-item</li>
                <li>Third, with a fly-out
                    <ul>
                        <li>Flyout one</li>
                        <li>Flyout two</li>
                        <li>Flyout three</li>
                    </ul>
                </li>
                <li>Fourth sub-item</li>
            </ul>
        </li>
        <li>Fourth list item</li>
    </ul>

1 个答案:

答案 0 :(得分:1)

要横向显示它们,您只需在悬停时添加display:flex而不是display:block。希望这对你有所帮助。

&#13;
&#13;
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

ul li {
    display: inline-block;
    width: 10em;
    height: 2em;
    line-height: 2em;
    position: relative;
    border-bottom: 2px solid #ccc;
}

ul li:hover {
    background-color: #ffa;
}

ul li ul {
    display: none;
    position: absolute;
    top: 2em;
    left: 0;
}

ul li:hover ul {
    display: flex;
}

ul li ul li {
    display: list-item;
    position: relative;
}

ul li ul li ul {
    display: none;
    position: absolute;
    top: 2em;
    left: 0em;
}

ul li ul li ul li {
    display: none;
}

ul li ul li:hover ul {
    display: flex;
}

ul li ul li:hover ul li {
    display: list-item;
}
&#13;
<ul>
    <li>Main Menu
        <ul>
            <li>First sub-item</li>
            <li>Second sub-item</li>
            <li>Third sub-item</li>
            <li>Fourth sub-item
                <ul>
                    <li>first flyout</li>
                    <li>second flyout</li>
                    <li>third flyout</li>
                </ul>
            </li>
        </ul>
    </li> 
</ul>
&#13;
&#13;
&#13;