Home  >  Article  >  Web Front-end  >  How to arrange ul horizontally in html

How to arrange ul horizontally in html

下次还敢
下次还敢Original
2024-04-27 19:58:09735browse

How to make UL in HTML align horizontally: Make the list items float to the left by adding float: left style. Set the width for each list item to control its horizontal arrangement. Use the margin property to eliminate gaps between list items.

How to arrange ul horizontally in html

How to make the UL in HTML arrange horizontally

In HTML, by default, the unordered list ( UL) arranges its items vertically. However, horizontal alignment can be achieved by using CSS styles.

Steps:

  1. Apply float:

    Add CSS stylefloat: left to float the list items to the left.

<code class="css">ul {
  list-style-type: none; /* 删除默认圆点 */
  padding: 0; /* 删除默认间距 */
}

li {
  float: left; /* 使列表项浮动到左侧 */
}</code>
  1. Set the width:

    Set the width for each list item to control their horizontal arrangement.

<code class="css">li {
  width: 150px; /* 设置列表项的宽度 */
}</code>
  1. Eliminate gaps:

    Gaps may appear between list items. Use the margin property to eliminate these gaps.

<code class="css">li {
  margin-right: -1px; /* 消除水平间隙 */
}</code>

Example:

<code class="html"><ul>
  <li>项目 1</li>
  <li>项目 2</li>
  <li>项目 3</li>
</ul></code>
<code class="css">ul {
  list-style-type: none;
  padding: 0;
}

li {
  float: left;
  width: 150px;
  margin-right: -1px;
}</code>

After applying these styles, your unordered list will be arranged horizontally, with list items width 150 pixels, and There are no gaps.

The above is the detailed content of How to arrange ul horizontally in html. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn