Skip to main content

CSS Box Shadow

CSS Box Shadow

The box-shadow property in CSS is a powerful feature used to add shadows to HTML elements, creating depth and enhancing visual appeal. It allows developers to define shadows with precision, specifying their offset, blur, spread, and color. This article provides a comprehensive guide to using box-shadow, including its syntax, examples, and advanced techniques.


01. Syntax of box-shadow

The box-shadow property accepts multiple parameters to control the shadow's appearance:

box-shadow: horizontal-offset vertical-offset blur-radius spread-radius color;

Here’s what each parameter means:

  • Horizontal Offset: Specifies the shadow's position horizontally. Positive values move the shadow to the right, while negative values move it to the left.
  • Vertical Offset: Specifies the shadow's position vertically. Positive values move the shadow down, while negative values move it up.
  • Blur Radius: Controls the softness of the shadow's edges. A higher value creates a more blurred shadow.
  • Spread Radius: Expands or shrinks the shadow. Positive values increase the shadow's size, while negative values reduce it.
  • Color: Defines the shadow's color. It can be any valid CSS color value, such as named colors, hexadecimal, RGB, or HSL.

02. Adding Basic Box Shadows

A simple box shadow can be applied using the box-shadow property with minimal parameters. Let’s look at an example:

Example

<div style="width: 200px; height: 100px; background-color: #f0f0f0; box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);">
  Basic Box Shadow
</div>

Explanation

  • 5px: Horizontal shadow offset.
  • 5px: Vertical shadow offset.
  • 15px: Blur radius for soft edges.
  • rgba(0, 0, 0, 0.3): A semi-transparent black shadow color.

03. Inset Shadows

The inset keyword creates an inner shadow, making the element appear indented.

Example

<div style="width: 200px; height: 100px; background-color: #f0f0f0; box-shadow: inset 5px 5px 15px rgba(0, 0, 0, 0.3);">
  Inset Shadow
</div>

Explanation

Adding inset before the shadow parameters applies the shadow inside the element rather than outside.


04. Multiple Shadows

Multiple shadows can be applied to a single element by separating shadow declarations with commas.

Example

<div style="width: 200px; height: 100px; background-color: #f0f0f0; box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3), -5px -5px 10px rgba(255, 255, 255, 0.8);">
  Multiple Shadows
</div>

Explanation

  • The first shadow is a standard outer shadow.
  • The second shadow moves to the top-left and uses a light color to create a glowing effect.

05. Advanced Box Shadow Techniques

Neumorphism

Neumorphism combines light and dark shadows for a soft, 3D effect.

<div style="width: 200px; height: 100px; background-color: #e0e0e0; box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.2), -10px -10px 20px rgba(255, 255, 255, 0.7);">
  Neumorphic Design
</div>

Custom Shapes

Shadows can be customized to fit non-rectangular shapes by using clip-path in combination with box-shadow.


06. Browser Compatibility

The box-shadow property is widely supported across modern browsers. Ensure proper testing on older browser versions if targeting a broader audience.


07. Conclusion

CSS box-shadow is a versatile property that brings depth and dimension to web elements. By mastering its parameters and experimenting with techniques like inset shadows, multiple shadows, and advanced effects, you can create visually engaging designs that enhance user experience. Explore the examples provided in this article and incorporate them into your projects to unlock the full potential of box-shadow.

Comments