How to make a HTML list appear horizontally instead of vertically using CSS only?

自作多情 提交于 2019-12-17 22:33:03

问题


I need this because I want to make a menu (which is made from a HTML list) appear horizontally.

I prefer not to use absolute positioning since it might become messy when I start changing the layout of the page.

I would like also to remove the indenting of the sub-lists. Is it possible?


回答1:


You will have to use something like below

#menu ul{
  list-style: none;
}
#menu li{
  display: inline;
}
	
<div id="menu">
  <ul>
    <li>First menu item</li>
    <li>Second menu item</li>
    <li>Third menu item</li>
  </ul>
</div>



回答2:


quite simple:

ul.yourlist li { float:left; }

or

ul.yourlist li { display:inline; }



回答3:


Using display: inline-flex

#menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: inline-flex
}
<div id="menu">
  <ul>
    <li>1 menu item</li>
    <li>2 menu item</li>
    <li>3 menu item</li>
  </ul>
</div>

Using display: inline-block

#menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

#menu li {
  display: inline-block;
}
<div id="menu">
  <ul>
    <li>1 menu item</li>
    <li>2 menu item</li>
    <li>3 menu item</li>
  </ul>
</div>


来源:https://stackoverflow.com/questions/2145181/how-to-make-a-html-list-appear-horizontally-instead-of-vertically-using-css-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!