Drop shadow with css

Drop-Shadow: The Underrated CSS Filter

If you’re familiar with CSS, you probably know all about the box-shadow property. But did you know there is a CSS filter, drop-shadow , that does something similar? Like box-shadow , we can pass in values for x-offset, y-offset, blur radius and colour:

.my-element  
filter: drop-shadow(0 0.2rem 0.25rem rgba(0, 0, 0, 0.2));
>

Unlike box-shadow , it doesn’t take a spread parameter (more on that later).

Why is drop-shadow useful?

If we have box-shadow , why do we need drop-shadow at all?

Non-rectangular shapes

Using drop-shadow allows us to add a shadow to an element that doesn’t correspond to its bounding box, but instead uses the element’s alpha mask. We could add a drop shadow to a transparent PNG or SVG logo, for instance.

img  
filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));
>

We can compare the effect of box-shadow versus drop-shadow :

A pink cat logo with box-shadow on the left and the same logo with drop-shadow on the right

Using box-shadow gives us a rectangular shadow, even though the element has no background, while drop-shadow creates a shadow of the non-transparent parts of the image.

This will work whether the image is inline in the HTML (either as an inline SVG, or in tag), or a CSS background image. That means we could also add a shadow to a gradient background. These shapes are created with background gradients, with the drop-shadow filter applied:

Clipped elements

If we clip or mask an element using clip-path or mask-image , any box-shadow we add will be clipped too — so it will be invisible if it’s outside of the clipped area.

But we can create a drop shadow on the clipped element by applying the drop-shadow filter on the element’s parent. Pretty cool!

.parent-element  
filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));
>

.clipped-element
clip-path: polygon(0 0, 50% 0, 100% 50%, 50% 100%, 0 100%, , 50% 50%))
>

Pink chevron shape with blue drop-shadow

Grouped elements

On occasion I’ve needed to build components made up of overlapping elements, which itself needs to cast a shadow.

If we add a box-shadow to the whole component, we’ll be left with strange empty spaces:

Two call-to-action components with box-shadow

If we add a box-shadow to each element individually, then each one will cast its own shadow, which might not be the desired effect. We’d need to employ some clever CSS to hide those shadows where elements overlap.

Two call-to-action components with box-shadow

But by using drop-shadow on the whole component, we get the shadow exactly where we want it, without resorting to hacks:

Two call-to-action components with drop-shadow

Multiple drop shadows

Here’s a fun thing: You can use multiple drop shadows for some pretty cool effects! Check out the following demo.

(Side note: transitioning and animating CSS filters isn’t particularly performant, and it’s probably best to avoid animating this many filters at once in real projects. This one’s just for fun though!)

Limitations

As mentioned above, drop-shadow doesn’t include the spread parameter. This means we can’t currently use it to create an outline effect, which I think would be really useful. For example, if it was supported, we could use drop-shadow to create an outline on a gradient background, in the way we can with box-shadow on other elements.

Gotchas

drop-shadow doesn’t render the exact same shadow effect as box-shadow , even when given the same parameters. box-shadow tends to give a darker, heavier shadow than drop-shadow when the same values are used. I suspect this is something to do with CSS filters being based on SVG filter primitives. Whatever the case, you’ll likely need to compensate for the difference by adjusting your drop-shadow values somewhat.

If you’re interested in further reading, Ana Tudor pointed me to this article on how blur radius is calculated.

Browser support

CSS filters (including drop-shadow ) are supported in all modern browsers. I tend to use it as progressive enhancement, without the need for a workaround for older browsers, as it isn’t normally something that would affect the user experience in any significant way. But if you do need to provide alternative styling for older browsers, you could do so using a feature query, with a box-shadow fallback:

.my-element > *  
box-shadow: 0 0.2rem 0.25rem rgba(0, 0, 0, 0.2);
>

@supports (filter: drop-shadow(0 0.2rem 0.25rem rgba(0, 0, 0, 0.2)))
.my-element
filter: drop-shadow(0 0.2rem 0.25rem rgba(0, 0, 0, 0.2));
>

.my-element > *
box-shadow: none;
>
>

Conclusion

Despite having excellent support, the drop-shadow filter is highly under-utilised. I hope this article highlights some cases where it could save you hacking around with box-shadow – maybe you could use it in your next project!

Источник

How to add a Drop Shadow to an Image with CSS?

Javascript Course - Mastering the Fundamentals

Drop Shadow in CSS is used to add a drop shadow effect to the input image. It produces a nice 3-D effect on the image or the other objects. It adds a cool effect to the objects and increases the website’s aesthetics.

CSS Drop Shadow Effect

In this article, we will learn how to add a drop shadow to images and other objects.

Drop Shadow CSS Syntax

Let’s understand the syntax of drop-shadow() in CSS. The drop-shadow() in CSS uses the parameter of type shadow having values like offset-x, offset-y, blur-radius, color.

Parameters of drop-shadow()

Let’s understand the parameters of drop-shadow() in CSS. drop-shadow() takes the following parameters.

  • offset-x: It is the horizontal distance that determines the length of the shadow from the object. Shadow depends on the following values:
    • Positive value: positive value places the shadow to the right of the object by the given length.
    • Negative value: negative value places the shadow to the left of the object by the given length.
    • 0: Shadow is placed directly behind the object.
    • Positive value: positive value places the shadow below the object by the given length.
    • Negative value: negative value places the shadow above the object by the given length.
    • 0: Shadow is placed directly behind the object.
    • The larger the value passed, the more the shadow will be blurred.
    • The default value is 0, which results in unblurred edges of the shadow.

    Basic Example

    Let us take a look at a basic example of how the drop-shadow() functions in CSS.

    In this example, we are simply adding a drop shadow to a div using this function.

    drop shadow() function in CSS

    Here in this example, we have simply added the drop shadow to an HTML div element using this function.

    How to Add a Drop Shadow to an Image With CSS?

    We can simply use the drop-shadow() function to add a drop shadow to an image. Let us take a look at how we can do it.

    Example: Adding Drop Shadow to an Image

    In this example, we are simply adding a drop shadow to an image using this function.

    Drop shadow code

    Here, we have added a drop shadow to the image using the drop-shadow() function

    Explaining the Drop Shadow Code

    In the above-used code, we have passed the following values to the drop-shadow() function:

    • First value of -10px is the horizontal distance that determines the length of the shadow from the object. It is a negative value that places the shadow to the left of the object by the given length.
    • Second value of -10px is the vertical distance that determines the length of the shadow from the object. It is a negative value that places the shadow above the object by the given length.
    • Third value of 10px specifies the shadow’s blur radius.
    • Fourth parameter sets the color of the shadow.

    How to Remove White Space Under images

    If the image has a white space between the image and the drop shadow, then it can be because the images are inline element and it should be corrected by treating them as block element .

    So, we need to change the display of the image as a block from inline:

    Example:

    Output:

    We got the red-colored drop-shadow to the image.

    drop shadow to an image

    How to Add a Drop Shadow to a Text box

    We can also add a drop shadow to a text box. For that, we will be using the box-shadow property for adding the shadow to the border of the text box.

    Example: Adding a drop shadow to a text Box

    Output:

    Here, we got the green coloured drop-shadow to the text-box.

    Example of Drop Shadow in CSS

    More Examples:

    Example: How to Add Drop Shadow to the Text

    We can add a drop shadow to some text so as to get a cool shadow effect on the text. For that, simply add the text in a div and give some class to that div and use the drop-shadow() function to give the shadow effect to that text.

    Example: Adding Drop Shadow to a Text Box

    Output:

    We got a cool shadow effect on some text using the drop-shadow() function.

    Drop-shadow() function

    Limitations and Differences of Drop Shadow

    Limitations:

    • Drop-shadow() is not compatible with the spread parameter; hence it does not swell the shadow as the box-shadow does.
    • You cannot create an outline effect using this function.
    • Drop-shadow() doesn’t support the inset shadows
    • drop-shadow() doesn’t work well with some web browsers, so it can be limited to some browsers.

    Differences Between drop-shadow and box-shadow in CSS?

    We have seen that drop-shadow and box-shadow are almost of the same use, but there are certain differences between both them, whether it be browser support or the parameter values.

    Both can be used in certain situations according to the advantages of each one. Let’s see the major differences between the both:

    Parameters drop-shadow box-shadow
    Spread Parameter Support No Yes
    Browser Support Doesn’t support old browsers Supports all the Browsers
    inset shadows Doesn’t support Supports
    Performance Hardware accleration increases the performance Not Hardware accelerated
    Blur Radius Support Yes Yes

    Conclusion

Оцените статью