SVG drop shadow using css3
Is it possible to set drop shadow for an svg element using css3 , something like
box-shadow: -5px -5px 5px #888;
-webkit-box-shadow: -5px -5px 5px #888;
I saw some remarks on creating shadow using filter effects. Is there an example of using css alone. Below is a working code where the cusor style is correctly applied, but no shadow effect. Please help me to get the shadow effect with least bit of code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<style type="text/css" media="screen">
svg .shadow { cursor:crosshair;
-moz-box-shadow: -5px -5px 5px #888;
-webkit-box-shadow: -5px -5px 5px #888;
box-shadow: -5px -5px 5px #888; }
</style>
</head>
<body>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full" viewBox="0 0 120 70">
<rect class="shadow" x="10" y="10" width="100" height="50" fill="#c66" />
</svg>
</body>
</html>
Here's an example of applying dropshadow to some svg using the 'filter' property. If you want to control the opacity of the dropshadow have a look at this example. The slope
attribute controls how much opacity to give to the dropshadow.
Relevant bits from the example:
<filter id="dropshadow" height="130%">
<feGaussianBlur in="SourceAlpha" stdDeviation="3"/> <!-- stdDeviation is how much to blur -->
<feOffset dx="2" dy="2" result="offsetblur"/> <!-- how much to offset -->
<feComponentTransfer>
<feFuncA type="linear" slope="0.5"/> <!-- slope is the opacity of the shadow -->
</feComponentTransfer>
<feMerge>
<feMergeNode/> <!-- this contains the offset blurred image -->
<feMergeNode in="SourceGraphic"/> <!-- this contains the element that the filter is applied to -->
</feMerge>
</filter>
<circle r="10" style="filter:url(#dropshadow)"/>
Box-shadow is defined to work on CSS boxes (read: rectangles), while svg is a bit more expressive than just rectangles. Read the SVG Primer to learn a bit more about what you can do with SVG filters.
Use the new CSS filter
property.
Supported by webkit browsers, Firefox 34+ and Edge.
You can use this polyfill that will support FF < 34, IE6+.
You would use it like so:
.shadow {
/* Use -webkit- only if supporting: Chrome < 54, iOS < 9.3, Android < 4.4.4 */
-webkit-filter: drop-shadow( -5px -5px 5px #000 );
filter: drop-shadow( -5px -5px 5px #000 );
}
And your html would be:
<img src="my-svg-graphic.svg" class="shadow">
<!-- Or -->
<svg class="shadow" ... >
<rect x="10" y="10" width="100" height="50" fill="#c66" />
</svg>
This approach differs from the box-shadow
effect in that it accounts for opacity and does not apply the drop shadow effect to the box but rather to the corners of the svg element itself.
You can see a live example here.
Please Note : This approach only works when the class is placed on the <svg>
element alone. You can NOT use this on an inline svg element such as <rect>
.
<!-- This will NOT work! -->
<svg><rect class="shadow" ... /></svg>
Read more about css filters on html5rocks.
因此,正如Lorenzo Polidori接受的答案的埋葬评论中所提到的那样,Chrome中最适合我的选项(我确信其他Webkit浏览器)是:
-webkit-svg-shadow: 0 0 7px #53BE12;
链接地址: http://www.djcxy.com/p/89044.html
上一篇: 如何使用DOM解析器解析xhtml忽略DOCTYPE声明
下一篇: SVG使用css3拖放阴影