Tailwind CSS Tutorial: Build a Navbar with Dropdown Menu

Faraz

By Faraz -

Learn how to design a stunning responsive Navbar with Dropdown menu using Tailwind CSS. A comprehensive tutorial for web developers and UI designers.


Tailwind CSS Tutorial Build a Navbar with Dropdown Menu.jpg

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. JavaScript Code
  5. Preview
  6. Conclusion

Tailwind CSS is revolutionizing the way web developers approach designing user interfaces. This powerful utility-first CSS framework offers a streamlined and efficient way to create stunning web components. In this comprehensive tutorial, we'll walk you through the process of crafting a Navbar with Dropdown menu using Tailwind CSS.

Navigational menus are a fundamental part of web design, and adding a dropdown functionality enhances user experience and aesthetics. By the end of this guide, you'll have a functional Navbar and the skills to tailor it to your specific project needs.

Whether you're a seasoned developer looking to expand your skill set or a newcomer eager to learn, this tutorial is designed to be beginner-friendly and informative. So, let's dive in and harness the power of Tailwind CSS to create a responsive and visually appealing Navbar with Dropdown menus.

Source Code

Step 1 (HTML Code):

To get started, we will first need to create a basic HTML file. In this file, we will include the main structure for our navbar with dropdown menu.

After creating the files just paste the following codes into your file. Make sure to save your HTML document with a .html extension, so that it can be properly viewed in a web browser.

Here's an explanation of each part:

1. <!DOCTYPE html>: This declaration specifies that the document is an HTML5 document.

2. <html lang="en">: This opening tag defines the root element of the HTML document and specifies that the language of the document is English ("en").

3. <head>: This section contains metadata about the webpage, such as the title, character encoding, and viewport settings.

  • <title>: Sets the title of the webpage, which typically appears in the browser's title bar or tab.
  • <meta charset="UTF-8" />: Declares the character encoding of the document as UTF-8, which supports a wide range of characters and symbols.
  • <meta name="viewport" content="width=device-width" />: Defines the viewport settings, ensuring that the webpage is responsive and adjusts to the width of the device's screen.
  • <script src="https://cdn.tailwindcss.com"></script>: Includes an external JavaScript file from the specified URL. In this case, it's loading the Tailwind CSS framework.

4. <body>: This is the main content section of the HTML document, where the visible content of the webpage is placed.

5. <nav>: Represents the navigation bar of the webpage.

6. <div class="max-w-screen-xl flex flex-wrap items-center justify-between mx-auto p-4">: This div element contains the navigation bar content and uses Tailwind CSS classes for styling.

7. <a href="https://www.codewithfaraz.com" class="flex items-center">: This is a link to the website "https://www.codewithfaraz.com" and includes a logo and text.

8. <img src="https://www.codewithfaraz.com/InstaPic.png" class="h-8 mr-3" alt="codewithfaraz Logo" />: This is an image (logo) displayed with a specific height and margin.

9. <span class="self-center text-2xl font-semibold whitespace-nowrap dark:text-white">CodewithFaraz</span>: This is a text element with specific styling, including font size and color.

10. <button id="navbar-toggle" data-collapse-toggle="navbar-dropdown" type="button" class="inline-flex items-center p-2 w-10 h-10 justify-center text-sm text-gray-500 rounded-lg md:hidden hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600" aria-controls="navbar-dropdown" aria-expanded="false">: This is a button with an ID and various classes. It appears as a mobile menu toggle button with specific styling.

11. <span class="sr-only">Open main menu</span>: This hidden span provides an accessible label for screen readers.

12. <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 17 14">...</svg>: This SVG icon is used as the menu toggle button's visual representation.

13. <div class="hidden w-full md:block md:w-auto" id="navbar-dropdown">: This div is initially hidden but becomes visible on larger screens (md breakpoint). It contains the navigation menu items.

14. <ul class="flex flex-col font-medium p-4 md:p-0 mt-4 border border-gray-100 rounded-lg bg-gray-50 md:flex-row md:space-x-8 md:mt-0 md:border-0 md:bg-white dark:bg-gray-800 md:dark:bg-gray-900 dark:border-gray-700">: This unordered list contains the navigation menu items.

15. <li>: Each list item represents a menu option.

16. <a href="#" class="block py-2 pl-3 pr-4 text-white bg-blue-700 rounded md:bg-transparent md:text-blue-700 md:p-0 md:dark:text-blue-500 dark:bg-blue-600 md:dark:bg-transparent" aria-current="page">Home</a>: This is a link to the "Home" page with specific styling. The aria-current attribute indicates that it's the current page.

17. Similar list items and links exist for "About," "Services," "Product" (with a dropdown menu), and "Contact."

18. The "Product" menu item contains a dropdown menu with items for "Python," "C#," and "JavaScript."

19. <script src="script.js"></script>: Includes an external JavaScript file named "script.js," which is used to add interactivity and functionality to the webpage.

This is the basic structure of our navbar with dropdown menu using HTML, and now we can move on to styling it using CSS.

Step 2 (CSS Code):

No custom CSS thanks to Tailwind!

tailwindcss.com

/*

 No custom CSS thanks to Tailwind!
 tailwindcss.com

*/ 

Step 3 (JavaScript Code):

Finally, we need to create a function in JavaScript.

Let's break down what it does step by step:

1. document.addEventListener('DOMContentLoaded', function () { ... });: This line waits for the HTML document to be fully loaded and ready before executing the enclosed code. It ensures that the JavaScript code doesn't run until the entire webpage is ready to be manipulated.

2. const button = document.getElementById('navbar-toggle');: Here, the code selects an HTML element with the ID attribute 'navbar-toggle' and assigns it to the variable button. This element is a button that will be used to toggle the visibility of the navigation menu.

3. const menu = document.getElementById('navbar-dropdown');: Similarly, this line selects an HTML element with the ID attribute 'navbar-dropdown' and assigns it to the variable menu. This element probably represents the navigation menu that will be shown or hidden.

4. const dropdownButton = document.getElementById('dropdownNavbarLink');: This line selects an HTML element with the ID attribute 'dropdownNavbarLink' and assigns it to the variable dropdownButton. This element is a link used to toggle a dropdown menu.

5. const dropdownMenu = document.getElementById('dropdownNavbar');: Just like the previous lines, this one selects an HTML element with the ID attribute 'dropdownNavbar' and assigns it to the variable dropdownMenu. This element represents the dropdown menu that will be shown or hidden.

6. dropdownButton.addEventListener('click', () => { ... });: This line adds a click event listener to the dropdownButton. When this button is clicked, the function inside the curly braces is executed.

7. dropdownMenu.classList.toggle('hidden');: Within the click event listener, this line toggles the CSS class 'hidden' on the dropdownMenu element. Toggling means that if the element currently has the 'hidden' class, it will be removed, and if it doesn't have the 'hidden' class, it will be added. This action controls the visibility of the dropdown menu.

8. button.addEventListener('click', function () { ... });: Similarly, this line adds a click event listener to the button element, which is typically used to toggle the main navigation menu.

9. menu.classList.toggle('hidden');: Within the click event listener for the main navigation button, this line toggles the CSS class 'hidden' on the menu element, controlling the visibility of the main navigation menu.

Create a JavaScript file with the name of script.js and paste the given codes into your JavaScript file and make sure it's linked properly to your HTML document, so that the scripts are executed on the page. Remember, you’ve to create a file with .js extension.

document.addEventListener('DOMContentLoaded', function () {
  const button = document.getElementById('navbar-toggle');
  const menu = document.getElementById('navbar-dropdown');

  const dropdownButton = document.getElementById('dropdownNavbarLink');
  const dropdownMenu = document.getElementById('dropdownNavbar');

  dropdownButton.addEventListener('click', () => {
    dropdownMenu.classList.toggle('hidden');
  });

  button.addEventListener('click', function () {
    menu.classList.toggle('hidden');
  });
});

Final Output:

Tailwind CSS Tutorial Build a Navbar with Dropdown Menu.gif

Conclusion:

Congratulations! You've successfully navigated the world of Tailwind CSS to craft a sleek and responsive Navbar with Dropdown menus. As we wrap up this tutorial, let's recap what you've achieved and offer some final thoughts.

Now that you've accomplished all this, you're well-equipped to design and customize Navbars with Dropdown menus for your web projects. But remember, Tailwind CSS offers even more possibilities for enhancing your web development skills. Don't hesitate to explore further and adapt what you've learned here to other components and designs.

As you embark on your web development journey, keep experimenting, innovating, and pushing your creative boundaries. Building remarkable user interfaces is an art, and with Tailwind CSS in your toolkit, you're on the path to crafting exceptional digital experiences.

Thank you for joining us on this tutorial. We hope you found it both educational and inspiring. Happy coding, and may your web projects shine with the brilliance of Tailwind CSS!

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post