如何在水平行中显示<ul>

Bab*_*ker 101 html css html-lists

如何使用CSS将列表项水平连续显示?

#div_top_hypers {
    background-color:#eeeeee;
    display:inline;      
}
#ul_top_hypers {
    display: inline;
}
Run Code Online (Sandbox Code Playgroud)
<div id="div_top_hypers">
    <ul id="ul_top_hypers">
        <li>&#8227; <a href="" class="a_top_hypers"> Inbox</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Compose</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Reports</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Preferences</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> logout</a></li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

hbw*_*hbw 125

列表项通常是块元素.通过display属性将它们变成内联元素.

在您提供的代码中,您需要使用上下文选择器将display: inline属性应用于列表项,而不是列表本身(应用于display: inline整个列表将不起作用):

#ul_top_hypers li {
    display: inline;
}
Run Code Online (Sandbox Code Playgroud)

这是工作示例:

#div_top_hypers {
    background-color:#eeeeee;
    display:inline;      
}
#ul_top_hypers li{
    display: inline;
}
Run Code Online (Sandbox Code Playgroud)
<div id="div_top_hypers">
    <ul id="ul_top_hypers">
        <li>&#8227; <a href="" class="a_top_hypers"> Inbox</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Compose</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Reports</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Preferences</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> logout</a></li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 我也通过使用float实现了这个效果,从而保持了列表项的块性质. (2认同)
  • 你总是可以使用display:inline-block如果你想保留块性质......虽然它在这个阶段并不完全支持(我相信它是Fx2的主要罪魁祸首). (2认同)

ale*_*lex 15

您也可以将它们设置为向右浮动.

#ul_top_hypers li {
    float: right;
}
Run Code Online (Sandbox Code Playgroud)

这允许它们仍然是块级别,但将出现在同一行上.

  • 将它们向右浮动会产生额外的效果:它将交换它们的顺序,因此从左到右它们将是最后一个到第一个。 (3认同)

Pau*_*rie 11

display属性设置inline为您要应用于的列表.在A List Apart上显示列表有一个很好的解释.


tjh*_*ner 7

正如@alex所说,你可以正确漂浮它,但是如果你想保持标记相同,那就把它漂浮到左边!

#ul_top_hypers li {
    float: left;
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*son 6

正如其他人所说,你可以设置lidisplay:inline;,或floatli左或右。此外,您还可以display:flex;ul. 在下面的代码段中,我还添加justify-content:space-around了更多间距。

有关 flexbox 的更多信息,请查看此完整指南

#div_top_hypers {
    background-color:#eeeeee;
    display:inline;      
}
#ul_top_hypers {
    display: flex;
    justify-content:space-around;
    list-style-type:none;
}
Run Code Online (Sandbox Code Playgroud)
<div id="div_top_hypers">
    <ul id="ul_top_hypers">
        <li>&#8227; <a href="" class="a_top_hypers"> Inbox</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Compose</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Reports</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Preferences</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> logout</a></li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 5

它将为您工作:

#ul_top_hypers li {
    display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

#ul_top_hypers li {
  display: flex;
}
Run Code Online (Sandbox Code Playgroud)