Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a navigation bar in HTML

The Core Concept: What is a Navigation Bar?

Let's picture a navigation bar (navbar) as a road map of a website. It's that part of the website that guides visitors to different sections, much like a map would guide you to various locations in a city. In HTML, the navbars are created using a combination of HTML and CSS. HTML defines the structure while CSS styles it.

The Building Blocks: HTML and CSS

HTML (HyperText Markup Language) is the standard markup language for creating web pages. CSS (Cascading Style Sheets) is a style sheet language used for describing the look and formatting of a document written in HTML. Think of HTML as the skeleton of the web page, and CSS as the skin and clothes that make it look appealing.

The Blueprint: Structure of a Navbar

The basic structure of a navbar consists of a container (<nav>) and list items (<li>). The <nav> element represents a section of a page whose purpose is to provide navigation links. Inside this <nav> container, we usually place an unordered list (<ul>) that holds each navigation link (<li>). The <a> tag defines the actual link.

Here's what this structure looks like:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

Getting Our Hands Dirty: Coding the Navbar

Let's create a basic navbar in HTML. We'll start by setting up the structure as shown above. In the <a> tags, replace the # with the actual links to your pages.

<nav>
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="about.html">About</a></li>
    <li><a href="services.html">Services</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>

At this point, our navbar is functional but not very appealing. This is where CSS comes in.

Adding Style: Dressing Up Our Navbar with CSS

To make our navbar visually appealing and more user-friendly, we'll use CSS. We'll start by removing the default bullet points and styling the background.

nav {
  background-color: #333;
}

nav ul {
  list-style-type: none;
  padding: 0;
}

Next, we'll style the navigation links. We'll change the color, display them inline, and add some padding.

nav ul li {
  display: inline-block;
  margin-right: 20px;
}

nav ul li a {
  color: #fff;
  text-decoration: none;
}

Our navbar is looking much better now. But it could use some interaction. Let's change the color of the links when the mouse hovers over them.

nav ul li a:hover {
  color: #7F00FF;
}

And just like that, we've built and styled a basic navbar!

Making It Responsive: A Navbar that Adapts to Screen Sizes

In this day and age, it's crucial to have a navbar that looks good on all devices, whether it's a desktop, tablet, or mobile phone. This is called a responsive design. To make our navbar responsive, we're going to use a technique called media queries.

A media query is a tool in CSS that allows us to apply styles only when certain conditions are met. For our navbar, we'll apply different styles when the screen size is less than 600px, which is typically the width of a mobile device in portrait mode.

@media screen and (max-width: 600px) {
  nav ul li {
    display: block;
    margin: 10px 0;
  }
}

Now, when the screen size is less than 600px, our navbar links will stack vertically and have some space between them.

And there you have it! We've created a functional, styled, and responsive navbar. You can further customize it to fit your needs and match your website's style. Happy coding!