Why would we need to apply shadows to SVG?
- Shadows are a common design feature that can help elements, like icons, stand out. They could be persistent, or applied in different states (e.g. :hover, :focus, or :active) to indicate interaction to users.
- Shadows happen in real life, so they can be used on screens to breathe some life into your elements and add a touch of realism to a design.
Since we’re making lists, there are two primary ways we can apply shadows to an SVG:
- Using the CSS filter() property
- Using an SVG
Yes, both involve filters! And, yes, both CSS and SVG have their own types of filters. But there is some crossover between these as well. For example, a CSS filter can refer to an SVG
What you can’t use: the CSS box-shadow property. This is commonly used for shadows, but it follows the rectangular outside edge of elements, not the edges of the SVG elements like we want. Here’s Michelle Barker with a clear explanation:
If you’re using an SVG icon font, though, there is always text-shadow. That will indeed work. But let’s focus on those first two as they’re in line with a majority of use cases.
Shadows with CSS filters
The trick to applying a shadow directly to SVG via CSS filters is the drop-shadow() function :
svg { filter: drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4)); }
That will apply a shadow that starts at 3px horizontally, 5px down, with 2px of blur, and is 40% black. Here are some examples of that:
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Mobile / Tablet
Call an SVG filter inside a CSS filter
Say we have an SVG filter in the HTML:
<svg height="0" width="0"> <filter id="shadow" color-interpolation-filters="sRGB"> <fedropshadow dx="2" dy="2" stddeviation="3" flood-opacity="0.5"></fedropshadow> </filter> </svg>
We can use a CSS filter to call that SVG filter by ID instead of values we saw earlier:
svg { filter: url(#shadow); }
Now that filter is taken from the HTML and referenced in the CSS, which applies it.
Using SVG filter primitives
You might be wondering how we got that SVG
There are lots of different filter primitives in SVG. The one we’re reaching for is
So, similar to how we had something like this did this with a CSS filter:
svg { filter: drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4)); }
…we can accomplish the same with the
- dx — This shifts the position of the shadow along the x-axis.
- dy — This shifts the position of the shadow along the y-axis.
- stdDeviation — This defines the standard deviation for the drop shadow’s blur operation. There are other attributes we can use, such as the flood-color for setting the drop shadow color, and flood-opacity for setting the drop shadow’s opacity.
That example includes three
Using SVG filters
SVG filters are very powerful. We just looked at
Let’s take the SVG markup for the Twitter logo as an example :
<svg viewbox="0 0 20 20"> <path fill="#4691f6" d="M18.258,3.266c-0.693,0.405-1.46,0.698-2.277,0.857c-0.653-0.686-1.586-1.115-2.618-1.115c-1.98,0-3.586,1.581-3.586,3.53c0,0.276,0.031,0.545,0.092,0.805C6.888,7.195,4.245,5.79,2.476,3.654C2.167,4.176,1.99,4.781,1.99,5.429c0,1.224,0.633,2.305,1.596,2.938C2.999,8.349,2.445,8.19,1.961,7.925C1.96,7.94,1.96,7.954,1.96,7.97c0,1.71,1.237,3.138,2.877,3.462c-0.301,0.08-0.617,0.123-0.945,0.123c-0.23,0-0.456-0.021-0.674-0.062c0.456,1.402,1.781,2.422,3.35,2.451c-1.228,0.947-2.773,1.512-4.454,1.512c-0.291,0-0.575-0.016-0.855-0.049c1.588,1,3.473,1.586,5.498,1.586c6.598,0,10.205-5.379,10.205-10.045c0-0.153-0.003-0.305-0.01-0.456c0.7-0.499,1.308-1.12,1.789-1.827c-0.644,0.28-1.334,0.469-2.06,0.555C17.422,4.782,17.99,4.091,18.258,3.266"></path> </svg>
We’re going to need a
Here is the syntax showing an SVG filter and applying it to a source image :
<svg width="300" height="300" viewbox="0 0 300 300"> <filter> <!-- All filter effects/primitives go in here --> </filter> <g filter="url(#myfilters)"> <!-- Filter applies to everything in this group --> <path fill="..." d="..."></path> </g> </svg>
The filter element is meant to hold filter primitives as children. It is a container to a series of filter operations that are combined to create a filter effects.
These filter primitive perform a single fundamental graphical operation (e.g. blurring, moving, filling, combining, or distorting) on one or more inputs. They are like building blocks where each SVG filter can be used to in conjunction with others to create an effect.
Let’s say we define the following SVG filter with
<svg version="1.1" width="0" height="0"> <filter> <fegaussianblur stddeviation="1 0"></fegaussianblur> </filter> </svg>
When applied on an element, this filter creates a Gaussian blur that blurs the element on a 1px radius on the x-axis, but no blurring on the y-axis. Here’s the result, with and without the effect:
It is possible to use multiple primitives inside a single filter. This will create interesting effects, however, you need to make the different primitives aware of each other. Bence Szabó has a crazy cool set of patterns he created this way.
When combining multiple filter primitives, the first primitive uses the original graphic (SourceGraphic) as its graphic input. Any subsequent primitive uses the result of the filter effect before it as its input. And so on. But we can get some flexibility on that with using the in, in2 and result attributes on primitive elements. Steven Bradley has an excellent write-up on filter primitives that dates back to 2016, but still hold true today.
There are 17 primitives we can use today:
Notice the fe prefix on all of them. That stands for filter effect. Understanding SVG filters is challenging. An effect like an inset shadow requires a verbose syntax that is difficult to grasp without a thorough understanding of math and color theory. (Rob O’Leary’s “Getting Deep Into Shadows” is a good place to start.)
Rather than running down the rabbit hole of all that, we’re going to work with some pre-made filters. Fortunately, there are a lot of ready-to-use SVG filters around.
Inset shadows
To use filter effect on the Twitter logo, we need to declare it in our “SVG source document” with a unique ID for referencing in our
<filter id="inset-shadow"> <!-- Shadow offset --> <feoffset dx="0" dy="0"></feoffset> <!-- Shadow blur --> <fegaussianblur stddeviation="1" result="offset-blur"></fegaussianblur> <!-- Invert drop shadow to make an inset shadow --> <fecomposite operator="out" in="SourceGraphic" in2="offset-blur" result="inverse"></fecomposite> <!-- Cut color inside shadow --> <feflood flood-color="black" flood-opacity=".95" result="color"></feflood> <fecomposite operator="in" in="color" in2="inverse" result="shadow"></fecomposite> <!-- Placing shadow over element --> <fecomposite operator="over" in="shadow" in2="SourceGraphic"></fecomposite> </filter>
There are four different primitives in there and each one performs a different function. But, taken together, they achieving an inset shadow.
Now that we’ve created this inset shadow filter, we can apply it to our SVG. We’ve already seen how to apply it via CSS. Something like:
.filtered { filter: url(#myfilters); } /* Or apply only in certain states, like: */ svg:hover, svg:focus { filter: url(#myfilters); }
We can also apply an SVG
<svg> <!-- Apply a single filter --> <path d="..." filter="url(#myfilters)"></path> <!-- Or apply to a whole group of elements --> <g filter="url(#myfilters)"> <path d="..."></path> <path d="..."></path> </g> </svg>
More examples
Here are some more shadow examples from Oleg Solomka:
Note that the basic shadows here are probably a bit more complicated than they need to be. For example, a colored shadow can still be done with
<fedropshadow dx="-0.8" dy="-0.8" stddeviation="0" flood-color="pink" flood-opacity="0.5"></fedropshadow>
But that embossed effect is pretty great as a filter!
Also note that you might see SVG filters in SVG syntax like this:
<svg height="0" width="0" style="position: absolute; margin-left: -100%;"> <defs> <filter> <!-- ... --> </filter> <symbol> <!-- ... --> </symbol> </defs> </svg>
On the first line there, that’s saying: this SVG shouldn’t render at all — it’s just stuff that we intend to use later. The
<svg> <use xlink:href="#my-icon"></use> </svg>
SVG filters have wide support (even in Internet Explorer and Edge!) with very fast performance.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Mobile / Tablet
Wrapping things up
A final comparison:
- CSS filters are easier to use, but are much more limited. I don’t think it’s possible to add an inset shadow with the drop-shadow() function, for example.
- SVG filters are much more robust, but much more complicated as well, and require having the
somewhere in the HTML. - They both have great browser support and perform well on all modern browsers, though SVG filters have (surprisingly) the deepest browser support.
In this article, we have seen why and how to apply shadow to SVG icons with examples on each. Have you done this, but did it a different way than anything we looked at? Have you tried to do a shadow effect that you found impossible to pull off? Please share!
The above is the detailed content of Adding Shadows to SVG Icons With CSS and SVG Filters. For more information, please follow other related articles on the PHP Chinese website!

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor