CSS Cursor Property: A Beginner's Guide With Examples

Guides

The CSS cursor property decides what the mouse pointer looks like over an element. This beginner guide shows keyword values, custom images, hotspots and the limits you will hit.

1. Keyword values

button {
  cursor: pointer;
}

p {
  cursor: text;
}

The browser picks the picture. For what each keyword means, read cursor types explained.

2. A custom image with a fallback

.card {
  cursor: url("cursor.png"), auto;
}

The list ends with a keyword. MDN says this non-URL fallback must be at the end. It is used if no image loads.

3. The hotspot

.card {
  cursor: url("cursor.png") 4 12, auto;
}

The two numbers are the hotspot: the exact pixel that counts as the click point. They are measured in image pixels from the top-left corner, which is 0 0. If you leave them out, the browser may read the hotspot from the file, or it defaults to 0 0.

4. Several images

.card {
  cursor:
    url("cursor.svg") 4 5,
    url("cursor.png") 4 5,
    pointer;
}

The browser tries each image in order and moves on if one fails.

Where custom cursors do not apply

MDN notes that cursor changes over browser toolbar areas are commonly blocked to prevent spoofing. Your CSS only works on your own page. To use a custom pointer on other sites, try the cursor collections with the extension. For design tips, see how to design a custom cursor.

Why do I need a keyword at the end?

It is the fallback. Without it, the declaration is not valid.

A complete small example

Here is a page with a styled box. Copy it, swap in your own image path, and open the file in a browser.

<div class="box">Hover me</div>

<style>
  .box {
    padding: 40px;
    background: #eee;
    cursor: url("star.png") 16 16, auto;
  }
</style>

If star.png is 32 by 32 pixels, the hotspot 16 16 puts the click point in the middle of the image. That is a good choice for a round or centered design. For an arrow-shaped cursor, put the hotspot at the tip, usually near 0 0.

Common mistakes

  • No fallback keyword. Always finish with auto, pointer or another keyword.
  • Image too big. Stay at 32 by 32 pixels or less. Browsers may ignore anything over their limit.
  • Wrong file path. A path that does not load sends the browser to the fallback.
  • Hotspot off the tip. Clicks feel wrong when the click point is not where the eye expects.

Formats

MDN lists PNG and static SVG with a natural size as formats browsers must support. Desktop browsers also broadly support the .cur format. Animated formats are optional for browsers, so do not count on them.

Next, read the history of the mouse pointer for the story behind the arrow.

Sources

Related posts