How to Build Custom Cursors & Magnetic Pointer Effects in CSS & JS
To create custom mouse cursors in CSS, define custom SVG or PNG pointers using cursor: url("cursor.svg") x y, auto where x y specifies the active hotspot offset coordinates. For animated trailing dots and magnetic magnetic hover snaps, combine a small fixed pointer dot with a lagging ring element positioned via pointer-events: none and animated using GPU transforms. Always wrap custom cursor tracking in @media (pointer: fine) to disable custom cursors automatically on mobile touchscreens.
Step 1: Native CSS cursor: url() with Precise Coordinate Hotspots
You can replace the default browser cursor with any custom graphic in pure CSS using the cursor property. Crucially, you must declare active hotspot coordinates (the exact X and Y pixel where click events register) and provide a fallback system cursor like auto or pointer.
/* Hotspot at 4px X and 4px Y with fallback to default pointer */
.custom-link {
cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Ccircle cx='4' cy='4' r='4' fill='%236366f1'/%3E%3C/svg%3E") 4 4, pointer;
}
Step 2: Building Trailing Cursor Rings with pointer-events: none
A fatal bug with custom trailing cursor rings is that they sit physically beneath the user's mouse and intercept click events, rendering the entire website unclickable. Adding pointer-events: none allows clicks to pass through the visual cursor ring directly to underlying links and buttons.
.cursor-dot, .cursor-ring {
position: fixed;
top: 0;
left: 0;
/* CRITICAL: Never block underlying mouse clicks */
pointer-events: none;
z-index: 99999;
border-radius: 50%;
will-change: transform;
}
.cursor-dot {
width: 8px;
height: 8px;
background: #ffffff;
}
.cursor-ring {
width: 36px;
height: 36px;
border: 1.5px solid rgba(255, 255, 255, 0.4);
transition: transform 0.15s ease-out, width 0.2s ease, height 0.2s ease;
}
Step 3: Inverting Backgrounds with mix-blend-mode: difference
High-end agency portfolios feature cursors that seamlessly invert colors whether moving over dark or light sections. Setting mix-blend-mode: difference on a white cursor ring makes it appear pitch black over white backgrounds and stark white over dark backgrounds automatically.
.cursor-ring-invert {
position: fixed;
pointer-events: none;
width: 32px;
height: 32px;
background: #ffffff;
border-radius: 50%;
/* Inverts underlying page colors */
mix-blend-mode: difference;
z-index: 10000;
}
Step 4: Disabling Custom Cursors on Touchscreens with @media (pointer: fine)
Smartphones and tablets do not have a persistent mouse pointer. A trailing cursor dot following touch gestures looks clumsy and confuses mobile users. Use the CSS interaction media feature @media (pointer: fine) to display custom cursors exclusively on devices with precise mice.
/* Hide custom cursor elements by default (mobile first) */
.custom-cursor-element {
display: none;
}
/* Enable only when a true mouse or trackpad is detected */
@media (pointer: fine) {
.custom-cursor-element {
display: block;
}
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why are clicks not registering when I use a custom trailing cursor?
Custom cursor elements float over the document and intercept pointer events. You must add pointer-events: none to all custom cursor container elements so clicks pass through seamlessly to links and buttons beneath.
What is the maximum recommended dimensions for custom CSS cursor images?
The W3C recommends cursor dimensions no larger than 32x32 pixels (or 64x64 on @2x high-DPI displays). Many browsers will reject or clip custom cursor images exceeding 128x128 pixels.
How do I specify the click hotspot on a custom CSS cursor?
Provide X and Y coordinates after the image URL: cursor: url("cursor.png") 8 8, auto. The numbers represent the exact pixel offset within the image that triggers clicks.
How do I prevent custom cursors from appearing on smartphones?
Wrap all custom cursor styles and JavaScript event listeners inside a CSS media query check for @media (pointer: fine). This ensures custom cursors only execute on devices with high-precision mice or trackpads.
How does mix-blend-mode: difference work on a custom cursor?
mix-blend-mode: difference subtracts the cursor’s RGB values from the background color beneath it. A white cursor (RGB 255, 255, 255) inverted over white results in black (0, 0, 0), and inverted over black results in white (255, 255, 255).