【问题标题】:SVG in 'background-image:url(background.svg)' not displaying'background-image:url(background.svg)' 中的 SVG 不显示
【发布时间】:2021-12-19 06:40:37
【问题描述】:

我想使用 svg 图像作为 div 的背景,但它没有显示在任何浏览器上。

附上以下截图:

HTML:

<div class="landing></div>

CSS:

background-image : url(bg.svg);
width:100vw;
height:100vh;
background-repeat: no-repeat;
background-size: contain;

请注意,我使用的 svg 相当复杂。在使用具有简单形状和单一颜色的 svg 时,它似乎正在工作。附上 svg 的截图供您参考。enter image description here

最小可重现示例: Codepen

【问题讨论】:

  • 你能提供一个minimal reproducible example吗?我们需要实际文件,以便我们自己查看问题。
  • 这是一个 codepen 说明我的问题。
  • 在 Firefox 上似乎对我有用。
  • 是的,在 firefox 和 safari 上运行良好,但在 chrome 上运行良好
  • 这可能是一件多云的事情。如果我使用不同的 svg 图片我没问题。

标签: html css svg


【解决方案1】:

问题来自 cloudinary 文件路径。 您可以在 cloudinary 中使用 fl_sanitize 标志或 您可以为后台 URL 设置本地文件路径

这里详细介绍 Cloudinary https://support.cloudinary.com/hc/en-us/community/posts/115000752672/comments/115000145951中的 SVG 文件

【讨论】:

  • 用本地文件路径试过了,还是不行。有趣的是,使用更简单的 SVG(具有单一形状和颜色)就可以了。
【解决方案2】:

您使用的 id 包含不符合要求的字符 - 在您的情况下,它是冒号 ':'

<g clip-path="url(#clip0_32:2)">

直接在 chrome 中查看 svg 文件将显示图像。 在 html 上下文中,chrome 将拒绝呈现它。

所以你可以很容易地用连字符替换所有出现的地方:

<svg width="1440" height="810" viewBox="0 0 1440 810" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_32-2)">
<rect width="1440" height="810" fill="white"/>
<g style="mix-blend-mode:darken" filter="url(#filter0_f_32-2)">
<ellipse cx="336" cy="473" rx="481" ry="288" fill="url(#paint0_radial_32-2)" fill-opacity="0.4" style="mix-blend-mode:multiply"/>
</g>
<g style="mix-blend-mode:darken" filter="url(#filter1_f_32-2)">
<ellipse cx="2068.5" cy="661" rx="474.5" ry="215" fill="url(#paint1_radial_32-2)" fill-opacity="0.2" style="mix-blend-mode:multiply"/>
</g>
</g>
<defs>
<filter id="filter0_f_32-2" x="-310.91" y="19.09" width="1293.82" height="907.82" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feBlend mode="normal" in="SourceGraphic" in2="BackgroundImageFix" result="shape"/>
<feGaussianBlur stdDeviation="82.955" result="effect1_foregroundBlur_32-2"/>
</filter>
<filter id="filter1_f_32-2" x="1428.09" y="280.09" width="1280.82" height="761.82" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feBlend mode="normal" in="SourceGraphic" in2="BackgroundImageFix" result="shape"/>
<feGaussianBlur stdDeviation="82.955" result="effect1_foregroundBlur_32-2"/>
</filter>
<radialGradient id="paint0_radial_32-2" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(-56.305 490.702) rotate(1.378) scale(1048.6 628.503)">
<stop stop-color="#217AE2"/>
<stop offset="0.419491" stop-color="#181FCE"/>
<stop offset="1" stop-color="#00B6DE"/>
</radialGradient>
<radialGradient id="paint1_radial_32-2" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(1681.5 674.215) rotate(1.0429) scale(1034.3 469.253)">
<stop stop-color="#217AE2"/>
<stop offset="0.419491" stop-color="#181FCE"/>
<stop offset="1" stop-color="#00B6DE"/>
</radialGradient>
<clipPath id="clip0_32-2">
<rect width="1440" height="810" fill="white"/>
</clipPath>
</defs>
</svg>

推荐
在 html/css/js 中使用您也将用于选择器的 Id 或类名。

另见Why do colons in linearGradient IDs break them when the SVG is used in an tag?

【讨论】:

  • 瞧!有效。非常感谢。
猜你喜欢
  • 2019-04-30
  • 2012-12-05
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 2013-02-17
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多