开发者

HTML, CSS - <li> elements do not float

I am trying to get all of the <li> elements in this div:

<div id="nav_bar">
    <ul>
        <li><a href="#1">Nav Item</a></li>
        <li><a href="#2">Nav Item</a></li>
        <li><a href="#3">Nav Item</a></li>
        <li><a href="#4">Nav Item</a></li>
        <li><a href="#5">Nav Item</a></li>
        <li><a href="#6">Nav Item</a开发者_运维技巧></li>
    </ul>
</div>

To be all along one line.

I have the following CSS styles for those elements:

div#nav_bar
{
    width: 25px;
    background-color: #CCC;
}
    div#nav_bar li
    {
        list-style: none;

        padding: 10px;
        height: 20px;
        margin-right: 0px;
        float: left;

        width: 100px;
        background-color: #CCC;
    }

Yet they are underneath each other.


That is because you are setting the width of the div#nav_bar to 25px and hence the li's are floating but overflowing to next line because of the tiny width.

Either set the width to a larger value or remove that property.

Check this: http://jsfiddle.net/d4S2u/


That's a horizontal list and there are many examples.

The simplest would be to use display: inline instead of float: left in your posted code


that cause you are cramping 6

  • tags in a width of 25px, since oveflow is not specified, they get to another line. remove width of div#nav_bar.

  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜