Mon Sep 09 2024

Understanding CSS Shadows: box-shadow vs drop-shadow

CSS33 views

When designing web elements with CSS, shadows can add depth, emphasis, and a modern aesthetic. Two primary properties used to create shadows in CSS are box-shadow and drop-shadow. You may think that both are the same at first glance, but they have distinct differences and use cases. In this article, I’ll break down how to use box-shadow and drop-shadow in CSS, their differences, and when to use each one.

1. box-shadow

The box-shadow property in CSS is used to add shadows to box-like elements, such as divs, buttons, containers, and more. It works by applying a shadow effect to the entire box including borders, padding, and content areas.

Key Features of box-shadow

  • Applies to the entire box of an element.

  • Can have multiple shadows using comma-separated values.

  • Can also create inner shadows using the inset keyword.

When to Use box-shadow

  • When you need to add shadows to regular elements (like buttons, cards, divs, etc.).

  • Useful for creating soft shadows or adding depth to UI elements.

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

2. drop-shadow

The drop-shadow property is part of CSS Filters and is used to apply shadows to the edges of an element’s visual content, including images, SVGs, and more complex shapes. Unlike box-shadow, which shadows the entire box, drop-shadow follows the contour of the element itself.

Key Features of drop-shadow

  • Works on the content (like images or SVG paths) instead of the entire box.

  • Can be more performant on complex shapes since it only applies to visible content.

  • Part of the CSS Filters module, so it can be combined with other filters like blur(), brightness().

When to Use drop-shadow

  • When you want to add shadows around images, SVG elements, or other complex shapes where box-shadow might not give the desired result.

  • Useful for images with transparency, like PNG files, where box-shadow would apply a shadow to the entire rectangular area.

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

Conclusion

Both box-shadow and drop-shadow are essential tools in CSS for web designing. But they are different from each other. While box-shadow is great for adding depth to boxes and containers, drop-shadow is good for more intricate and transparent elements like images or SVGs.

  • Use box-shadow when you want to apply shadows to boxes and borders.

  • Use drop-shadow when you need shadows to follow the outline of an image or shape.

By understanding the distinctions between the two, you can enhance the aesthetics of your web designs and add sophisticated shadow effects to different types of elements.

Understanding CSS Shadows: box-shadow vs drop-shadow

File Name: css-shadow.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      body {
        background: #212121;
      }
      section {
        width: 100%;
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      img {
        /* Box Shadow */
        box-shadow: 0 0 15px #fbf792;
        /* Drop Shadow */
        filter: drop-shadow(0 -5px 15px #fbf792);
      }
    </style>
  </head>
  <body>
    <section>
      <img src="princess.png" alt="" width="250" />
    </section>
  </body>
</html>

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.