Open In App

How to Set Depth of Box Shadow in CSS ?

A powerful CSS box-shadow attribute may give web designs depth and dimension. Although CSS box-shadow first appears to be a straightforward property, there are many ways to use it to produce intricate shadow effects that can improve a website’s aesthetic appeal and user experience. In this article, we will see & dip deep into CSS box-shadow and demonstrate how to make sophisticated shadow effects that can elevate your website designs. 

Basics of CSS Box Shadow: The Box-shadow facilitates the production of a shadow-like effect to the frames of an element. The multiple effects can be applied to the element’s frame which is separated by a comma. There are 5 parameters that make up the box-shadow property, which is used to cast a shadow effect around an element.



Syntax:

box-shadow: [horizontal] [vertical] [blur radius] [spread radius] [color];

Example 1: We use the following code to generate a simple box shadow effect. This will generate a box shadow 5 pixels to the right and 5 pixels below the element, with a 5-pixel blur and the color #008000.






<!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">
    <title
        The Basics of CSS Box Shadow 
    </title>
    <style>
        div {
            color: black;
            background: #eee;
            border-radius: 6px;
            box-shadow: 7px 7px 7px #008000;
        }
          
        div h1 {
            padding: 20px 20px 20px 20px;
        }
          
        div h3 {
            padding: 20px 20px 20px 20px;
        }
    </style>
</head>
  
<body>
    <div>
        <h1>Welcome To GeeksforGeeks!</h1>
    </div>
    <div>
        <h3>A Computer Science portal for geeks. </h3>
    </div>
</body>
  
</html>

Output:

The Basics of CSS Box Shadow

Creating Complex Shadow Effects: Here, we will understand how to use CSS box-shadow to create more complicated shadow effects.

Example 2: A red shadow with a blur of 10 pixels, a cyan shadow with a blur of 20 pixels, and a green with a blur of 25 pixels will be produced around the element as a result. The output is a multilayered shadow effect that gives that element more depth and dimension.




<!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">
    <title> Several Shadows </title>
    <style>     
        div {
            border-radius: 6px;
            box-shadow: 0px 0px 10px red,
                0px 0px 20px cyan,
                0px 0px 25px green;
        }
  
        div h1 {
            padding: 20px 20px 20px 20px;
        }
  
        div h3 {
            padding: 20px 20px 20px 20px;
        }
    </style>
</head>
  
<body>
    <div>
        <h1>Welcome To GeeksforGeeks!</h1>
    </div>
    <div>
        <h3>
              A Computer Science portal for geeks.
          </h3>
    </div>
</body>
</html>

Output:

Several(Multiple) Shadows

Example 3: The color #008000 will be used to produce an inset shadow that is 15 pixels wide. 




<!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">
    <title> Inset Shadows </title>
    <style>
        div {
            background: #eee;
            box-shadow: inset 0 0 15px #008000;
        }
        div h1 {
            padding: 20px 20px 20px 20px;
        }
        div h3 {
            padding: 20px 20px 20px 20px;
        }
    </style>
</head>
  
<body>
    <div>
        <h1>Welcome To GeeksforGeeks!</h1>
    </div>
    <div>
        <h3>
              A Computer Science portal for geeks. 
          </h3>
    </div>
</body>
</html>

Output:
 

Inset shadow

Example 4:




<!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">
    <title> Text Shadow </title>
    <style>
        h1 {
            text-shadow: 5px 2px 5px #008000;
         }
    </style>
</head>
  
<body>
    <h1> This is Heading </h1>
</body>
  
</html>

Output: 

Text Shadows

Advanced CSS Box Shadow Techniques: You can use more effective methods in addition to these fundamental ones to produce detailed and complex shadow effects. 

Example 5:




<!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">
    <title>Layered Inset Shadows</title>
    <style
        div {
            box-shadow: inset 0 0 15px #008000,
                inset 0 5px 5px #0984e3,
                inset 0 -5px 5px #00cec9;
        }
        div h1 {
            padding: 20px 20px 20px 20px;
        }
  
        div h3 {
            padding: 20px 20px 20px 20px;
        }
    </style>
</head>
  
<body>
    <div>
        <h1>Welcome To GeeksforGeeks!</h1>
    </div>
    <div>
        <h3>
              A Computer Science portal for geeks.
          </h3>
    </div>
</body>
</html>

Output:

Layered Inset Shadows

Example 6:




<!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">
    <title>Interactive Shadow </title>
    <style>
        div h1 {
            padding: 20px 20px 20px 20px;
        }
          
        div h3 {
            padding: 20px 20px 20px 20px;
        }
          
          
        div {
            border-radius: 10px;
            box-shadow: 0 0 10px #00b894;
            transition: box-shadow 0.5s ease-in-out;
        }
          
        div:hover {
            box-shadow: 0 0 20px #d63031;
        }
    </style>
</head>
  
<body>
    <div>
        <h1>Welcome To GeeksforGeeks!</h1>
    </div>
    <div>
        <h3>
              A Computer Science portal for geeks. 
          </h3>
    </div>
  
</body>
  
</html>

Output:

 

Conclusion: A powerful CSS box-shadow attribute may give web designs depth and dimension. Advanced techniques like multiple shadows, inset shadows, and interactive shadows allow you to build complex and complicated shadow effects that will improve your website’s aesthetic appeal and user experience. With these methods at your disposal, you can advance your web designs and produce spectacular shadow effects that will distinguish your website from the competition. 


Article Tags :