In this tutorial, we're going to build a simple navigation bar using HTML and CSS. This navigation bar will guide users to different sections of a website, improving the overall user experience.
What you'll learn:
- Basic HTML and CSS syntax
- How to structure a simple navigation bar
- Styling the navigation bar with CSS
Prerequisites:
- Basic understanding of HTML and CSS
Firstly, let's discuss the HTML structure of a navigation bar. For this tutorial, we'll use the <nav>
element, which is a semantic HTML5 element specifically designed for navigation. Inside the <nav>
, we'll have an unordered list (<ul>
) with several list items (<li>
), each representing a different section of the site.
Once the HTML structure is complete, we'll move onto CSS to style our navigation bar. We'll add colors, change the font, and add hover effects.
Example 1: Basic HTML structure
<!-- This is the basic HTML structure of our navigation bar -->
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
In the above code:
- The <nav>
element is a container for our navigation bar
- The <ul>
element contains the list of navigation items
- Each <li>
element represents a different section of the site, with a hyperlink (<a>
) leading to the corresponding section
Example 2: Basic CSS styling
/* This is the basic CSS for our navigation bar */
nav {
background-color: #333;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 20px;
}
nav ul li a {
color: #fff;
text-decoration: none;
}
In the above code:
- We set a background color to the <nav>
element
- We remove the default bullet points from the <ul>
with list-style-type: none
- We display the <li>
elements in a row with display: inline
- We set the color of the links to white and remove the underline with text-decoration: none
We've covered the basic HTML structure and CSS styling for a simple navigation bar. The key points are:
<nav>
, <ul>
, <li>
, and <a>
elements are used to structure a navigation barNext steps for learning could include:
- Learning how to add dropdown menus to the navigation bar
- Learning how to make the navigation bar responsive for different screen sizes
Exercise 1: Create a navigation bar with five sections: Home, About, Services, Portfolio, and Contact.
Exercise 2: Style the navigation bar: change the background color, remove the bullet points, display the items in a row, change the link color to white, and add a hover effect.
Exercise 3: Create a vertical navigation bar: display the items in a column instead of a row.
Solutions and explanations will be provided for these exercises.