This content originally appeared on DEV Community and was authored by larainfo
In this tutorial, we will create responsive navbar menu in react js using tailwind css. We will see navbar with hamburger menu with react, tailwind navbar component, navnar with sign in and signup examples with Tailwind CSS & React.
Tool Use
React JS
Tailwind CSS 3.v
Heroicons Icon
First you need to setup react project with tailwind css. You can install manually or you read below blog.
How to install Tailwind CSS in React
Install & Setup Vite + React + Typescript + Tailwind CSS 3
Example 1
React JS responsive navbar menu with tailwind css.
import { useState } from "react";
export default function NavBar() {
const [navbar, setNavbar] = useState(false);
return (
<nav className="w-full bg-white shadow">
<div className="justify-between px-4 mx-auto lg:max-w-7xl md:items-center md:flex md:px-8">
<div>
<div className="flex items-center justify-between py-3 md:py-5 md:block">
<a href="javascript:void(0)">
<h2 className="text-2xl font-bold">LOGO</h2>
</a>
<div className="md:hidden">
<button
className="p-2 text-gray-700 rounded-md outline-none focus:border-gray-400 focus:border"
onClick={() => setNavbar(!navbar)}
>
{navbar ? (
<svg
xmlns="http://www.w3.org/2000/svg"
className="w-6 h-6"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fillRule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clipRule="evenodd"
/>
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
className="w-6 h-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 6h16M4 12h16M4 18h16"
/>
</svg>
)}
</button>
</div>
</div>
</div>
<div>
<div
className={`flex-1 justify-self-center pb-3 mt-8 md:block md:pb-0 md:mt-0 ${
navbar ? "block" : "hidden"
}`}
>
<ul className="items-center justify-center space-y-8 md:flex md:space-x-6 md:space-y-0">
<li className="text-gray-600 hover:text-blue-600">
<a href="javascript:void(0)">Home</a>
</li>
<li className="text-gray-600 hover:text-blue-600">
<a href="javascript:void(0)">Blog</a>
</li>
<li className="text-gray-600 hover:text-blue-600">
<a href="javascript:void(0)">About US</a>
</li>
<li className="text-gray-600 hover:text-blue-600">
<a href="javascript:void(0)">Contact US</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
);
}
tailwind responsive hamburger menu in react.
Example 2
React JS responsive navbar menu with tailwind css sign in and sign up button.
import { useState } from "react";
export default function NavBar() {
const [navbar, setNavbar] = useState(false);
return (
<nav className="w-full bg-purple-500 shadow">
<div className="justify-between px-4 mx-auto lg:max-w-7xl md:items-center md:flex md:px-8">
<div>
<div className="flex items-center justify-between py-3 md:py-5 md:block">
<a href="javascript:void(0)">
<h2 className="text-2xl font-bold text-white">LOGO</h2>
</a>
<div className="md:hidden">
<button
className="p-2 text-gray-700 rounded-md outline-none focus:border-gray-400 focus:border"
onClick={() => setNavbar(!navbar)}
>
{navbar ? (
<svg
xmlns="http://www.w3.org/2000/svg"
className="w-6 h-6 text-white"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fillRule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clipRule="evenodd"
/>
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
className="w-6 h-6 text-white"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 6h16M4 12h16M4 18h16"
/>
</svg>
)}
</button>
</div>
</div>
</div>
<div>
<div
className={`flex-1 justify-self-center pb-3 mt-8 md:block md:pb-0 md:mt-0 ${
navbar ? "block" : "hidden"
}`}
>
<ul className="items-center justify-center space-y-8 md:flex md:space-x-6 md:space-y-0">
<li className="text-white hover:text-indigo-200">
<a href="javascript:void(0)">Home</a>
</li>
<li className="text-white hover:text-indigo-200">
<a href="javascript:void(0)">Blog</a>
</li>
<li className="text-white hover:text-indigo-200">
<a href="javascript:void(0)">About US</a>
</li>
<li className="text-white hover:text-indigo-200">
<a href="javascript:void(0)">Contact US</a>
</li>
</ul>
<div className="mt-3 space-y-2 lg:hidden md:inline-block">
<a
href="javascript:void(0)"
className="inline-block w-full px-4 py-2 text-center text-white bg-gray-600 rounded-md shadow hover:bg-gray-800"
>
Sign in
</a>
<a
href="javascript:void(0)"
className="inline-block w-full px-4 py-2 text-center text-gray-800 bg-white rounded-md shadow hover:bg-gray-100"
>
Sign up
</a>
</div>
</div>
</div>
<div className="hidden space-x-2 md:inline-block">
<a
href="javascript:void(0)"
className="px-4 py-2 text-white bg-gray-600 rounded-md shadow hover:bg-gray-800"
>
Sign in
</a>
<a
href="javascript:void(0)"
className="px-4 py-2 text-gray-800 bg-white rounded-md shadow hover:bg-gray-100"
>
Sign up
</a>
</div>
</div>
</nav>
);
}
This content originally appeared on DEV Community and was authored by larainfo
larainfo | Sciencx (2022-07-03T07:14:06+00:00) React Responsive Navbar Menu With Tailwind CSS Example. Retrieved from https://www.scien.cx/2022/07/03/react-responsive-navbar-menu-with-tailwind-css-example/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.