Open In App

How to use drop-shadow on SVG element using CSS3 ?

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Using drop-shadow on an SVG element with CSS3 means applying a shadow effect that enhances the appearance of the SVG graphic. This effect is controlled through properties like horizontal/vertical offsets, blur radius, and color, creating depth and visual impact.

SVG is used for creating images that can be scaled without losing resolution. CSS3 offers to style and animate SVG graphics. We can apply drop shadow using the 'filter' property. 

Adding drop shadow to SVG Using CSS3 filter property

The Adding drop shadow to SVG using CSS3 filter property approach applies the drop-shadow() function to SVG elements, controlling shadow properties like offsets, blur radius, and color. This enhances the SVG's appearance by creating depth and visual focus.

Syntax:

 rect {
filter: drop-shadow(2px 2px 4px rgba(0, 0, 0, 0.5));
}

Example 1: In this example we creates an SVG graphic containing a red rectangle and green text "GeeksforGeeks". The rectangle and text are placed inside a 200x200 SVG canvas without applying any drop shadow effects.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Drop shadow Using CSS3</title>
</head>

<body>
    <svg width="200" height="200">
        <rect x="20" y="20" width="100" 
            height="100" fill="#f00" />
        <text x="20" y="150" fill="green">
            GeeksforGeeks
        </text>
    </svg>
</body>

</html>

Output:

drop-shadow example

We can select the target element by using the CSS selector property.

Example 2: In this example applies a drop shadow to a green circle (<circle>) using the filter: drop-shadow() property, creating a shadow effect for visual depth.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Drop shadow Using CSS3</title>
    <style>
        #cr {
            filter: drop-shadow(4px 6px 1px rgb(5, 5, 5));
        }
    </style>
</head>

<body>
    <svg width="300" height="200">
        <circle cx="150" 
                cy="100" 
                r="80" 
                fill="green" id="cr" />
        <text x="150" y="125" 
              font-size="60" 
              text-anchor="middle" 
              fill="white">
            GFG
        </text>
    </svg>
</body>

</html>

Output: 

drop-shadow example

Next Article
Article Tags :

Similar Reads