SVG LinearGradient hidden if svg is hidden in seperate class

Either consider a different ID for the gradients:

.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>

Or consider only one gradient if it’s the same used in both SVG (related: 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>

Repeating the same ID is invalid and only the first one will be considered for both gradients and since the first one has display:none it will not render.

Changing the order will make your code work because the first one will no more have 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>

Leave a Comment