gpt4 book ai didi

html - 如果 svg 隐藏在单独的类中,则 SVG LinearGradient 隐藏

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:38 24 4
gpt4 key购买 nike

基本上,如果 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>

如您在上面的代码片段中所见,SVG 元素在那里,线性渐变填充被隐藏

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

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

最佳答案

要么考虑为渐变使用不同的 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>

关于html - 如果 svg 隐藏在单独的类中,则 SVG LinearGradient 隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com