Tailwind CSS tutorial #3: Responsive Navbar
Tags: tailwindcss, css, codenewbie
Published: 2022-10-26 17:26:59

Hey everyone, in many apps you need a navbar which slides in if you click on a hamburger icon and which is also responsive. In this tutorial, we are going to see how to build that.


## What we are building:
We will be building a simple navbar with HTML and CSS. We will use the below design as the guideline for our component.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fy0in3kalfcypf5mbhk3.png)


## Requirements
-Prior knowledge of HTML and CSS
-[HTML template for navbar](https://github.com/codewithshubhi/learn-tailwind-css/blob/main/navindex.html)
-Code editor of your choice.

## Implementation
**Step1**
Get the [html code template](https://github.com/codewithshubhi/learn-tailwind-css/blob/main/navindex.html). Install tailwind CSS however, you will need to add the tailwind CSS you have installed into our project and you can do by using the “link” tag to link the tailwind CSS file to the HTML template.

`npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
`
**Code:**
```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Navigation</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body></body>
</html>

```
Checkout detailed setup of tailwind [here](https://dev.to/shubhicodes/get-started-with-tailwind-css-2g98)

since we know it's a navigation component we will use the `nav` element as our wrapping container. Let's also add a class of `navbar`.

```html
<nav class="navbar"></nav>
```
**Step 2**
Next, I would review the content inside the navigation.
I would split the content into two parts:

1. The brand content/body
2. The navigational items

The reason I would split this up is that the brand content isn't necessarily a navigation item and I want the HTML to have the correct meaning.

**Step 3**
Next, let's implement option one from Step 2, The `brand` content. Since we know from the requirements it needs to be a link I would use the anchor tag. I will also add a class name of the `brand` so we can style it later.

**Our code should now look like this:**

```html
<nav class="navbar">
  <a class="brand" href="#">My Portfolio</a>
</nav>

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wibkuo5anjt15hwadxtx.png)

**Step 4**

Next, let's implement option two from Step 2, the navigational items. I would classify these as a list of links. Keeping with ensuring our HTML structure is semantic I would use a list to structure the items.

Our code should now look like this:

```html
<nav class="navbar">
  <a class="brand" href="#">My portfolio</a>
  <ul>
    <li>
      <a href="#">Home</a>
    </li>
    <li>
      <a href="#">Blog </a>
    </li>
    <li>
      <a href="#">About Me</a>
    </li>
    <li>
      <a href="#">Contact</a>
    </li>
  </ul>
</nav>

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2pvqbt6med8tuw5g6zu.png)

**Step 5**
At this stage, we will add the necessary menu items to help users navigate around the website. Below is the code for the menu

**Code**

```html
<!-- Nav menu -->
<div
  class="w-full lg:w-auto block lg:flex lg:items-center lg:inline-block hidden"
  id="navbar"
  >
  <div class="lg:flex-grow text-2xl text-center space-x-3">
    <a href="#" class="block lg:inline-block hover:text-green-400 mt-4">
    Home
    </a>
    <a href="#" class="block lg:inline-block hover:text-green-400 mt-4">
    Blog
    </a>
    <a
      href="#"
      class="block lg:inline-block hover:text-green-400 mt-4 mb-3z"
      >
    About Me
    </a>
    <a
      href="#"
      class="lg:inline-block hover:text-green-400 mt-6 border rounded border-white hover:border-transparent hover:bg-white px-4"
      >
    Contact
    </a>
  </div>
</div>
</nav>

```
We added some classes to the menu to help it fit into our navigation bar.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pa4h97mwnviyhav92puk.png)

In the code above, we used SVG to create a hamburger for the small screen and gave it a height and weight of 6. We also changed the color to gray. Other classes we included are
Flex is used to set the hamburger menu along with the items in the Tailwind CSS navbar.

**Step 6**
We added some classes to the menu to help it fit into our navigation bar.
Add the mobile button
Next, we are going to create the menu for small screens. Then We can go ahead to use JavaScript to toggle the menu we created.
The JavaScript code will be written using the `<script>` tag.
We can go ahead and grab the HTML tag we use to apply this functionality.

**Code**

```html
<script>
  var navbar = document.getElementById("navbar");
  
  const toggle_nav = () => {
    navbar.classList.toggle("hidden");
  };
  
  // Close menu is window size increases
  window.onresize = () => {
    let viewportWidth = window.innerWidth;
    if (viewportWidth > 1050) {
      navbar.classList.add("hidden");
    }
  };
</script>

```

## Overview

**The overall code will be like the code below**:
[Repo link](https://github.com/codewithshubhi/learn-tailwind-css/blob/main/navbar.html)

## Conclusion

With some basic knowledge of HTML and CSS, we were able to create a fully functioning navbar component.

If you liked this article, consider following me on [Dev.to](https://dev.to/shubhicodes) for my latest publications. You can reach me on [Twitter](https://twitter.com/heyShubhi).

Keep learning! Keep coding!! 💛