CSS NAVIGATION BAR
CSS NAVIGATION BAR
Having easy-to-use navigation is important for any web site. With CSS you can transform boring HTML menus into good-looking navigation bars.
Navigation Bar = List of Links
A navigation bar needs standard HTML as a base. A navigation bar is basically a list of links, so using the <ul> and <li> elements is a necessity:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
</style>
</head>
<body>
<header>
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
</header>
<p>Now let's remove the bullets and the margins and padding from the list:</p>
</body>
</html>
list-style-type: none; - Removes the bullets. A navigation bar does not need list markers
Set margin: 0; and padding: 0; to remove browser default settings The code in the example above is the standard code used in both vertical, and horizontal navigation bars,
CSS Vertical Navigation Bar
To build a vertical navigation bar, you can style the elements inside the list.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
li a {
display: block;
width: 60px;
}
</style>
</head>
<body>
<header>
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
</header>
<p>Now let's remove the bullets and the margins and padding from the list:</p>
</body>
</html>
Example explained:
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.
Vertical Navigation Bar With added styles
We are Creating a basic vertical navigation bar with a blue background color and with a hover effect that changes the background color of the links when the user moves the mouse over them:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
</style>
</head>
<body>
<header>
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
</header>
</body>
</html>
Added Effects
Active/Current Navigation Link
Add an "active" class to the current link to let the user know which page he/she is on:
Center Links & Add Borders
Add text-align:center to <li> or <a> to center the links.
Borders
Add the border property to <li> inside the navbar, and remove the border from the last child <li>.
<!DOCTYPE html>
<html>
<head>
<style>
.active {
background-color: #4CAF50;
color: white;
}
ul {
list-style:none;
}
li {
text-align: center;
border-bottom: 1px solid #555;
background-color: #7897f4;
}
li:hover{
background:#0c2676;
}
li:last-child {
border-bottom: none;
}
li a{
color:white;
text-decoration:none;
}
</style>
</head>
<body>
<header>
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
</header>
</body>
</html>
Full-height Fixed Vertical Navbar
Create a full-height, "Fixed" side navigation:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
height: 100%; /* Full height */
position: fixed; /* Make it stick, even on scroll */
overflow: auto; /* Enable scrolling if the sidenav has too much content */
}
</style>
</head>
<body>
<header>
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
</header>
</body>
</html>
Note: This example might not work properly on mobile devices.