最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a button a linear gradient color border in tailwind css? - Stack Overflow

programmeradmin4浏览0评论

I am trying Nextjs and tailwind css i created a simple page with that but now i am facing a problem that i want to make the button a linear gradient color border in tailwind css how can i do that with my code?

     <Link href={'/about'}>
        <button className="bg-black-500 hover:bg-blue-700 mt-5 text-white font-bold py-2 px-4 rounded-full border border-blue-500 shadow-md transition duration-300 ease-in-out transform hover:scale-105">
          Learn More
        </button>
      </Link>

I am trying Nextjs and tailwind css i created a simple page with that but now i am facing a problem that i want to make the button a linear gradient color border in tailwind css how can i do that with my code?

     <Link href={'/about'}>
        <button className="bg-black-500 hover:bg-blue-700 mt-5 text-white font-bold py-2 px-4 rounded-full border border-blue-500 shadow-md transition duration-300 ease-in-out transform hover:scale-105">
          Learn More
        </button>
      </Link>
Share Improve this question asked Jun 18, 2023 at 7:52 cwecaecwecae 1031 gold badge4 silver badges10 bronze badges 2
  • 1 Here is an example of a gradient border: dhairyashah.dev/posts/… – Brijesh Bhakta Commented Jun 18, 2023 at 7:58
  • @BrijeshBhakta Can you please give me a simple code example? i saw that blog post how to do things in my code please? – cwecae Commented Jun 18, 2023 at 8:08
Add a ment  | 

3 Answers 3

Reset to default 7

Here is the code to make your button's background into a linear gradient:

    <button class="bg-gradient-to-r from-blue-500 to-purple-500 text-white font-semibold py-2 px-4 rounded">
      Gradient Button
    </button>

You can find the detailed explanation about the tailwind CSS linear gradient classes here https://tailwindcss./docs/background-image#linear-gradients

If you want to make it look like it has its border as the linear gradient color then you can try something like this:

<button class="bg-gradient-to-r from-blue-500 to-purple-500 text-white font-semibold rounded p-1">
  <span class="flex w-full bg-gray-900 text-white rounded p-2">
  Gradient border
     </span>
</button>

It needs you to create another element inside it which will take the background color of the parent container where it resides and our main button will have a full bg gradient. That will create an illusion of having a border gradient. I am sure there might be other ways, including using pseudo-elements to create this effect.

This worked for me

<script src="https://cdn.tailwindcss."></script>
<button class="bg-gradient-to-r from-blue-500 to-purple-500 text-white font-semibold rounded p-1">
  <span class="flex w-full bg-gray-900 text-white rounded p-2">
  Gradient border
     </span>
</button>

This can be easily done by using bg-gradient-to class with from and to colors.

For example :

<Link href="/about">
  <button className="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 className="relative z-10">Learn More</span>
    <span className="absolute inset-0 bg-gradient-to-r from-blue-500 to-green-500 border-2 border-transparent rounded-full"></span>
  </button>
</Link>
发布评论

评论列表(0)

  1. 暂无评论