-
Set up the project environment: First, ensure that TailwindCSS is correctly installed and configured in your project.
-
Extend TailwindCSS configuration: In the
tailwind.config.jsfile, extend the utilities to add custom border styles:
javascriptmodule.exports = { // Other configurations... plugins: [ function({ addUtilities }) { const newUtilities = { '.border-gradient': { borderImageSlice: 1, borderImageSource: 'linear-gradient(to left, #743ad5, #d53a9d)', }, }; addUtilities(newUtilities, ['responsive', 'hover']); }, ], };
In this code snippet, .border-gradient is the newly defined utility class. borderImageSlice: 1 ensures the gradient fully covers the border, while borderImageSource specifies the gradient colors, transitioning from left to right from #743ad5 to #d53a9d.
- Use the new class in HTML files:
Apply the
border-gradientutility class to set the button's border in your HTML. For example:
html<button class="border-2 border-gradient p-2 rounded text-white bg-black hover:bg-gray-700"> Click me </button>
Here, border-2 sets the border width, p-2 sets the padding, rounded rounds the button corners, and text-white bg-black hover:bg-gray-700 defines the text color, background color, and hover effect.
By following these steps, you can implement a button with a linear gradient border in TailwindCSS. This approach offers flexibility in defining gradient colors and directions, enhancing the interface with a more vibrant and personalized appearance.