Open In App

How To Dynamically Change Border Color In Next js With Tailwind CSS?

Last Updated : 21 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Next.js, Tailwind CSS can be dynamically used to create responsive and interactive web applications. By using Tailwind's utility classes alongside JavaScript or React state, you can dynamically change styles like border color based on user interactions or application state. This article will guide you through the process of implementing dynamic border color in Next js with Tailwind CSS.

Prerequisites

These are the approaches for how to dynamically change border color in next js with Tailwind CSS

Steps To Dynamically Change Border Color

Step 1: Create a nextJS application by using this command

npx create-next-app my-app

Step 2: Install Tailwind CSS by running this command

npm install tailwindcss

Step 3: Configure Tailwind CSS by creating a ‘tailwind.config.js’ file and adding any customization if needed.

Folder Structure

PS
Folder Structure

Dependencies

"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.3"
},
"devDependencies": {
"postcss": "^8",
"tailwindcss": "^3.4.1"
}

1. Using Tailwind CSS with State and Conditional Classes

In this approach, we used classes from Tailwind CSS to dynamically change the border color based on the component's state. By toggling between two different Tailwind classes (border-red-500 and border-blue-500), the border color changes when the button is clicked, showing conditional rendering in NextJS.

Example: This example shows how to dynamically change border color of a 'div' element using State Management and Tailwind CSS utility classes.The 'use client' directive ensures that this component runs on client side.

JavaScript
//src/app/page.js

"use client";
import { useState } from 'react';

export default function Page() {
    const [borderColor, setBorderColor] = useState('border-red-500');

    const handleChangeColor = () => {
        setBorderColor(prev => prev === 'border-red-500' ? 'border-blue-500' : 'border-red-500');
    };

    return (
        <div className="flex flex-col items-center p-6">
            <h3 className="text-lg font-semibold mt-4">State and Conditional Classes</h3>
            <div className={`w-64 h-64 mt-6 border-4 ${borderColor}`} />
            <button
                className="mt-4 p-2 bg-green-500 text-white rounded"
                onClick={handleChangeColor}
            >
                Change Border Color
            </button>
        </div>
    );
}

Output

change-border
Using Tailwind CSS with State and Conditional Classes


2. Using Tailwind CSS with Inline Style Binding

In this approach, we are using Tailwind CSS for layout and styling, while dynamically changing the border color through inline style binding. By integrating a color picker input, users can select any color to apply as the border color of the box in real-time.

Example: This example shows how to dynamically change border color of a 'div' element using State Management and Tailwind CSS utility classes.The 'use client' directive ensures that this component runs on client side.

JavaScript
// src/app/page.js

"use client";
import { useState } from 'react';

export default function Page() {
    const [borderColor, setBorderColor] = useState('#ff0000');

    const handleColorChange = (event) => {
        setBorderColor(event.target.value);
    };

    return (
        <div className="flex flex-col items-center p-6">
            <h3 className="text-lg font-semibold mt-4"> Inline Style
                Binding with Color Picker</h3>
            <div
                className="w-64 h-64 mt-6 border-4"
                style={{ borderColor: borderColor }}
            />
            <input
                type="color"
                value={borderColor}
                onChange={handleColorChange}
                className="mt-4 p-2 border-2 border-gray-300 rounded"
            />
        </div>
    );
}

Output



Next Article

Similar Reads