gpt4 book ai didi

css - 使用 SVG 作为背景图片

转载 作者:技术小花猫 更新时间:2023-10-29 10:10:21 26 4
gpt4 key购买 nike

我似乎无法让它按预期工作。我的页面根据加载的内容改变高度,如果它需要滚动,svg 似乎没有拉伸(stretch)......

html {
height: 100%;
background-image: url(http://www.horizonchampion.eu/themes/projectbase/images/bg.svg);
background-size: 100% 100%;
-o-background-size: 100% 100%;
-webkit-background-size: 100% 100%;
background-size: cover;
}
<svg width="1024" height="800" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient fy="0.04688" fx="0.48047" r="1.11837" cy="0.04688" cx="0.48047" id="svg_2">
<stop stop-color="#ffffff" offset="0"/>
<stop stop-opacity="0" stop-color="#eaeaea" offset="1"/>
</radialGradient>
<radialGradient fy="0.04688" fx="0.48047" r="1.71429" cy="0.04688" cx="0.48047" id="svg_5">
<stop stop-color="#ffffff" offset="0"/>
<stop stop-opacity="0" stop-color="#eaeaea" offset="1"/>
</radialGradient>
</defs>
<g display="inline">
<title>Layer 1</title>
<rect fill="#eaeaea" stroke-width="0" x="0" y="0" width="1024" height="800" id="svg_1"/>
</g>
<g>
<title>Layer 2</title>
<rect id="svg_3" height="282" width="527" y="1" x="1" stroke-width="0" fill="url(#svg_2)"/>
<rect id="svg_4" height="698" width="1021.99999" y="1" x="1" stroke-width="0" fill="url(#svg_5)"/>
</g>
</svg>

仅使用 CSS3 可以做到这一点吗?我不想加载另一个 JS 库或调用...有什么想法吗?谢谢!

最佳答案

使用我的解决方案,您可以获得类似的结果:

svg background image css

这里是 bulletprof 解决方案:

您的html: <input class='calendarIcon'/>

您的SVG:我用了 fa-calendar-alt

fa-calendar-alt

(任何 IDE 都可以打开 svg 图像,如下所示)

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
<path d="M148 288h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm108-12v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm96 0v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm-96 96v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm-96 0v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm192 0v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm96-260v352c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V112c0-26.5 21.5-48 48-48h48V12c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h128V12c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h48c26.5 0 48 21.5 48 48zm-48 346V160H48v298c0 3.3 2.7 6 6 6h340c3.3 0 6-2.7 6-6z"/>
</svg>

要在 css background-image 中使用它,您必须对 svg 进行编码以寻址有效字符串。我用了this tool (名称:URL解码器—转换乱码地址)

只要你拥有了所有你需要的东西,你就会来到 css

.calendarIcon{
//your url will be something like this:
background-image: url("data:image/svg+xml,***<here place encoded svg>***");
background-repeat: no-repeat;
}

注意:这些样式不会对编码的 svg 图像产生任何影响

.{
fill: #f00; //neither this
background-color: #f00; //nor this
}

因为对图像 的所有更改都必须直接应用于其 svg 代码

<svg xmlns="" path="" fill="#f00"/></svg>

为了获得正确的位置,我复制了一些 Bootstrap 间距,我的最终 CSS 得到了下一个外观:

.calendarIcon{
background-image: url("data:image/svg+xml,%3Csvg...svg%3E");
background-repeat: no-repeat;
padding-right: calc(1.5em + 0.75rem);
background-position: center right calc(0.375em + 0.1875rem);
background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}

关于css - 使用 SVG 作为背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185434/

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