【问题标题】:SVG LinearGradient hidden if svg is hidden in seperate class如果 svg 隐藏在单独的类中,则隐藏 SVG LinearGradient
【发布时间】:2020-02-11 14:10:59
【问题描述】:

如果 svg 隐藏在平行元素中,基本上发生的情况是线性渐变填充被隐藏

.mobile {
  display: none;
}

.content-svg {
  border: 1px solid black;
}
<div class="desktop">
  <!-- stuff here -->
</div>

<div class="mobile">
  <svg class="mobile-svg" height="150" width="400">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

<div class="content">
  <svg class="content-svg" height="150" width="400">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

正如你在上面的 sn-p 中看到的,SVG 元素在那里,线性渐变填充是隐藏的

如果我将填充更改为我的 CSS 中设置的纯色,它会按预期工作,这似乎只发生在渐变填充中

现在这只是我的整体问题的一个基本示例,我想避免重新创建 SVG,因为我在 Vue 中使用它并创建了一个可重用的 svg 组件

【问题讨论】:

  • 不要对两个不同的渐变使用相同的 ID,实际上只考虑第一个并且它有 display:none
  • @TemaniAfif 正如我所说,这个 svg 在我的应用程序中被多次重用,将 ID 更改为类似乎也不能解决问题
  • 不是一个类,另一个 ID ... 检查这个:jsfiddle.net/gvda0myo .. 更改顺序修复问题,因为考虑的是第一个没有显示的问题:无。您有义务使用不同的 ID 或只保留一个渐变,您放入可见的 SVG 中
  • @TemaniAfif 这不会使 svg 非常有用,但我无法重新订购移动部分和内容部分
  • 在这种情况下,在另一个 SVG 中定义渐变,在该 SVG 中您将只有 defs,并使该 svg 的高度和宽度为 0(无显示:无).. 使用的渐变必须在 SVG 内不显示:无

标签: html css svg


【解决方案1】:

为渐变考虑不同的 ID:

.mobile {
  display: none;
}

.content-svg {
  border: 1px solid black;
}
<div class="desktop">
  <!-- stuff here -->
</div>

<div class="mobile">
  <svg class="mobile-svg" height="150" width="400">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

<div class="content">
  <svg class="content-svg" height="150" width="400">
  <defs>
    <linearGradient id="grad2" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad2)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

或者如果在两个 SVG 中使用相同的渐变,则只考虑一个渐变(相关:Gradients hidden using SVG symbols

.mobile {
  display: none;
}

.content-svg {
  border: 1px solid black;
}
<div class="desktop">
  <!-- stuff here -->
</div>

  <svg  height="0" width="0">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  </svg>
<div class="mobile">
  <svg class="mobile-svg" height="150" width="400">
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

<div class="content">
  <svg class="content-svg" height="150" width="400">
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

重复相同的 ID 是无效的,只有第一个会被考虑用于两个渐变,因为第一个有 display:none 它不会呈现。

更改顺序将使您的代码正常工作,因为第一个将不再有 display:none

.mobile {
  display: none;
}

.content-svg {
  border: 1px solid black;
}
<div class="desktop">
  <!-- stuff here -->
</div>
<div class="content">
  <svg class="content-svg" height="150" width="400">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>
<div class="mobile">
  <svg class="mobile-svg" height="150" width="400">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg>
</div>

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2017-07-22
    • 2016-06-19
    • 2020-04-03
    • 2015-04-01
    • 1970-01-01
    相关资源
    最近更新 更多