How to create: Top navigation bar

Learn how to use CSS to create a top navigation bar.

Top navigation bar

Try it yourself

Create a top navigation bar

First step - Add HTML:

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

Second step - Add CSS:

/* Add black background color for top navigation */
.topnav {
  background-color: #333;
  overflow: hidden;
}
/* Set styles for links in navigation bar */
.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}
/* Change link color on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}
/* Add color for active/current link */
.topnav a.active {
  background-color: #04AA6D;
  color: white;
}

Try it yourself

Related Pages

Tutorial:How to Create Responsive Top Navigation

Tutorial:CSS Navigation Bar