tutorial tutorials

Create a Responsive Navbar using Tailwind CSS

In this tutorial, we will see how we can create a simple responsive Navbar or Header using only Tailwind CSS.

For this tutorial, we are going to set up a simple HTML project in which we will use Tailwind CSS.

We can use Tailwind CSS in multiple ways follow these tutorials to learn more about it.

but in this tutorial, we are going to use Tailwind CSS play CDN which is <script src="https://cdn.tailwindcss.com"></script> put this script tag inside <head> </head> tag as shown below.

 <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" />
 
     <script src="https://cdn.tailwindcss.com"></script>
 
     <title>Responsive Navbar with Tailwind CSS</title>
   </head>

now we are ready to use Tailwind CSS in our HTML project.

and below is the source code for Responsive Navbar using Tailwind CSS.

<!-- 
  Project: Responsive Navbar with Tailwind CSS
  Author: Tutorend.com
  Date: Feb 2022
 -->

 <!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" />
 
     <script src="https://cdn.tailwindcss.com"></script>
 
     <title>Responsive Navbar with Tailwind CSS</title>
   </head>
 
   <body class="bg-slate-900">
     <nav
       class="bg-slate-700 flex text-white items-center justify-between flex-wrap pb-2 px-5"
     >
       <!-- Logo -->
       <div>
         <span class="font-semibold text-4xl text-green-400">Tutorend</span>
       </div>
       <!-- Toggle button -->
       <div class="block lg:hidden">
         <button
           onclick="toggle_nav()"
           class="flex flex-col px-3 py-2 border rounded border-gray-800 hover:text-white hover:border-white"
         >
           <div class="w-6 h-1 bg-white"></div>
           <div class="w-6 h-1 bg-white my-1"></div>
           <div class="w-6 h-1 bg-white"></div>
         </button>
       </div>
 
       <!-- 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 Us
           </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"
           >
             Login / Sign Up
           </a>
         </div>
       </div>
     </nav>
 
     <a href="https://www.youtube.com/watch?v=_L09qRbV1kU">
       <h1 class="text-center m-auto my-10 text-white text-6xl">Watch Video</h1>
     </a>
 
     <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>
   </body>
 </html>
 

that’s it for this tutorial. Happy Coding.

Related Posts

How To Use react-icons in React Js or Next Js app

Using Icons on a website makes it look so beautiful and easy to navigate, and if we are building one with React Js or any React-based frameworks like…

How To Upgrade Existing Projects To Next Js 13

Upgrade your existing next js projects to next js 13, with simple steps recommended by Vercel like comdemod

how to install and use bootstrap 5 in reat js tutorend

How to use Bootstrap 5 in React js

When it comes to styling a website as quickly as possible, there is no good alternative to Bootstrap, Bootstrap gives us ready-made components to use inside our website…

particle js in react

How to use Particles Js in React with react-tsparticles

Particle.js is a great JavaScript library for creating 2d as well as 3d looking particles on your website. But using Particle.js is not an easy task so there…

tailwind sidebar preview

Tailwind Sidebar Navigation Menu

Source Code Tailwind CSS is a great UI framework for designing and building websites faster, we can quickly implement different components in a short time using Tailwind CSS….

animated hamburger preview

How to create an Animated Toggle Button with CSS

In this tutorial we are going to create an animated Hamburger Navigation Toggle Button with HTML, CSS and little bit of JS Source Code on GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *