【问题标题】:I can't use inline svg as custom cursor in CSS我不能在 CSS 中使用内联 svg 作为自定义光标
【发布时间】:2020-08-16 12:28:45
【问题描述】:

这是我的代码。如您所见,我想使用 inline svg 使用自定义光标更改光标,但不起作用。但是,如果我将代码作为背景,则可以正常工作:

.container {
  width: 50vw;
  height: 50vh;
  background-color:#ccc;
  cursor: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8' standalone='no'%3F%3E%3Csvg xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:cc='http://creativecommons.org/ns%23' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns%23' xmlns:svg='http://www.w3.org/2000/svg' xmlns='http://www.w3.org/2000/svg' xmlns:sodipodi='http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd' xmlns:inkscape='http://www.inkscape.org/namespaces/inkscape' version='1.1' id='Layer_1' x='0px' y='0px' viewBox='0 0 200 199.99999' xml:space='preserve' sodipodi:docname='close-icon.svg' inkscape:version='1.0 (4035a4fb49, 2020-05-01)' width='200' height='200'%3E%3Cmetadata id='metadata852'%3E%3Crdf:RDF%3E%3Ccc:Work rdf:about=''%3E%3Cdc:format%3Eimage/svg+xml%3C/dc:format%3E%3Cdc:type rdf:resource='http://purl.org/dc/dcmitype/StillImage' /%3E%3Cdc:title%3E%3C/dc:title%3E%3C/cc:Work%3E%3C/rdf:RDF%3E%3C/metadata%3E%3Cdefs id='defs850' /%3E%3Csodipodi:namedview pagecolor='%23ffffff' bordercolor='%23666666' borderopacity='1' objecttolerance='10' gridtolerance='10' guidetolerance='10' inkscape:pageopacity='0' inkscape:pageshadow='2' inkscape:window-width='1366' inkscape:window-height='705' id='namedview848' showgrid='false' inkscape:zoom='1.6244302' inkscape:cx='-6.1504656' inkscape:cy='92.478743' inkscape:window-x='-8' inkscape:window-y='-8' inkscape:window-maximized='1' inkscape:current-layer='Layer_1' inkscape:document-rotation='0' inkscape:snap-global='true' /%3E%3Cg id='g817'%3E%3C/g%3E%3Cg id='g819'%3E%3C/g%3E%3Cg id='g821'%3E%3C/g%3E%3Cg id='g823'%3E%3C/g%3E%3Cg id='g825'%3E%3C/g%3E%3Cg id='g827'%3E%3C/g%3E%3Cg id='g829'%3E%3C/g%3E%3Cg id='g831'%3E%3C/g%3E%3Cg id='g833'%3E%3C/g%3E%3Cg id='g835'%3E%3C/g%3E%3Cg id='g837'%3E%3C/g%3E%3Cg id='g839'%3E%3C/g%3E%3Cg id='g841'%3E%3C/g%3E%3Cg id='g843'%3E%3C/g%3E%3Cg id='g845'%3E%3C/g%3E%3Cpath d='M 4.6524564e-7,4.3710052 195.62899,200 200,195.62899 4.371006,-2.5331698e-7 Z' style='fill:%23000000;stroke:none;stroke-width:7.43033;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:0.959839' id='rect961' /%3E%3Cpath id='path862' style='fill:%23000000;stroke:none;stroke-width:7.43033;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:0.959839' d='M 200,4.3710052 4.3710079,200 4.6524564e-7,195.62899 195.62899,-2.5331698e-7 Z' /%3E%3C/svg%3E%0A");
}
<div class="container"></div>

【问题讨论】:

  • @RobertLongson 将评论转换为答案,以便我将其选为最佳答案。
  • 请阅读:stackoverflow.com/questions/45962081/… 同样在您的 svg 中,您有很多空组和一个没有内容的 defs 元素。

标签: html css svg


【解决方案1】:

你有两个问题:

  • 浏览器通常将光标的最大尺寸限制为 128 x 128 像素以防止滥用。您的尺寸为 200 x 200 像素,因此将被忽略。
  • url 游标requires a fallback 的语法。

【讨论】:

    【解决方案2】:

    在您的数据网址后添加, auto

    cursor: url(data:image/png;base64,...), auto;
    

    您应该将后备设置为自动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多