Creating a custom cursor for your website
Bridget Amana

Bridget Amana @bridget_amana

About: Just a curious frontend developer learning in public

Location:
Lagos, Nigeria
Joined:
Jun 27, 2024

Creating a custom cursor for your website

Publish Date: Sep 7 '24
24 13

I’ve been working on my portfolio website to showcase my work, and I wanted it to stand out with a touch of personality. I thought, “Why not add a custom cursor to make the experience a bit more exciting?” so, I decided on a sleek, circular cursor that would follow the mouse. If you’re looking to give your website a similar unique touch, follow along as I show you how to create a custom circle cursor using a bit of CSS and JavaScript.

Step 1: Set Up Your HTML

To start, we’ll set up a basic HTML structure and include a div element that will serve as our custom cursor. This will allow us to style and position it as needed.

Add the following code to your html:

<body>
    <h1>Custom circle cursor</h1>
    <!-- The div below is what is needed -->
    <div class="custom-cursor"></div>
    <script src="script.js"></script>
</body>
Enter fullscreen mode Exit fullscreen mode

Step 2: Style the Cursor with CSS

Now, let’s style our div to look like a stylish circle cursor. We’ll hide the default cursor and apply custom styles to the new div.

Create a CSS file named styles.css and include the following code:

/* Hide the default cursor */
body {
    cursor: none;
}

/* Style the custom circle cursor */
.custom-cursor {
    position: absolute;
    width: 30px;  
    height: 30px;
    border-radius: 50%;
    border: 1px solid #000;
    pointer-events: none;
    transform: translate(-50%, -50%);
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Move the Cursor with JavaScript

Next, we’ll add JavaScript to make the circle cursor follow the mouse pointer. This step involves updating the position of the div based on mouse movements.

In your Javascript file add the following code:

// Get the cursor element
const cursor = document.querySelector('.custom-cursor');

// Update cursor position based on mouse movement
document.addEventListener('mousemove', (e) => {
    cursor.style.left = `${e.clientX}px`;
    cursor.style.top = `${e.clientY}px`;
});
Enter fullscreen mode Exit fullscreen mode
  • Select the Cursor Element: document.querySelector('.custom-cursor'); grabs the div we styled in CSS.
  • Track Mouse Movement: document.addEventListener('mousemove', (e) => { ... }); listens for mouse movements and updates the cursor’s left and top properties to follow the mouse. e.clientX and e.clientY provide the current mouse coordinates.

Here’s a heads-up: While setting up my custom cursor, I noticed that when viewing my portfolio website with my phone I faced issues with the cursor blocking touch interactions and it just being there. To address this, just add the CSS below and you'll be fine

@media (pointer: coarse) {
    .custom-cursor {
        display: none; /* This hides the custom cursor on touch devices */
    }
}
Enter fullscreen mode Exit fullscreen mode

And there you have it! With just a bit of CSS and JavaScript, you’ve added a personal and interactive element to your website. Feel free to tweak it to your preferred shape, and color, or even add an image as the cursor.
Demo

Comments 13 total

  • Ashutosh_dev
    Ashutosh_devSep 7, 2024

    Try to attach some demo link.

    • Bridget Amana
      Bridget AmanaSep 7, 2024

      Thank you for the tip
      I’ve added a demo link

  • Md Nazmus Sakib
    Md Nazmus SakibSep 8, 2024

    very informative

  • Himanshu Sorathiya
    Himanshu Sorathiya Sep 8, 2024

    Wow, very good,

  • Pooya Deperson
    Pooya DepersonSep 8, 2024

    Amazing and clean. I was looking for a similar interaction with a bit of offset, so made this little changed to add offset of 100. The performance is really good I have seen similar interactions but much slower. thanks {}

    Image description

    document.addEventListener('mousemove', (e) => {
        cursor.style.left = `${e.clientX + 100}px`;
        cursor.style.top = `${e.clientY + 100}px`;
    });
    
    Enter fullscreen mode Exit fullscreen mode
    • Bridget Amana
      Bridget AmanaSep 8, 2024

      Thanks for your feedback! Glad you liked it

  • Pooya Deperson
    Pooya DepersonSep 8, 2024

    Nice work, there is an issue on scrolling, try to add height:2000px; to scroll down, the cursor will not follow the correct position when the page scrolled.

  • Krunal Kanojiya
    Krunal KanojiyaSep 8, 2024

    Great work!! This types of small things are so required and its not easy to find best clean and source. So keep posting this type of necessary things. Also if you don’t mind I would like to suggest you please attach demo. So it will help us to understand, how actually output looks like…🙌🏻🤘🏻

  • Parker Elie
    Parker ElieSep 9, 2024

    really cool!!

  • Armando Ota
    Armando OtaSep 9, 2024

    specially cool for people with disabilities

  • BadCat Design
    BadCat DesignSep 10, 2024

    *Fun! *
    Perhaps the cursor: none should go on html instead of body, like this... so if the body isn't full height the default cursor doesn't show up?
    html {
    cursor: none;
    }

    IDK if that has any other issues in doing it that way?

    demo

Add comment