乐闻世界logo
搜索文章和话题

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

1个答案

1
  1. Set up the project environment: First, ensure that TailwindCSS is correctly installed and configured in your project.

  2. Extend TailwindCSS configuration: In the tailwind.config.js file, extend the utilities to add custom border styles:

javascript
module.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.

  1. Use the new class in HTML files: Apply the border-gradient utility 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.

2024年6月29日 12:07 回复

你的答案