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>‣ <a href="" class="a_top_hypers"> Inbox</a></li>
<li>‣ <a href="" class="a_top_hypers"> Compose</a></li>
<li>‣ <a href="" class="a_top_hypers"> Reports</a></li>
<li>‣ <a href="" class="a_top_hypers"> Preferences</a></li>
<li>‣ <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>‣ <a href="" class="a_top_hypers"> Inbox</a></li>
<li>‣ <a href="" class="a_top_hypers"> Compose</a></li>
<li>‣ <a href="" class="a_top_hypers"> Reports</a></li>
<li>‣ <a href="" class="a_top_hypers"> Preferences</a></li>
<li>‣ <a href="" class="a_top_hypers"> logout</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 15
您也可以将它们设置为向右浮动.
#ul_top_hypers li {
float: right;
}
Run Code Online (Sandbox Code Playgroud)
这允许它们仍然是块级别,但将出现在同一行上.
正如@alex所说,你可以正确地漂浮它,但是如果你想保持标记相同,那就把它漂浮到左边!
#ul_top_hypers li {
float: left;
}
Run Code Online (Sandbox Code Playgroud)
正如其他人所说,你可以设置li
到display:inline;
,或float
在li
左或右。此外,您还可以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>‣ <a href="" class="a_top_hypers"> Inbox</a></li>
<li>‣ <a href="" class="a_top_hypers"> Compose</a></li>
<li>‣ <a href="" class="a_top_hypers"> Reports</a></li>
<li>‣ <a href="" class="a_top_hypers"> Preferences</a></li>
<li>‣ <a href="" class="a_top_hypers"> logout</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)