Using CSS box-shadow – Properties and Examples

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.

Mind! I don’t mean to say that I know, of my own knowledge, what there is particularly dead about a door–nail. I might have been inclined, myself, to regard a coffin–nail as the deadest piece of ironmongery in the trade. But the wisdom of our ancestors is in the simile; and my unhallowed hands shall not disturb it, or the Country’s done for. You will, therefore, permit me to repeat, emphatically, that Marley was as dead as a door–nail. …

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.

Scrooge knew he was dead? Of course he did. How could it be otherwise? Scrooge and he were partners for I don’t know how many years. Scrooge was his sole executor, his sole administrator, his sole assign, his sole residuary legatee, his sole friend, and sole mourner. And even Scrooge was not so dreadfully cut up by the sad event, but that hewas an excellent man of business on the very day of the funeral, and solemnised it with an undoubted bargain. …

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.

The mention of Marley’s funeral brings me back to the point I started from. There is no doubt that Marley was dead. This must be distinctly understood, or nothing wonderful can come of the story I am going to relate. If we were not perfectly convinced that Hamlet’s Father died before the play began, there would be nothing more remarkable in his taking a stroll at night, in an easterly wind, upon his own ramparts, than there would be in any other middle–aged gentleman rashly turning out after dark in a breezy spot—say St. Paul’s Church–yard, for instance—literally to astonish his son’s weak mind. …

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.

Scrooge never painted out Old Marley’s name. There it stood, years afterwards, above the warehouse door: Scrooge and Marley. The firm was known as Scrooge and Marley. Sometimes people new to the business called Scrooge Scrooge, and sometimes Marley, but he answered to both names. It was all the same to him. …

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.

Oh! but he was a tight–fisted hand at the grindstone, Scrooge! a squeezing, wrenching, grasping, scraping, clutching, covetous, old sinner! Hard and sharp as flint, from which no steel had ever struck out generous fire; secret, and self–contained, and solitary as an oyster. The cold within him froze his old features, nipped his pointed nose, shrivelled his cheek, stiffened his gait; made his eyes red, his thin lips blue; and spoke out shrewdly in his grating voice. A frosty rime was on his head, and on his eyebrows, and his wiry chin. He carried his own low temperature always about with him; he iced his office in the dog–days; and didn’t thaw it one degree at Christmas. …

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.

External heat and cold had little influence on Scrooge. No warmth could warm, no wintry weather chill him. No wind that blew was bitterer than he, no falling snow was more intent upon its purpose, no pelting rain less open to entreaty. Foul weather didn’t know where to have him. The heaviest rain, and snow, and hail, and sleet could boast of the advantage over him in only one respect. They often “came down” handsomely and Scrooge never did. …

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.

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:

.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;
}

More about CSS box-shadow at W3.org »


Interesting Sponsors and Related Topics

If you liked this, maybe you'll like this too: