When working on a customized design, you often need to change the cursor icon depending on the requirements. You may want to show not-allowed
icon on disabled elements, a loading icon when a request is submitted, and so on. TailwindCSS offers every possible cursor icon available in CSS.
How to Change Cursor in TailwindCSS
You can use cursor-
class to change the cursor icon. All you need to do is to add cursor-
and press crtl+space
and the intellSense will do the rest. Here is a simple example of changing the cursor in TailwindCSS.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<div class="flex flex-col h-screen bg-slate-200 w-full space-x-4 justify-center items-center"> <h1 class="text-3xl py-10"> Hover on Elements </h1> <div class="grid grid-cols-2 gap-4"> <div class="bg-green-500 w-52 h-24 flex justify-center items-center cursor-pointer"> <p class="text-white p-4 text-2xl">Pointer </p> </div> <div class="bg-white w-52 h-24 flex justify-center items-center cursor-not-allowed"> <p class="p-4 text-2xl">Not Allowed </p> </div> <div class="bg-white w-52 h-24 flex justify-center items-center cursor-move"> <p class="p-4 text-2xl">Move </p> </div> <div class="bg-green-500 w-52 h-24 flex justify-center items-center cursor-zoom-in"> <p class="text-white p-4 text-2xl">Zoom in </p> </div> </div> </div> |
You can check the working example of the above code on the tailwind playground. Also, check the available classes for the cursor in TailwindCSS.
How to Add Custom Icon for Mouse Pointer
You can also add your own icon if you have a .cur
file. There are two ways to do that in TailwindCSS.
Method 1: Arbitrary Value to Add Custom Icon for Mouse Pointer
You can specify the URL to your icon with an arbitrary value. Check the following code to see how it works.
1 2 3 4 5 6 7 |
<div class="cursor-[url(hand.cur),_pointer]"> <p class="text-white p-4 text-2xl">Customized Icon</p> </div> |
Method 2: Extending Theme to Add Custom Icon for Mouse Pointer
The second option is to add a custom icon in the config file. You need to extend the cursor
property in the theme and provide the URL of the file. The following code adds a smiley
cursor icon in the theme.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
module.exports = { theme: { extend: { cursor: { 'smiley': 'url(smiley.cur), pointer', } } } } |
Conclusion
Changing a cursor icon is very easy in TailwindCSS. The cursor-
class allows you to specify any cursor you want to use on your web page. You can also add your custom icon for the cursor with arbitrary values or by extending the theme.