Home >Web Front-end >CSS Tutorial >Why Aren\'t My Horizontal List Items Displaying Correctly?

Why Aren\'t My Horizontal List Items Displaying Correctly?

Susan Sarandon
Susan SarandonOriginal
2024-11-22 21:49:33462browse

Why Aren't My Horizontal List Items Displaying Correctly?

Horizontal List Items: Troubleshooting and Solutions

In an attempt to create a horizontal list for a website, you've encountered issues despite applying commonly suggested solutions like setting 'float' to left. To diagnose and resolve this problem, let's examine the provided HTML and CSS structure.

ul#menuItems {
  background: none;
  height: 50px;
  width: 100px;
  margin: 0;
  padding: 0;
}
ul#menuItems li {
  display: inline;
  list-style: none;
  margin-left: auto;
  margin-right: auto;
  top: 0px;
  height: 50px;
}
ul#menuItems li a {
  font-family: Arial, Helvetica, sans-serif;
  text-decoration: none;
  font-weight: bolder;
  color: #000;
  height: 50px;
  width: auto;
  display: block;
  text-align: center;
  line-height: 50px;
}
<ul>

Analysis

The provided code structure is almost correct. However, you've set the 'display' property of 'li' elements to 'inline' instead of 'inline-block.' This prevents the elements from flowing horizontally.

Solution

To create a horizontal list, modify the 'display' property of 'li' elements to 'inline-block' in your CSS:

ul#menuItems li {
  display: inline-block;
}

By updating your CSS as suggested, your list items will now display horizontally, as intended.

The above is the detailed content of Why Aren\'t My Horizontal List Items Displaying Correctly?. 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