HTML & CSS - Mastery / Visual Styling with CSS

Box Shadows

🔶 What is a Box Shadow?

The box-shadow property in CSS adds shadow effects around an element's box, like cards, divs, or buttons.


✅ Basic Syntax

box-shadow: offset-x offset-y blur-radius spread-radius color;

All except color are length values (in px, em, etc.).


🔹 Example 1: Basic Shadow

<body>
  <div class="box1">Box with Shadow</div>
</body>
.box1 {
  box-shadow: 5px 5px 10px gray;
}

🧠 Explanation:

  • Shadow is pushed 5px right and 5px down.

  • It blurs with a 10px softness.

  • Appears like the box is slightly raised off the page.


🔹 Example 2: Shadow with Spread

<body>
  <div class="box2">Box with Advanced Shadow</div>
</body>
.box2 {
  box-shadow: 0px 4px 10px 5px rgba(0, 0, 0, 0.3);
}

🧠 Explanation:

  • The shadow spreads outward more due to the 5px spread-radius.

  • Gives a wider and softer feel.

  • Looks like a strong glow behind the element.


🔹 Example 3: Inset Shadow (inside the box)

<body>
  <div class="box3">Box with Inset Shadow</div>
</body>
.box3 {
  box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.5);
}

🧠 Explanation:

  • Shadow is rendered inside the element.

  • Looks like the element is pressed in or hollow.

  • Great for neumorphism and modern UI designs.


🔹 Example 4: Multiple Shadows

<body>
  <div class="box4">Box with Dual Shadows</div>
</body>
.box4 {
  box-shadow: 2px 2px 5px red, -2px -2px 5px blue;
}

🧠 Explanation:

  • One red shadow to the bottom-right

  • One blue shadow to the top-left

  • Creates a glow or 3D color effect


✅ Playground


🧠 Full Code Example

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Box Shadow Example</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="box1">Box with Shadow</div>
    <div class="box2">Box with Advanced Shadow</div>
    <div class="box3">Box with Inset Shadow</div>
    <div class="box4">Box with Dual Shadows</div>
  </body>
</html>
.box1 {
  width: 200px;
  height: 100px;
  background-color: #f0f0f0;
  box-shadow: 5px 5px 10px gray;
  margin: 50px auto;
  border-radius: 6px;
}

.box2 {
  width: 200px;
  height: 100px;
  background-color: #fafafa;
  box-shadow: 0px 4px 10px 5px rgba(0, 0, 0, 0.3);
  margin: 50px auto;
  display: flex;
  font-size: 18px;
  color: #222;
  border-radius: 6px;
}

.box3 {
  width: 200px;
  height: 100px;
  background-color: #e0e0e0;
  box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.5);
  margin: 50px auto;
  font-size: 18px;
  color: #111;
  border-radius: 6px;
}

.box4 {
  width: 200px;
  height: 100px;
  background-color: #ffffff;
  box-shadow: 2px 2px 5px red, -2px -2px 5px blue;
  margin: 50px auto;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: Arial, sans-serif;
  font-size: 18px;
  color: #000;
  border-radius: 6px;
}

📊 Comparison Table

Syntax Example

Effect

5px 5px 10px gray

Basic drop shadow

0px 4px 10px 5px rgba(0,0,0,0.3)

Wider, blurred shadow

inset 2px 2px 5px rgba(0,0,0,0.5)

Inner shadow

2px 2px 5px red, -2px -2px 5px blue

Double colored shadows