html - 无法在 html 页面上将线性渐变显示为背景图像

标签 html css svg

<svg version="1.1" xmlns="http://w3.org/2000/svg">
    	<title> Background </title>
    	<text>
    		<LinearGradient id="g" x1="200%" x2="0%" y1="50%" y2="0%">
    		<stop style = "stop-color: green;" offset="0"/>
    		<stop style = "stop-color: white;" offset="1"/>
    		</LinearGradient>
    	</text>
    	<rect style = "fill: url(#g);" width = "100%" height = "100%"/>
    </svg>

此代码的输出要么是损坏的图像,要么是标题“背景”,我看不出有什么问题。

最佳答案

你有两个问题只影响独立的 SVG

  • SVG 命名空间不正确,因此该文件未被识别为 SVG 文件。您缺少 namespace 中的 www。
  • 独立的 SVG 文件区分大小写,因此我们需要编写 linearGradient

即使您将 SVG 嵌入到 html 中,这也是一个错误。

  • 你不能让 linearGradient 成为 <text> 的子元素标签。我们可以使用 <defs>反而。理论上我们可以省略 <defs>标签,尽管我认为 Safari 并不热衷于此。

这给我们留下了这个......

<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
    <title> Background </title>
    <defs>
        <linearGradient id="g" x1="200%" x2="0%" y1="50%" y2="0%">
        <stop style = "stop-color: green;" offset="0"/>
        <stop style = "stop-color: white;" offset="1"/>
        </linearGradient>
    </defs>
    <rect style = "fill: url(#g);" width = "100%" height = "100%"/>
</svg>

关于html - 无法在 html 页面上将线性渐变显示为背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52339299/

相关文章:

javascript - window.onload 事件不触发或 "how to properly initialize js project?"

html - CSS Sprite 背景和选择器

html - Angular - 将垂直滚动转换为水平滚动

css - 这个媒体查询是如何被覆盖的?

css - 将类 (fancybox) 分配给 SVG 元素

javascript - 日本日期选择器日历

html - 未显示弹出窗口

html - 将 Div 保持在行中

css - 设置 SVG Material Design 图标的背景颜色

javascript - svg:一个角色的坐标?