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.
svg .shadow { cursor:crosshair; -moz-box-shadow: -5px -5px 5px #888; -webkit-box-shadow: -5px -5px 5px #888; box-shadow: -5px -5px 5px #888; }
<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>
SVGs are SpecialOnce you have done this, your SVG will appear with a drop shadow custom-designed just for its unique characteristics: The values that we can specify for the drop-shadow filter function are nearly identical to what we would specify for box-shadow and text-shadow.
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:
/* Use -webkit- only if supporting: Chrome < 54, iOS < 9.3, Android < 4.4.4 */ .shadow { -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7)); filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7)); /* Similar syntax to box-shadow */ }
<img src="https://upload.wikimedia.org/wikipedia/commons/c/ce/Star_wars2.svg" alt="" class="shadow" width="200"> <!-- Or --> <svg class="shadow" ...> <rect x="10" y="10" width="200" height="100" fill="#bada55" /> </svg>
This approach differs from the css box-shadow
effect in that it accounts for opacity and does not apply the drop shadow effect to the box but rather to the outline of the svg element itself.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With