使用 CSS 的 SVG 渐变 [英] SVG gradient using CSS

查看:119
本文介绍了使用 CSS 的 SVG 渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将渐变应用于 SVG rect 元素.

I'm trying to get a gradient applied to an SVG rect element.

目前,我正在使用 fill 属性.在我的 CSS 文件中:

Currently, I'm using the fill attribute. In my CSS file:

rect {
    cursor: pointer;
    shape-rendering: crispEdges;
    fill: #a71a2e;
}

并且 rect 元素在浏览器中查看时具有正确的填充颜色.

And the rect element has the correct fill color when viewed in the browser.

但是,我想知道是否可以对这个元素应用线性渐变?

However, I'd like to know if I can apply a linear gradient to this element?

推荐答案

只需在 CSS 中使用您将在 fill 属性中使用的任何内容.当然,这需要您在 SVG 中的某处定义线性渐变.

Just use in the CSS whatever you would use in a fill attribute. Of course, this requires that you have defined the linear gradient somewhere in your SVG.

这是一个完整的例子:

rect {
    cursor: pointer;
    shape-rendering: crispEdges;
    fill: url(#MyGradient);
}

<svg width="100" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg">
      <style type="text/css">
        rect{fill:url(#MyGradient)}
      </style>
      <defs>
        <linearGradient id="MyGradient">
          <stop offset="5%" stop-color="#F60" />
          <stop offset="95%" stop-color="#FF6" />
        </linearGradient>
      </defs>
      
      <rect width="100" height="50"/>
    </svg>

这篇关于使用 CSS 的 SVG 渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆