Categories
Web development

How to create a Navigation Bar?

Having easy-to-use navigation is very important for any website. Today you can learn how to create a top navigation bar with CSS. 

How to create a Navigation Bar?

With CSS you can transform boring HTML menus into good-looking navigation bars. The navigation bar can be vertical, or horizontal.

With CSS you can transform boring HTML menus into good-looking navigation bars. The navigation bar can be vertical, or horizontal.
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 makes perfect sense (you can review your knowledge about HTML list tags here.)

Step 1: Add HTML

<!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>

</body>
</html>

Output:

Step 2: Add CSS

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #264653;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #e9c46a;
  color: black;
}

.topnav a.active {
  background-color: #2a9d8f;
  color: white;
}
</style>
</head>
<body>

<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>

<div style="padding-left:16px">
  <h3>Top Navigation Example</h3>
  <p>Some content..</p>
</div>

</body>
</html>

Output:

Top Navigation Example

Some content..


To build a vertical navigation bar, you can style the <a> elements inside the list, in addition to the code from 1st step:

<!DOCTYPE html>
<html>
<head>
<style>
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

li a {
  display: block;
  width: 100px;
  background-color: #e9c46a;
}
</style>
</head>
<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>

</body>
</html>

Output:


You can create a basic vertical navigation bar with a yellow 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: #2a9d8f;
}

li a {
  display: block;
  color: #000;
  padding: 8px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: #e9c46a;
  color: white;
}
</style>
</head>
<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>

</body>
</html>

Output:


Enjoy coding!

Read also:

CSS Sticky social media bar

CSS Hamburger Menu

How to create equal height columns with CSS?