Creating a custom cursor for your website

Bridget Amana - Sep 7 - - Dev Community

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

. . . . . . . . . . . . . . . . .
Terabox Video Player