How to make a button a linear gradient color border in tailwind css? – Javascript

by
Ali Hasan
next.js reactjs tailwind-css

The Problem:

I am currently using Next.js and Tailwind CSS to create a webpage. I want to add a button with a linear gradient border using Tailwind CSS. How can I achieve this with my existing code snippet?

The Solutions:

Solution 1: Using Linear Gradient Background

To create a linear gradient background for your button, use the `bg-` class followed by the desired gradient classes. For instance, to create a gradient from blue to purple, use the following:

<button class="bg-blue-500 to-blue-700 text-white font-semibold py-2 px-4 rounded">
  Button
</button>

Solution 2: Border Gradient Effect

To create the appearance of a border gradient, create a child element within the button that inherits the background color of its parent. The parent button should have a full-width gradient background. This technique creates the impression of a gradient border:

<button class="bg-blue-500 to-blue-700 rounded p-1">
  <span class="flex w-full bg-gray-900 text-white rounded p-2">
    Button
  </span>
</button>

Solution 2: Using bg-gradient-to class

To create a linear gradient color border for a button using Tailwind CSS, you can utilize the bg-gradient-to class along with the from and to properties to specify the colors of the gradient.

<button class="relative mt-5 text-white font-bold py-2 px-4 rounded-full overflow-hidden shadow-md transition duration-300 ease-in-out transform hover:scale-105">
  <span class="relative z-10">Learn More</span>
  <span class="absolute inset-0 bg-gradient-to-r from-blue-500 to-green-500 border-2 border-transparent rounded-full"></span>
</button>

In this example:

  • The relative class positions the button relative to its parent.
  • The overflow-hidden class prevents the gradient from overflowing beyond the button’s borders.
  • The absolute class positions the gradient absolutely within the button.
  • The from and to properties of the bg-gradient-to-r class define the starting and ending colors of the gradient, respectively.
  • The border-2 and border-transparent classes add a 2px transparent border to the gradient to enhance the visual effect.

Q&A

How to make a button border as a linear gradient color in tailwind css?

Use bg-gradient-to-r class along with from and to colors to get a linear gradient border in tailwindcss.

Could you provide a code example of above given solution?

Use a span inside a button element as follows to provide gradient effect as button border :

button class=&quot;relative mt-5 text-white font-bold py-2 px-4 rounded-full overflow-hidden shadow-md transition duration-300 ease-in-out transform hover:scale-105&quot;&gt;<span class=&quot;relative z-10&quot;&gt;Learn More&lt;/span&gt;<span class=&quot;absolute inset-0 bg-gradient-to-r from-blue-500 to-green-500 border-2 border-transparent rounded-full&quot;&gt;&lt;/span&gt;<button>

Video Explanation:

The following video, titled "Glowing Background Gradient Effects with Tailwind CSS - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

In this video, I'll show you how to achieve that awesome glow effect popping up everywhere with vanilla Tailwind CSS utilities.