2

How to replace all cursors with my custom image in tailwindcss?

My attempt

In tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      cursor: {
        default: "url(/images/cursor.png)",
        pointer: "url(/images/cursorPointer.png)",
      },
    },
  },
};

Answer:

In global.css:

*,
*:before,
*:after {
  @apply cursor-default;
}

a, button {
  @apply cursor-pointer;
}

In tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      cursor: {
        default: 'url(/images/cursor.png), default',
        pointer: 'url(/images/cursorPointer.png), pointer',
      },
    },
  }
}
Jingles
  • 875
  • 1
  • 10
  • 31
  • Does this answer your question? [Using external images for CSS custom cursors](https://stackoverflow.com/questions/18551277/using-external-images-for-css-custom-cursors) – gru Jan 25 '22 at 08:47
  • That isn't using the tailwindcss "tailwind.config.js" – Jingles Jan 25 '22 at 08:55
  • I was referring to the cause, that an image could not be used as expected. – gru Jan 25 '22 at 08:58
  • Are you sure that your image is available? You could try using an external image instead. – gru Jan 25 '22 at 08:59

1 Answers1

2

The <url> value must be followed by a single keyword value:

/* URL with mandatory keyword fallback */
cursor: url(/images/cursor.png), pointer;

See: https://developer.mozilla.org/en-US/docs/Web/CSS/cursor#syntax

So, in your tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      cursor: {
        default: 'url(/images/cursor.png), default',
        pointer: 'url(/images/cursor.png), pointer',
      },
    },
  },
  plugins: [],
}

Demo: https://play.tailwindcss.com/Nz8Ur49ENq


If you want to replace the cursor for all the elements in the page, a solution might be:

*,
*:before,
*:after {
  @apply cursor-default;
}

Example: https://play.tailwindcss.com/XdgFOu86ix?file=css

andreivictor
  • 7,628
  • 3
  • 48
  • 75