The Wayback Machine - https://web.archive.org/web/20240717024753/https://www.geeksforgeeks.org/how-to-use-drop-shadow-on-svg-element-using-css3/
Open In App

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

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

The ‘filter’ property allows us to apply visual effects to SVG elements. To add a drop shadow to an SVG, we can use the ‘drop-shadow()’ function. The ‘drop-shadow()’ function takes four parameters:

  • x-offset: The horizontal distance of the shadow from the element.
  • y-offset”: The vertical distance of the shadow from the element.
  • blur-radius: The amount of blurring applied to the shadow.
  • color: The color of the shadow.

Syntax:

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

 

Example 1: The below example will demonstrate the use of SVG drop shadow using CSS3:

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

In the above example, we’ve applied a drop shadow to the rectangle using the ‘filter’ property and the ‘drop-shadow()’ function. The ‘x-offset‘ and ‘y-offset‘ are both set to ‘2px’, which means the shadow will be offset by 2 pixels horizontally and vertically. The ‘blur-radius’ is set to ‘4px’, which determines the amount of blur applied to the shadow. The ‘color’ is set to ‘rgba(0, 0, 0, 0.5)’, which creates a semi-transparent black shadow. You can adjust these values to create a desired shadow effect.

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

Example 2: This is another example that will demonstrate the use of SVG drop shadow using css3:

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

In conclusion, adding a drop shadow to an SVG using CSS3 is a powerful way to enhance the appearance of your graphics.



Similar Reads

Understanding CSS Shadows: Box Shadow vs. Drop Shadow
We're going to explore CSS shadows and breaking down the key differences between "Box Shadow" and "Drop Shadow." Understanding these properties can help you create more engaging and visually appealing web designs. What is Box Shadow?Box shadow is a CSS property that allows you to add shadow effects to elements on a web page. This property allows yo
5 min read
How to create long shadow of text without using text-shadow in HTML and CSS ?
Shadows are really a nice way to give depth and 3-D look to any text. Generally, we use text-shadow to give shadow to the text but this shadow is short and to create long shadow (shadow in which text is visible like that of reflection) we have to use before selector and skew function. Approach: The approach is to use a skew to create tilted text fi
2 min read
Difference between -webkit-box-shadow &amp; box-shadow in CSS
In this article, we will find difference between 'webkit-box-shadow' &amp; 'box-shadow' in CSS. Webkit-box-shadow: The webkit-box-shadow property in CSS is used to apply a box-shadow. The webkit-box-shadow is a browser-specific implementation for WebKit browsers like Google Chrome and Safari. Syntax: webkit-box-shadow: h-offset v-offset blur; Advan
1 min read
Drop shadow for PNG image using CSS
There is a basic way to add shadow effect on images but that effect will behave like the image is square, so there is another way to do the shadow which is basically applied on PNG images. The normal shadow effect will always put a square image shadow for the image which can be squared or cannot be squared but the shadow will be always square. filt
2 min read
How to create a drop shadow effect using CSS ?
In this article, we are going to learn how to create a drop shadow effect using CSS. drop-shadow() is an inbuilt function used to create a blurred shadow in a given offset and color. A drop shadow effect adds depth and visual interest to elements on a web page by creating a shadow behind them. This effect can be achieved using various CSS propertie
3 min read
Tailwind CSS Drop Shadow
The Drop Shadow class is used to apply a filter to the image to set the shadow of the image. This class creates a blurred shadow in a given offset and color. In CSS, we do that by using the CSS drop-shadow() Function. Tailwind CSS newly added feature brightness in 2.1 version. Drop Shadow: drop-shadow-sm: This class is used to set the small shadow
2 min read
CSS drop-shadow() Function
The CSS drop-shadow() function adds a shadow effect to elements, like images, using parameters for horizontal and vertical offsets, blur radius, spread radius, and color. It enhances visual depth and prominence in web design. Syntax: drop-shadow( offset-x offset-y blur-radius spread-radius color )Parameters: This function accepts five parameters as
2 min read
How to combine background-image and gradient on the same element using CSS3 ?
Cascading Style Sheets fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. It describes how a webpage should look: it prescribes colors,
2 min read
How to use Sass Variables with CSS3 Media Queries ?
SASS Variables: SASS Variables are very simple to understand and use. SASS Variables assign a value to a name that begins with $ sign, and then just refer to that name instead of the value. Despite this, they are one of the most useful tools SASS provides. SASS Variables provide the following features: Reduce repetitionDo complex mathematicsConfigu
3 min read
How to customize a “select” HTML form element with CSS3 ?
The &lt;select&gt; tag in HTML is used to create a drop-down list. The &lt;select&gt; tag contains &lt;option&gt; tag to display the available option of drop-down list. In this article, you will learn many ways to customize "select” HTML form element with CSS 3. Note: The &lt;select&gt; tag is used in a form to receive user response. Syntax: &lt;se
4 min read
Article Tags :
three90RightbarBannerImg