Description of SVG filters
All SVG filters are defined inside a <defs> element. The <defs> element is a short form of definitions. It contains a definition of specific elements like filters. The <filter> element defines an SVG filter. This element has an id attribute (required) identifying the filter.
To create drop shadows, use the <feOffset> element. You will need to take an SVG graphic and move it in the xy plane.
Example of the SVG <feOffset> element:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<svg width="150" height="150">
<defs>
<filter id="filter" x="0" y="0" width="150%" height="150%">
<feOffset result="offOut" in="SourceGraphic" dx="30" dy="30" />
<feBlend in="SourceGraphic" in2="offOut" mode="normal" />
</filter>
</defs>
<rect width="110" height="110" stroke="purple" stroke-width="5" fill="pink"
filter="url(#filter)" />
Sorry, your browser doesn't support inline SVG.
</svg>
</body>
</html>
In the above-mentioned example, the <filter> element’s id attribute specifies a unique name for the filter, and the <rect> element’s filter attribute links the element to the "filter" filter.
Example of the SVG <feGaussianBlur> element:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<svg width="200" height="200">
<defs>
<filter id="filter" x="0" y="0" width="250%" height="250%">
<feOffset result="offOut" in="SourceGraphic" dx="30" dy="30" />
<feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
<feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>
</defs>
<rect width="150" height="150" stroke="coral" stroke-width="5" fill="pink"
filter="url(#filter)" />
Sorry, your browser doesn't support inline SVG.
</svg>
</body>
</html>
Here, it is possible to blur the offset image with the <feGaussianBlur> element. This element’s stdDeviation attribute specifies the amount of blur.
In the following example, the <feOffset> element’s in attribute is changed to "SourceAlpha". It uses the Alpha channel to apply blur instead of the whole RGBA pixel.
Example of coloring the shadow:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<svg height="200" width="200">
<defs>
<filter id="filter" x="0" y="0" width="150%" height="150%">
<feOffset result="offOut" in="SourceAlpha" dx="15" dy="15" />
<feGaussianBlur result="blurOut" in="offOut" stdDeviation="8" />
<feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>
</defs>
<rect width="120" height="120" stroke="purple" stroke-width="5" fill="pink"
filter="url(#filter)" />
Sorry, your browser doesn't support inline SVG.
</svg>
</body>
</html>
To transform the colors in the offset image, use the <feColorMatrix> element.
Example of the<feColorMatrix> element:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<svg height="200" width="200">
<defs>
<filter id="filter" x="0" y="0" width="150%" height="150%">
<feOffset result="offOut" in="SourceGraphic" dx="25" dy="25" />
<feColorMatrix result="matrixOut" in="offOut" type="matrix"
values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" />
<feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="9" />
<feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>
</defs>
<rect width="150" height="150" stroke="purple" stroke-width="5" fill="lightblue"
filter="url(#filter)" />
Sorry, your browser doesn't support inline SVG.
</svg>
</body>
</html>
Practice Your Knowledge
Quiz Time: Test Your Skills!
Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.