Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a navigation bar in HTML

Understanding the Basics

Before diving straight into creating a navigation bar, let's understand what it is. A navigation bar, or navbar, is a section of a webpage that contains links to other pages on the same website. It's like the table of contents in a book, helping you navigate to the right section quickly.

Why Do We Need a Navigation Bar?

Imagine you're in a big supermarket with no signs or labels. Finding the bread aisle would be quite a task, wouldn't it? That's why we need a navigation bar in a website. It helps users find the information they need quickly without having to scroll through every page.

Getting Started

Now that we understand what a navigation bar is and why we need it, let's start creating one in HTML. HTML, or Hyper Text Markup Language, is the standard language for creating webpages.

We will be using the <nav> and <a> HTML elements. The <nav> tag is used to define a set of navigation links and the <a> tag defines a hyperlink.

Here's a basic structure of a navigation bar:

  <a href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>

Each href attribute in the <a> tag points to a different section of the website. This is how we create links to different pages on the same website.

Styling the Navigation Bar

A navigation bar without any styling would just look like a list of links. To make it more appealing and user-friendly, we can add some CSS (Cascading Style Sheets) to it. CSS is a style sheet language used for describing the look and formatting of a document written in HTML.

Let's add some basic styles to our navigation bar:

nav {
  background-color: #333;
  overflow: hidden;

nav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;

In the above code, we have set the background color of the navigation bar to black (#333), and the color of the links to white (#f2f2f2). The overflow: hidden; property ensures that the content does not spill out of the navigation bar. We also added some padding to the links to give them some spacing.

Making the Navigation Bar Responsive

In today's world, websites are viewed on devices of all sizes. Therefore, it's important that our navigation bar is responsive, i.e., it adjusts itself according to the screen size.

We can make our navigation bar responsive by using media queries in CSS. A media query is a technique used in CSS to apply styles to different devices based on their characteristics, such as screen size or device-width.

Here is how we can make our navigation bar responsive:

@media screen and (max-width: 600px) {
  nav a {
    float: none;
    width: 100%;

In the above code, we have set the width of the links to 100% and removed the float property when the screen size is 600px or less. This means that the links will stack on top of each other, instead of floating to the left, when viewed on smaller screens.


Creating a navigation bar in HTML is not a complex task. With the use of <nav> and <a> elements, we can easily create links to different sections of the website. And with a bit of CSS, we can style the navigation bar to make it more appealing and user-friendly. Finally, by using media queries, we can ensure that our navigation bar is responsive and works well on all devices.

Remember, a well-designed navigation bar not only enhances the user experience but also improves the overall usability of the website. So, take the time to design your navigation bar properly. Happy coding!