How to create a Drop Shadow with CSS

Although design trends are different every year, you can count on using basic shadow effects like shadow box And drop ghost To contribute positively to the aesthetics of the website. you can use drop shadows To create fun and beautiful effects without looking like cheese.

Let’s take a closer look at CSS drop ghost Feature.

What is a CSS drop shadow?

drop ghost( ) A CSS effect that renders a shadow around the shape of a selected object. Here’s the syntax for a CSS app drop ghost.

Syntax: 
filter: drop-shadow(offset-x offset-y blur-radius color);

There is a wide range of files Filter functions Including blur()And brightness( ), And drop ghost( ).

x . offset Determines the horizontal distance and compensation y Determines the vertical distance. Note that negative values ​​put the shadow on the left (x . offset) And above (compensation y) object.

The last two parameters are optional. You can specify the shadow blur radius as the length. By default, it is set to 0. You cannot have a negative blur radius.

Shadow color is set as <اللون>. If you don’t specify a color, it follows a value of the color Feature.

When is CSS drop shadow useful?

You may already know that Box-shadow does the job quite well. So, you might think, why do we need drop ghost Absolutely? There are many cases where a file drop ghost( ) The job is a lifesaver. Let’s look at some of them:

non-rectangular shapes

unlike shadow box, you can add a file drop ghost For non-rectangular shapes. For example, we have a transparent SVG or PNG with a non-rectangular shape – a star for example. Here, adding the shadow that corresponds to the same object can be completed with either of them shadow box or drop ghost. Consider both scenarios:

programming language

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Drop-shadow</title>
</head>
<body>
<div class="star-img">
<img class="box-shadow" src="star.svg" alt="">
<img class="drop-shadow" src="star.svg" alt="">
</div>
</body>
</html>

CSS

.star-img img {
display: inline-block;
height: 15em;
width: 25em;
}
.box-shadow {
color: red;
box-shadow: 0.60em 0.60em 0.2em;
}
.drop-shadow {
filter: drop-shadow(0.60em 0.60em 0.2em);
}

production |:

Box-shadow vs. Drop Shadow on SVG

When comparing both effects, it is clear that a shadow box Gives a rectangular shadow It also doesn’t matter if the image is transparent or already has a background. On the other hand, drop ghost It allows you to create a shadow that matches the shape of the image itself.

The limiting factors are that drop ghost( ) The function accepts all parameters of the type <ظل> Except for inset Keywords and diffusion Labs.

Collected items

There are many cases where you may need to build components by nesting certain elements. If you are using files shadow box, you’ll run into the trouble of trying to cast the shadow the right way. Here’s how it works when compiling an image and a text component:

programming language

<body>
<div class="parent-container drop-shadow">
<div class="image-container box">
<img src="smiling-girl.jpg" alt="smiling girl" />
</div>
<div class="text-container box">
<h2>Live in the moment</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptas porro quo nam odit, non debitis, facilis eligendi ea labore obcaecati ut quia asperiores. Modi, corrupti commodi quisquam ex numquam incidunt.</p>
</div>
</div>
</body>

Basic CSS

body {
padding: 5em 1em;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande',
'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
h2 {
font-size: 2rem;
}
p {
font-size: 0.8rem;
}
.parent-container {
display: flex;
flex-direction: column;
height: 17rem;
width: 50em;
}
.image-container img {
width: 15em;
position: absolute;
z-index: 1;
top: 2em;
left: 1.5em;
}
.text-container {
color: rgb(255, 236, 236);
background-color: rgb(141 0 35);
width: 30rem;
padding: 3rem;
align-self: flex-end;
position: relative;
}

Now, apply shadow box And drop ghost to see the difference.

.drop-shadow {
filter: drop-shadow(0.4rem 0.4rem 0.45rem rgba(0, 0, 30, 0.5));
}
.box,
.box img {
box-shadow: 0.4rem 0.4rem 0.45rem rgba(0, 0, 30, 0.3);
}

Produce:

Drop-shadow and box-shadow on grouped items

As you can see, the shadow box It is applied to each component separately while drop ghost They both combine and apply shadow.

clipped items

You can use the track clip Crop a specific area determines the parts of the image or object that should be displayed. The drop shadow filter Allows us to create a file drop ghost On the truncated element by applying it to the origin of that element:

programming language

<body>
<div class="parent-container">
<div class="clipped-element">
</div>
</div>
</body>

CSS

.parent-container {
filter: drop-shadow(0rem 0rem 1.5rem maroon);
}
.clipped-element {
width: 50em;
height: 50em;
margin: 0 auto;
background-image: url(smiling-girl.jpg);
clip-path: circle(50%);
background-size: cover;
background-repeat: no-repeat;
}

Produce:

Apply a drop shadow to the cut elements

We cropped 50% of the image with a circular path. Therefore, the drop shadow filter It is applied only to the visible part of the image. Isn’t it wonderful?

Limitations and differences

As we discussed above, drop ghost does not support diffusion Labs. This means that creating an outline effect will not be possible with an extension drop ghost( ) It works because it kills everywhere. Also, it displays different shadow effects from shadow box And text shadow (with the same parameters). You may feel the differences between shadow box And drop ghost It boils down to the CSS Box Model. One follows him while the other does not. This is an example:

programming language

<body>
<div class="parent-container">
<p class="drop-shadow">Every MUO article will bring you one step closer.</p>
<p class="box-shadow">
Every MUO article will bring you one step closer.
</p>
<p class="text-shadow">
Every MUO article will bring you one step closer.
</p>
</div>
</body>

Basic CSS

body {
padding: 5em 1em;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande',
'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
.parent-container {
width: 72rem;
}
p {
font-size: 3em;
font-style: bold;
}

Apply shadow effects

.drop-shadow {
filter: drop-shadow(0.5em 0.5em 0.1em #555);
}
.box-shadow {
box-shadow: 0.5em 0.5em 0.1em #555;
}
.text-shadow {
text-shadow: 0.5em 0.5em 0.1em #555;
}

Produce:

Drop shadow vs square shadow vs text shadow

You can see that a file shadow box Gives a shade heavier and darker than text shadow And drop ghost. Also, there is a slight difference in the position of the shadow between text shadow And drop ghost. However, you may prefer different shade effects according to your requirements.

Browser support

The drop ghost( ) The functionality is supported in all modern browsers except for older browsers such as Internet Explorer. Although it’s not something that would seriously hamper the user experience, you can add a feature query using a file shadow box Retreat.

Try different shadow effects

popularity shadow box Quite obvious due to the many use cases. However, the drop ghost( ) A largely underutilized function. We hope you will try different shadow effects and try to implement them drop ghost in your future projects.

Pseudo-classes add a whole new range of functionality to your CSS and personal web development inventory. Learn more about it to become a more efficient and effective web developer.


CSS code
How to use CSS box-shadow: 13 tricks and examples

Cute chests look boring. Beautify them with a CSS square shadow effect!

read the following


About the author

(Visited 1 times, 1 visits today)

Related posts