CSS3 Drop Shadows

Using CSS3 Drop Shadows

With CSS3, you can add drop shadow effects to elements just like in Photoshop, without needing any images. Before CSS3, we had to use sliced images to create shadows around elements, which was quite tedious.

The next section will explain how to apply shadows to text and elements.

CSS3 box-shadow Property

The box-shadow property allows you to add shadows to element boxes. You can even apply multiple shadow effects using a comma-separated list of shadows. The basic syntax for creating a box shadow is:

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

The components of the box-shadow property are explained as follows:

  • offset-x — Specifies the horizontal offset of the shadow.
  • offset-y — Specifies the vertical offset of the shadow.
  • blur-radius — Specifies the blur radius. The larger the value, the more blurred the shadow's edge will be. Negative values are not allowed.
  • color — Specifies the color of the shadow. If the color value is omitted, it defaults to the value of the color property.

Refer to the CSS3 box-shadow property to learn about other possible values.

.box{
width: 200px;
height: 150px;
background: #ccc;
box-shadow: 5px 5px 10px #999;
}

Note: When adding the box-shadow, if the blur-radius component is not specified or is set to zero (0), the shadow's edges will be sharp.

Similarly, you can add multiple box shadows using a comma-separated list:

.box{
width: 200px;
height: 150px;
background: #000;
box-shadow: 5px 5px 10px red, 10px 10px 20px yellow;
}

CSS3 text-shadow Property

The text-shadow property allows you to add shadow effects to text. You can also add multiple shadows to text using the same format as the box-shadow property.

h1 {
text-shadow: 5px 5px 10px #666;
}
h2 {
text-shadow: 5px 5px 10px red, 10px 10px 20px yellow;
}