In this tutorial, we are going to learn about using HTML lists to create navigation menus.
HTML lists can be used to create navigation menus in a website. This is done by styling the list items to look like buttons or links, and placing them horizontally or vertically as per the design.
HTML lists are created using a pair of <ul>
(unordered list) or <ol>
(ordered list) tags, with individual list items represented by <li>
tags.
To make an HTML list function as a navigation menu, we use CSS to style it. We can change the list's layout from vertical to horizontal, change the appearance of the list items, and add hover effects.
<ul>
for navigation menus, as the order of menu items doesn't usually matter.Here's how to create a basic HTML list:
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
This will create a vertical list of items.
Now let's create a horizontal navigation menu and style it with CSS:
<!-- HTML -->
<ul class="nav-menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<!-- CSS -->
<style>
.nav-menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.nav-menu li {
float: left;
}
.nav-menu li a {
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
</style>
Here, we've used CSS to remove the list bullets (list-style-type: none
), float the <li>
elements to the left to make them horizontal, and style the <a>
elements to look like buttons.
Create a vertical navigation menu with the following items: Home, Services, Products, and Contact. Style it with CSS.
Create a horizontal navigation menu with the same items as in Exercise 1. Add hover effects to the menu items.
<!-- HTML -->
<ul class="nav-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Contact</a></li>
</ul>
<!-- CSS -->
<style>
.nav-menu {
list-style-type: none;
margin: 0;
padding: 0;
}
.nav-menu li a {
display: block;
color: black;
text-decoration: none;
padding: 10px;
}
</style>
<!-- HTML -->
<ul class="nav-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Contact</a></li>
</ul>
<!-- CSS -->
<style>
.nav-menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.nav-menu li {
float: left;
}
.nav-menu li a {
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.nav-menu li a:hover {
background-color: #ddd;
}
</style>
In the solution to Exercise 2, we added a hover effect to the menu items by changing their background color when the mouse hovers over them.