The box-shadow property is a shorthand property used to apply a set values for horizontal and vertical displacement, blur amount, spread, color and position to an element. Multiple box-shadows can be declared, each set separated by a comma. In the order in which they should be declared, the properties include:
- h-shadow: sets the horizontal displacement of the shadow
positive values create it on the right
negative on the left - v-shadow: sets the vertical displacement of the shadow
positive values create it on the bottom
negative on the top - blur: sets the depth of the blur, negative values not allowed
- spread: sets the spread value, a little bit like a visual zoom, negative values allowed
- color: 1 color value declared as named color, Hex or RGB, with or without alpha, values
- inset: keywords “inset” or “outset” (default is outset) are used to define if the shadow shall be on the outside or inside of the box
outset creates the effect outside the box, i.e. a drop shadow effect
inset creates the effect inside the box, i.e. a inner shadow effect
Default value: none, when undeclared;
when box-shadow is declared, undeclared values default to: 0 0 0 0 / same as “color” value / outset
Inherited: no
Can be applied to: all HTML elements
Syntax: box-shadow: h-shadow v-shadow blur spread color inset;
The box shadow property also echoes the presence of rounded corners, i.e. if a box uses border-radius, the same radius will be applied to the shadow.
Tip: If using box-shadow on various background colors, the best results are obtained using alphas, e.g. rgba, this allows the shadow color to blend in rather than appearing as a solid gradient overlay.
Examples of box-shadow usage:
Box Shadow Top Right
Box Shadow effect, direction top right; negative vertical value (-3px) parses shadow above top edge of box; omitted spread value defaults to 0.
Code:
.shadow_two { background-color:#B53800; -moz-box-shadow: 3px -3px 3px #79726B; -webkit-box-shadow: 3px -3px 3px #79726B; -o-box-shadow: 3px -3px 3px #79726B; -ms-box-shadow: 3px -3px 3px #79726B; box-shadow: 3px -3px 3px #79726B; padding:7px; }
Box Shadow Top Left
Box Shadow effect, direction top left; negative vertical value (-3px) parses shadow above top edge of box; negative horizontal value (-3px) parses shadow to left side of box; omitted spread value defaults to 0.
Code:
.shadow_three { background-color:#B53800; -moz-box-shadow: -3px -3px 3px #79726B; -webkit-box-shadow: -3px -3px 3px #79726B; -o-box-shadow: -3px -3px 3px #79726B; -ms-box-shadow: -3px -3px 3px #79726B; box-shadow: -3px -3px 3px #79726B; padding:7px; }
Box Shadow Bottom Left with Rounded Corners
Box Shadow effect, direction bottom left. When using rounded corners (border-radius), notice that the shadows are also rounded.
Code:
.shadow_four { background-color:#B53800; -moz-box-shadow: -3px 3px 3px #79726B; -webkit-box-shadow: -3px 3px 3px #79726B; -o-box-shadow: -3px 3px 3px #79726B; -ms-box-shadow: -3px 3px 3px #79726B; box-shadow: -3px 3px 3px #79726B; -moz-border-radius:5px; -webkit-border-radius:5px; -o-border-radius:5px; -ms-border-radius:5px; border-radius:5px; padding:7px; }
Subtle Box Shadow Bottom Right
A standard, subtle shadow using a mellower color for the shadow for when the shadow needs to be seen but not heard.
Code:
.shadow_five { background-color:#DFDAD5; -moz-border-radius:10px; -webkit-border-radius:10px; -o-border-radius:10px; -ms-border-radius:10px; border-radius:10px; -moz-box-shadow: 3px 3px 5px #B1A59A; -webkit-box-shadow: 3px 3px 5px #B1A59A; -o-box-shadow: 3px 3px 5px #B1A59A; -ms-box-shadow: 3px 3px 5px #B1A59A; box-shadow: 3px 3px 5px #B1A59A; padding:10px; }
Box Shadow as Outline Blur
A very subtle outer shadow used as an outline blur. Horizontal and vertical displacements are set to 0, leaving the blur alone to create the shadowing; omitted spread value defaults to 0.
Code:
.shadow_six { background-color:#DFDAD5; -moz-border-radius:10px; -webkit-border-radius:10px; -o-border-radius:10px; -ms-border-radius:10px; border-radius:10px; -moz-box-shadow: 0 0 5px #ddd; -webkit-box-shadow: 0 0 5px #ddd; -o-box-shadow: 0 0 5px #ddd; -ms-box-shadow: 0 0 5px #ddd; box-shadow: 0 0 5px #ddd; padding:10px; }
Box Shadow Inset for Inner Shadow
Use the inset value to create a shadow on the inner side of a box.
Code:
.shadow_seven { background-color:#DFDAD5; -moz-border-radius:10px; -webkit-border-radius:10px; -o-border-radius:10px; -ms-border-radius:10px; border-radius:10px; -moz-box-shadow: 3px 3px 25px #685447 inset; -webkit-box-shadow: 3px 3px 25px #685447 inset; -o-box-shadow: 3px 3px 25px #685447 inset; -ms-box-shadow: 3px 3px 25px #685447 inset; box-shadow: 3px 3px 25px #685447 inset; padding:10px; }
Outer Glow and Inner Shadow
Take advantage of box-shadow by using it to create inner shadow on the outer div and an outer glow/inner shadow on the nested div.
Nobody ever stopped him in the street to say, with gladsome looks, “My dear Scrooge, how are you? When will you come to see me?” No beggars implored him to bestow a trifle, no children asked him what it was o’clock, no man or woman ever once in all his life inquired the way to such and such a place, of Scrooge. Even the blind men’s dogs appeared to know him; and, when they saw him coming on, would tug their owners into doorways and up courts; and then would wag their tails as though they said, “No eye at all is better than an evil eye, dark master!” …
Code:
The grey outer div has a black inner shadow using the css:
.glow_one_one { background-color: #5F5E5D; padding: 15px; -moz-box-shadow: 0 0 30px #333 inset; -webkit-box-shadow: 0 0 30px #333 inset; -o-box-shadow: 0 0 30px #333 inset; -ms-box-shadow: 0 0 30px #333 inset; box-shadow: 0 0 30px #333 inset; }
The inner box uses 2 sets of box-shadow values separated by a comma. The first creates an outer glow, the second has the inset value declared to create an inner shadow.
.glow_one_two { background: #fff url(img/db_border10.png); padding: 10px; border: solid 1px #5F5E5D; -moz-box-shadow: 0 0 10px #DFDAD5, 0 0 30px #333 inset; -webkit-box-shadow: 0 0 10px #DFDAD5, 0 0 30px #333 inset; -o-box-shadow: 0 0 10px #DFDAD5, 0 0 30px #333 inset; -ms-box-shadow: 0 0 10px #DFDAD5, 0 0 30px #333 inset; box-shadow: 0 0 10px #DFDAD5, 0 0 30px #333 inset; }
Double Shadow Effect Using Inset, Outset
The example divs on this page are all set to 10px margin to allow the shadow effect room. Because this next example has a spread of 25px you can notice that the left side of the shadow is cropped down which doesn’t look nice, but I’ve left it in so you can see it. This needs to be allowed for when planning shadow effects.
Code:
.shadow_double { background: #DFDAD5 url(img/db_border10.png); padding: 10px; border: solid 15px #5F5E5D; -moz-border-radius:10px; -webkit-border-radius:10px; -o-border-radius:10px; -ms-border-radius:10px; border-radius: 10px; -moz-box-shadow: 3px 3px 25px #685447, 0 0 10px #333 inset; -webkit-box-shadow: 3px 3px 25px #685447, 0 0 10px #333 inset; -o-box-shadow: 3px 3px 25px #685447, 0 0 10px #333 inset; -ms-box-shadow: 3px 3px 25px #685447, 0 0 10px #333 inset; box-shadow: 3px 3px 25px #685447, 0 0 10px #333 inset; }