react router active navlink with tailwind css

In this tutorial, we will see how to use active navlink in react js with tailwind css using react router 6.


Tool Use

React JS

Tailwind CSS 3.v

React router


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 router with tailwind css active navlink. By default, an active class is added to a <NavLink> component when it is active so you can use CSS to style it.

index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

.active{
    @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}


Root.tsx / Home.tsx

import { NavLink } from "react-router-dom";

export default function Root() {
  return (
    <>
      <nav className="container flex justify-around py-8 mx-auto bg-white">
        <div>
          <h3 className="text-2xl font-medium text-blue-500">LOGO</h3>
        </div>
        <div className="space-x-8">
          <NavLink to="/">Home</NavLink>
          <NavLink to="/about">About</NavLink>
          <NavLink to="/contact">Contact US</NavLink>
        </div>
      </nav>
    </>
  );
}
react router 6 active navlink

react router 6 active navlink

Example 2

React router active navlink using className with tailwind css.

import { NavLink } from "react-router-dom";

export default function About() {
  return (
    <>
      <nav className="container flex justify-around py-8 mx-auto bg-white">
        <div>
          <h3 className="text-2xl font-medium text-blue-500">LOGO</h3>
        </div>
        <div className="space-x-8">
          <NavLink
            to="/"
            className={({ isActive }) =>
              isActive ? "text-blue-700 font-bold" : ""
            }
          >
            Home
          </NavLink>
          <NavLink
            to="/about"
            className={({ isActive }) =>
              isActive ? "text-blue-700 font-bold" : ""
            }
          >
            About
          </NavLink>
          <NavLink
            to="/contact"
            className={({ isActive }) =>
              isActive ? "text-blue-700 font-bold" : ""
            }
          >
            Contact US
          </NavLink>
        </div>
      </nav>
    </>
  );
}


You can also use shorthand method.

import { NavLink } from "react-router-dom";

export default function About() {
  const activeLink = "text-blue-700 font-bold";
  return (
    <>
      <nav className="container flex justify-around py-8 mx-auto bg-white">
        <div>
          <h3 className="text-2xl font-medium text-blue-500">LOGO</h3>
        </div>
        <div className="space-x-8">
          <NavLink
            to="/"
            className={({ isActive }) => (isActive ? activeLink : "")}
          >
            Home
          </NavLink>
          <NavLink
            to="/about"
            className={({ isActive }) => (isActive ? activeLink : "")}
          >
            About
          </NavLink>
          <NavLink
            to="/contact"
            className={({ isActive }) => (isActive ? activeLink : "")}
          >
            Contact US
          </NavLink>
        </div>
      </nav>
    </>
  );
}
react router 6 active navlink with tailwind css

react router 6 active navlink with tailwind css