30

I'm trying to apply a custom cursor pointer with an SVG image but it's not working, instead if I use a png image everything is working fine.

Here's my code.

.container {
  /* not working one */
  cursor: url("/images/icon-cross.svg"), auto;
  /* working one */
  cursor: url("/images/icon-cross.png"), auto;
}

Is there any trick/workaround to make it working also with SVG or it's something which is not supported?

Thanks

UPDATE

Here's the svg code:

 <?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="512px" height="512px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
<path d="M443.6,387.1L312.4,255.4l131.5-130c5.4-5.4,5.4-14.2,0-19.6l-37.4-37.6c-2.6-2.6-6.1-4-9.8-4c-3.7,0-7.2,1.5-9.8,4
    L256,197.8L124.9,68.3c-2.6-2.6-6.1-4-9.8-4c-3.7,0-7.2,1.5-9.8,4L68,105.9c-5.4,5.4-5.4,14.2,0,19.6l131.5,130L68.4,387.1
    c-2.6,2.6-4.1,6.1-4.1,9.8c0,3.7,1.4,7.2,4.1,9.8l37.4,37.6c2.7,2.7,6.2,4.1,9.8,4.1c3.5,0,7.1-1.3,9.8-4.1L256,313.1l130.7,131.1
    c2.7,2.7,6.2,4.1,9.8,4.1c3.5,0,7.1-1.3,9.8-4.1l37.4-37.6c2.6-2.6,4.1-6.1,4.1-9.8C447.7,393.2,446.2,389.7,443.6,387.1z"/>
</svg>
Alessio
  • 3,134
  • 19
  • 36
  • 46

2 Answers2

37

Your image is simply too large. Reduce the width and height to something less than 128px.

Icon size limits for cursors in CSS | MDN

...the limit of the cursor size is 128×128px. Larger cursor images are ignored.

Example:

cursor: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32' ...") 16 16, pointer;

https://jsfiddle.net/bx4og7n5/

Edit: Added hotspot (center coordinates) for the cursor (see Dennis Bauszus' comment)

Stephen Ostermiller
  • 21,408
  • 12
  • 81
  • 104
Sphinxxx
  • 11,400
  • 4
  • 49
  • 78
  • Thanks! It's working fine! In the fiddle I saw you move the svg inside the CSS, is it the base64 encode? – Alessio Aug 30 '17 at 21:20
  • 2
    It's actually not base64 (although that would also be an option). This is the SVG's XML as-is with only a few characters encoded to make it readable while still being valid CSS. This article explains the technique: https://codepen.io/tigt/post/optimizing-svgs-in-data-uris – Sphinxxx Aug 30 '17 at 21:57
  • 2
    i just had the same issue. but in my case, the svg was compressed and did not have width & height attribute, only viewBox. once i set those, they were working fine. – honk31 Sep 03 '18 at 16:40
  • 1
    Nice example. You might want to add the clampoints x, y, to center the icon though. – Dennis Bauszus Sep 16 '20 at 09:07
  • @honk31 Thanks! Funnily enough the width/height don't have any affect, but need to be present. – kontur May 12 '21 at 08:27
  • Handy library, and in-browser utility, to do what @Sphinxxx mentioned: https://npm.runkit.com/mini-svg-data-uri – m_floer Aug 03 '21 at 03:34
0

You can also use a Blob URL to insert inside the url function of CSS.

const blob = new Blob([**your SVG String**],{type: 'image/svg+xml'});
const URL = window.URL.createObjectURL(blob);

Now you can use this URL variable and can insert inside the url function of CSS to obtain desired cursor pointer.