DEV Community

Cover image for Create a custom cursor for your website
Hossein Mobarakian
Hossein Mobarakian

Posted on

Create a custom cursor for your website

Hi, Do you think about how you can create a custom cursor for your website?
This section will show you how to create custom cursors quickly and easily. We must take simple step (Create a HTML file and paste the codes below into it)

    <div class="mouse" id="mouse">
        <img src="https://i.postimg.cc/NFx1b2f7/pack2586.png" alt="">
    </div>

    <style>
        body,
        html {

            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        * {
            cursor: none;

        }

        .mouse {
            width: 20px;
            height: 20px;
            background-color: none;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1000;
        }

        .mouse img {
            width: 40px;
        }
    </style>

    <script>
        document.onmousemove = function (e) {
            var mouse = document.getElementById("mouse");
            console.log(mouse.style);
            mouse.style.left = e.pageX + 1 + "px";
            mouse.style.top = e.pageY + 1 + "px";
        }
    </script>

Enter fullscreen mode Exit fullscreen mode

Top comments (8)

Collapse
 
link2twenty profile image
Andrew Bone • Edited

I thought I'd quickly make an example of each method the first is what you've done in this post and the second is using css only.

I've made it so when the mouse is hovering the button the button goes green.

Collapse
 
hosseinmobarakian profile image
Hossein Mobarakian

Thank you for your comment

Collapse
 
tsadarsh profile image
Adarsh TS

Thanks for the article and this comment.

Collapse
 
link2twenty profile image
Andrew Bone

The cursor property in css takes a URL for an image. Using JavaScript for this isn't great for performance.

developer.mozilla.org/en-US/docs/W...

Collapse
 
alvaromontoro profile image
Alvaro Montoro

This right here (animated gif)

Collapse
 
alvaromontoro profile image
Alvaro Montoro

Just use with caution so you don't confuse the user, and don't forget to use the coordinates for a better clicking experience.

Collapse
 
cristoferk profile image
CristoferK

Thanks for sharing this! Is very useful!

Collapse
 
ianwijma profile image
Ian Wijma • Edited

So on touch devices this is a terrible user experience. Also changing your cursor is a terrible user experience from 2000/2010.