首页 >web前端 >css教程 >CSS3 可以对 SVG 元素应用阴影吗?

CSS3 可以对 SVG 元素应用阴影吗?

Patricia Arquette
Patricia Arquette原创
2025-01-02 18:32:38475浏览

Can CSS3 Apply Drop Shadows to SVG Elements?

CSS3 SVG 投影

是否可以使用 CSS3 将投影应用于 SVG?

可以,使用 CSS 过滤器属性,即受 webkit 浏览器、Firefox 34 等现代浏览器支持Edge.

语法

filter: drop-shadow(horizontal-offset vertical-offset blur-radius color);

示例

.shadow {
  filter: drop-shadow(3px 3px 2px rgba(0, 0, 0, .7));
}

您也可以在 SVG 元素中使用此内容:

<svg>
  <rect class="shadow" x="10" y="10" width="200" height="100" fill="#bada55" />
</svg>

适用于旧版浏览器的 Polyfill

对于 Firefox 等浏览器

34、IE6,可以使用这个polyfill:
// Usage:
// $(element).cssShadow({horizontal-offset, vertical-offset, blur-radius, color});

$.fn.cssShadow = function(options) {
  // CSS syntax to include the units of the values
  var css = "-webkit-filter: drop-shadow("
    + options.horizontalOffset + "px "
    + options.verticalOffset + "px "
    + options.blurRadius + "px "
    + options.color
    + ");";

  // Applying the css
  return this.css({
    'filter': css,
    '-webkit-filter': css
  });
};

以上是CSS3 可以对 SVG 元素应用阴影吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn