The CSS cursor property defines the type of mouse pointer displayed when hovering over an element. It enhances user experience by giving visual cues about what actions can be performed - for example, whether something is clickable, draggable, or editable.
The basic syntax for setting a cursor is simple:
selector {
cursor: value;
}
You can use a predefined keyword or a custom image as a cursor. Here’s an example of a standard pointer for links:
a {
cursor: pointer;
}
CSS provides a wide range of built-in cursor types. Here are some of the most commonly used ones:
.default { cursor: default; }
.pointer { cursor: pointer; }
.text { cursor: text; }
.move { cursor: move; }
.not-allowed { cursor: not-allowed; }
.wait { cursor: wait; }
.help { cursor: help; }
.crosshair { cursor: crosshair; }
.grab { cursor: grab; }
.grabbing { cursor: grabbing; }
You can use a custom image as a cursor by specifying a url() value. It can be a PNG, SVG, or even a Base64-encoded image. A fallback cursor type is required in case the image fails to load.
button {
cursor: url('custom-cursor.png'), pointer;
}
You can also define the hotspot position (the exact point of click) by adding two numbers after the image URL:
button {
cursor: url('cursor.svg') 4 4, auto;
}
Here, 4 4 defines the X and Y coordinates of the hotspot relative to the image’s top-left corner.
Some browsers support animated cursors using .ani or .cur files, mainly in Windows environments. However, for cross-browser compatibility, stick to static images like PNG or SVG.
Using cursors in a user interface can improve usability. For example, when dragging elements or resizing boxes:
.draggable {
cursor: grab;
}
.resizable {
cursor: nwse-resize;
}
Resizing cursors such as n-resize, e-resize, nwse-resize, and nesw-resize indicate which direction resizing is possible.
Always include a fallback value when using custom cursors. This ensures that even if a browser fails to load your image, the user still sees a functional cursor.
.custom {
cursor: url('fancy-cursor.png'), auto;
}
- Avoid using overly large or distracting cursors, as they can harm usability.
- Use high-contrast images for visibility.
- Test across different browsers to ensure compatibility.
- Keep your cursor files small (under 32x32 pixels is ideal).
The cursor property is a simple yet powerful CSS feature that helps improve interactivity and user feedback. Whether you’re designing buttons, drag-and-drop elements, or custom interfaces, the right cursor choice can make your design feel more intuitive and responsive. With CSS, you can easily combine standard cursors with custom images for a unique and engaging user experience.