Skip to content Skip to sidebar Skip to footer

Svg Lineargradient Hidden If Svg Is Hidden In Seperate Class

Basically what Is happening is the linearGradient fill is hidden if the svg is hidden in a parallell element as you can see in the snippet above, the SVG element is there, the lin

Solution 1:

Either consider a different ID for the gradients:

.mobile {
  display: none;
}

.content-svg {
  border: 1px solid black;
}
<divclass="desktop"><!-- stuff here --></div><divclass="mobile"><svgclass="mobile-svg"height="150"width="400"><defs><linearGradientid="grad1"x1="0%"y1="0%"x2="100%"y2="0%"><stopoffset="0%"style="stop-color:rgb(255,255,0);stop-opacity:1" /><stopoffset="100%"style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient></defs><ellipsecx="200"cy="70"rx="85"ry="55"fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg></div><divclass="content"><svgclass="content-svg"height="150"width="400"><defs><linearGradientid="grad2"x1="0%"y1="0%"x2="100%"y2="0%"><stopoffset="0%"style="stop-color:rgb(255,255,0);stop-opacity:1" /><stopoffset="100%"style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient></defs><ellipsecx="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;
}
<divclass="desktop"><!-- stuff here --></div><svgheight="0"width="0"><defs><linearGradientid="grad1"x1="0%"y1="0%"x2="100%"y2="0%"><stopoffset="0%"style="stop-color:rgb(255,255,0);stop-opacity:1" /><stopoffset="100%"style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient></defs></svg><divclass="mobile"><svgclass="mobile-svg"height="150"width="400"><ellipsecx="200"cy="70"rx="85"ry="55"fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg></div><divclass="content"><svgclass="content-svg"height="150"width="400"><ellipsecx="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;
}
<divclass="desktop"><!-- stuff here --></div><divclass="content"><svgclass="content-svg"height="150"width="400"><defs><linearGradientid="grad1"x1="0%"y1="0%"x2="100%"y2="0%"><stopoffset="0%"style="stop-color:rgb(255,255,0);stop-opacity:1" /><stopoffset="100%"style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient></defs><ellipsecx="200"cy="70"rx="85"ry="55"fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg></div><divclass="mobile"><svgclass="mobile-svg"height="150"width="400"><defs><linearGradientid="grad1"x1="0%"y1="0%"x2="100%"y2="0%"><stopoffset="0%"style="stop-color:rgb(255,255,0);stop-opacity:1" /><stopoffset="100%"style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient></defs><ellipsecx="200"cy="70"rx="85"ry="55"fill="url(#grad1)" />
  Sorry, your browser does not support inline SVG.
  </svg></div>

Post a Comment for "Svg Lineargradient Hidden If Svg Is Hidden In Seperate Class"