如何仅使用 CSS 使 HTML 列表水平显示而不是垂直显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2145181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:59:52  来源:igfitidea点击:

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

htmlcssmenupositioninghorizontallist

提问by M. A. Kishawy

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

我需要这个是因为我想让菜单(由 HTML 列表制成)水平显示。

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?

我还想删除子列表的缩进。是否可以?

回答by Ravia

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>

回答by Tushar

Using display: inline-flex

使用 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

使用 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>

回答by roman

quite simple:

非常简单:

ul.yourlist li { float:left; }

or

或者

ul.yourlist li { display:inline; }