p61 Navigation
p61 Navigation
ITC 122
HTML NAVIGATION
Navigation menus are essential for improving the usability and user experience of a
website. They allow users to navigate through different sections, pages, or categories of
content. HTML provides a simple yet effective way to create navigation menus using
lists and anchor tags. By structuring your navigation menus with HTML, you can create
intuitive and accessible navigation systems for your web pages.
CREATING NAVIGATION MENUS WITH LISTS AND ANCHOR TAGS
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
In the above example, the <nav> element represents the navigation section. Inside the <nav> element, we use an unordered
list <ul> to create the menu items. Each menu item is represented by a list item <li>, and the text of the menu item is
wrapped within an anchor tag <a>. The href attribute of the anchor tag specifies the target location or section.
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>Note: We use href="#" for test links. In a real web site this would be URLs.</p>
</body>
</html>
CSS NAVIGATION BAR
<!DOCTYPE html> <p>In this example, we remove the bullets from the
<html> list, and its default padding and margin.</p>
<head>
<style> <ul>
ul { <li><a href="#home">Home</a></li>
list-style-type: none; <li><a href="#news">News</a></li>
margin: 0; <li><a href="#contact">Contact</a></li>
padding: 0; <li><a href="#about">About</a></li>
} </ul>
</style>
</head> </body>
<body> </html>
…
display: block; - Displaying the links as block elements makes the whole
link area clickable (not just the text), and it allows us to specify the width
(and padding, margin, height, etc. if you want)
width: 60px; - Block elements take up the full width available by default.
We want to specify a 60 pixels width
You can also set the width of <ul>, and remove the width of <a>, as they
will take up the full width available when displayed as block elements. This
will produce the same result as our previous example:
<!DOCTYPE html>
<html> <body>
<head>
<style> <ul>
ul { <li><a href="#home">Home</a></li>
list-style-type: none; <li><a href="#news">News</a></li>
margin: 0; <li><a href="#contact">Contact</a></li>
padding: 0; <li><a href="#about">About</a></li>
width: 60px; </ul>
}
<p>A background color is added to the links to show
li a { the link area.</p>
display: block; <p>Notice that the whole link area is clickable, not
background-color: #dddddd; just the text.</p>
}
</style> </body>
</head> </html>
CREATE A BASIC VERTICAL NAVIGATION BAR WITH A GRAY BACKGROUND
COLOR AND CHANGE THE BACKGROUND COLOR OF THE LINKS WHEN
THE USER MOVES THE MOUSE OVER THEM:
<!DOCTYPE html>
/* Change the link color on hover */
<html>
li a:hover {
<head>
background-color: #555;
<style>
color: white;
ul {
}
list-style-type: none;
</style>
margin: 0;
</head>
padding: 0;
<body>
width: 200px;
<h2>Vertical Navigation Bar</h2>
background-color: #f1f1f1;
<ul>
}
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
li a {
<li><a href="#contact">Contact</a></li>
display: block;
<li><a href="#about">About</a></li>
color: #000;
</ul>
padding: 8px 16px;
</body>
text-decoration: none;
</html>
}
ACTIVE/CURRENT NAVIGATION LINK
ADD AN "ACTIVE" CLASS TO THE CURRENT LINK TO LET THE USER
KNOW WHICH PAGE HE/SHE IS ON:
<!DOCTYPE html>
li a:hover:not(.active) {
<html>
background-color: #555;
<head>
color: white;
<style>
}
ul {
</style>
list-style-type: none;
</head>
margin: 0;
<body>
padding: 0;
width: 200px;
<h2>Vertical Navigation Bar</h2>
background-color: #f1f1f1;
<p>In this example, we create an "active" class with a
}
green background color and a white text. The class is
added to the "Home" link.</p>
li a {
display: block;
<ul>
color: #000;
<li><a class="active" href="#home">Home</a></li>
padding: 8px 16px;
<li><a href="#news">News</a></li>
text-decoration: none;
<li><a href="#contact">Contact</a></li>
}
<li><a href="#about">About</a></li>
</ul>
li a.active {
background-color: #04AA6D;
</body>
color: white;
</html>
}