By Konstantin Kichinsky
Today we will start studing the art of mastering shadows with the new CSS3 modules. There are two types: box-shadow and text-shadow, defined respectively in the modules CSS3 Backgrounds and Borders and CSS3 Text.
Both box and text work in a similar manner - actually the difinition of text-shadow is written pointing to the definition of box-shadow with some exclusions. In this series, let me begin with box shadows:
First of all there is one thing you should remember: the shadow does not increase or decrease the size of the box or its border. The spec says:
shadows do not trigger scrolling or increase the size of the scrollable area.
Offset and color
To create the most basic shadow all that you need is to define the first two parameters of the rule: horizontal and vertical offset of the shadow (1.1):
A positive value moves the shadow to the right and down, and a negative one to the left and up.
By default if you didn’t specify the shadow’s color most of the popular browsers will use the text color in current context (1.2). (The spec changed in February’12, in previous edition omitted box-shadow “colors were UA-chosen colors”. So you may find some browsers with a different behavior.):
To set the color of the shadow all that you need is just to add a color value at the end (1.3):
To define the color you can use any of available in CSS3 notations: #RGB, #RRGGBB, namely, by rgb() or rgba() and hsla() functions with alpha-channel. hsla-function is very useful while building complex samples.
The third one length you can set is a blur radius — a positive value defining how much the shadow’s edge is blurred (2.1–2.3):
By default the blur-radius equals zero and the shadow’s edge is sharp.
By combining blurring with the shadow’s offsets you can archive various effects. On the sample (2.3) both offsets are equal to zero, but thanks to the blurring effect there is a shadow around the box:
The blurring algorithm is not defined by the spec itself, but it says that the blurring effect should approximate the Gaussian blur with a standard deviation equal to half of the blur radius. In other words it means that the actual shadow generated by different UAs can differ at some pixels.
The fourth length is for spread-distance to expand or contract the shadow. By default the shadow’s size equals to the box’s one, but using the spread you can change it. Also I should note here that this parameter was introduced in later spec versions, so many out of date tutorials and samples you will find in the wild web have no idea on its existence. ;)
To expand the shadow set a positive spread-distance (3.1, 3.2):
To contract — use a negative one (3.3):
Defining spread value you can think on it as a shadow scaling operation, but actual algorithm if defined by the spec in a little bit more complex way using blurring analogy and deleting transparent or opaque pixels.
On the sample above (3.4) the shadow has horizontal and vertical offsets of 6px to the left and down and is expanded for 8px from each side:
If you use rounded corners on your box, expect that the border-radius of the expanded shadow will be also scaled up (3.5):
Finally one more interesting modifier you can define is the “inset” keyword which allows you to draw an inner shadow in your box (4.1-4.4):
Note that the inner shadow is drawn only inside the box. And if you use positive (expanding) spread-distance on inner shadow it means contracting the shadow’s perimeter shape (4.2).
And the last one piece of the mosaic: you can define as much shadows for the same box as you want by writing all of them comma-separated in the same rule.
For example to create a rainbow shadow (5.1) all you need to do is to write up 7 shadows with increasing spread-distance:
Note that the shadow effects are applied front-to-back. The violet shadow one will be drawn first, on top of it will be drawn the blue one and so on… the red one will be drawn at the end on top of other shadows, and finally — the box itself with all its content.
As all the shadows are independent you can easily combine shadows with different offsets (5.2):
Or combine outer and inner shadows (5.3):
Here is an underline (or a bottom shadow) sample (5.4):
By adding some special effects on the :before and :after pseudo-classes with content you can create a slick-box described by Matt Hamm (5.5):
(To simplify the code I removed all the vendor prefixes, but while trying to reproduce it you should use all these -ms-transform, -webkit-transform and so on.)
Summing up the common syntax for box-shadow looks like this:
The last one rule in a full version means:
The blur-radius and the spread-distance can be omitted. The inset keyword switched the shadow form outer to inner.
CSS properties discussed in this article are defined in the CSS3 Backgrounds and Borders module, which is currently in the Working Draft status. Meanwhile it seems to be quite stable it still can change in details.
About the Author
Training and Certifications
All Developer Centers and Hubs