Comparison of CSS shadow effects: drop-Shadow and box-Shadow

Comparison of CSS shadow effects: drop-Shadow and box-Shadow

Drop-shadow and box-shadow are both CSS properties for shadow effects (halo effects). The biggest difference between the two is that box-shadow can only create rectangular shadows, while drop-shadow can create shadows of exactly the same shape as the opaque area of ​​the object. Here are the usage of two CSS properties:

.drop-shadow {
    -webkit-filter: drop-shadow(12px 12px 7px rgba(0, 0, 0, 0.7));
    filter: drop-shadow(12px 12px 7px rgba(0, 0, 0, 0.7))
}

.box-shadow {
    box-shadow: 12px 12px 7px rgba(0, 0, 0, 0.7);
}

Because both are shadow effects (halo effects), the parameters (values) that can be set for the two are almost the same: Taking the example above, all the parameter values ​​represent from left to right: horizontal offset, vertical offset, shadow blur distance, shadow color.

Next, we will further compare drop-shadow and box-shadow for you

Border and Transform Effects

The shadows of drop-shadow and box-shadow can both reflect the rounded corners and deformation effects of the border. The difference is: drop-shadow reflects the shape of the actual border, a solid frame has a solid shadow, and a dotted frame has a dotted shadow; box-shadow treats the border and the content inside as a complete square and creates a shadow of the entire square, while the border style will be ignored and it is directly treated as a solid frame.

.box {
    border: 5px solid #262b57;
    width: 120px;
    height: 120px;
    border-radius: 10px;
    transform: rotate(15deg);
    font-size: 40px;
    text-align: center;
    line-height: 120px;
}

.dashed {
    border-style: dashed;
}

Background and transparency

If the box has a set color (not transparent), the shadow effects of drop-shadow and box-shadow will look similar. What if the background of the block is semi-transparent? We can see from the picture that the color around the shadow is darker and the color in the middle is lighter, so we can infer that transparency will affect drop-shadow but not box-shadow.

.bk {
    background-color: #ffcc66;
}

.bk-alpha {
    background-color: rgba(255, 204, 102, 0.3);
}

Image border

From the example, we know that drop-shadow can reflect the irregular shape of image-border, while box-shadow treats the border directly as a solid frame and ignores the shape of the border image. The owl in the picture is a transparent PNG file. The drop-shadow not only reflects the shape of the border image, but also the shape of the owl inside the border. The box-shadow adheres to the consistent principle and treats the border and the picture as a complete square.

.frame {
    width: 286px;
    height: 240px;
    -moz-border-image: url(frame_green_.png) 25 25 repeat;
    -webkit-border-image: url(frame_green_.png) 25 25 repeat;
    border-width: 25px;
    border-image: url(frame_green_.png) 25 25 repeat;
    border-color: #79b218;
    border-style: inset;
    border-width: 25px;
    box-sizing: border-box;
    display: block;
    margin: 10px;
}

Pseudo-elements

The pseudo-element drop-shadow can reflect the shape of the pseudo-element, while the box-shadow will ignore the pseudo-element.

.addition {
    width: 100px;
    height: 100px;
    background-color: #ffcc66;
    margin: 10px 60px;
    position: relative;
    display: inline-block;
}

.addition:before {
    width: 50px;
    height: 50px;
    background-color: #ff8833;
    content: '';
    display: block;
    position: absolute;
    left: 0;
    top: 50%;
    margin-left: -40px;
    transform: rotate(45deg);
    margin-top: -10px;
}

.addition:after {
    width: 60px;
    height: 60px;
    background-color: #ff8833;
    margin: 10px;
    content: '';
    display: block;
    transform: rotate(20deg);
    transform: skew(20deg, 20deg);
    top: 5px;
    right: -40px;
    position: absolute;
}

Small blocks within a block

The shadow of drop-shadow can reflect the shape of all elements in the block, while box-shadow directly reflects the rectangular shadow of the block.

.square {
    width: 50px;
    height: 50px;
    display: inline-block;
    background-color: #ffcc66;
    margin: 20px;
}
.circle {
    width: 50px;
    height: 50px;
    display: inline-block;
    border-radius: 50%;
    background-color: #ff8833;
    margin: 20px;
}
<div class="demo-wrap">
    <div class="drop-shadow">
        <span class="square"></span>
        <span class="circle"></span>
        <p>drop-shadow</p>
    </div>
    <div class="box-shadow">
        <span class="square"></span>
        <span class="circle"></span>
        <p>box-shadow</p>
    </div>
</div>

Differences between drop-shadow and box-shadow

Drop-shadow does not have the internal border (inset shadow) and distance (spread) characteristics. As far as support is concerned, IE currently does not support the drop-shadow property; while all browsers generally support box-shadow.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Docker-compose creates a bridge, adds a subnet, and deletes a network card

>>:  Three steps to solve the IE address bar ICON display problem

Blog    

Recommend

Virtual Box tutorial diagram of duplicating virtual machines

After getting used to VM, switching to BOX is a l...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

How to install redis5.0.3 in docker

1. Pull the official 5.0.3 image [root@localhost ...

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape Escape means the original semantics ...

Two ways to enable firewall in Linux service

There are two ways: 1. Service method Check the f...

Implementation of Docker deployment of ElasticSearch and ElasticSearch-Head

This article mainly explains how to deploy Elasti...

Vue3 uses axios interceptor to print front-end logs

Table of contents 1. Introduction 2. Use axios in...

Let's learn about JavaScript object-oriented

Table of contents JavaScript prototype chain Obje...

Summary of various postures of MySQL privilege escalation

Table of contents 1. Write Webshell into outfile ...

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

Summary of some situations when Docker container disk is full

Preface This article describes two situations I h...

Implementation of Nginx configuration https

Table of contents 1: Prepare https certificate 2:...