Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Like text-shadow, we're able to cast shadows off elements with the box-shadow property.
Quick Reference
The order of values for box-shadow is exactly like the order of values for the text-shadow property:
.wildlife {
box-shadow: 15px 15px 10px 20px rgba(0, 0, 0, .8);
}
The first value sets the horizontal offset of the shadow. The second value sets the vertical offset. The last value sets the color of the box shadow.
The optional third value defines the blur radius in a box shadow. The optional fourth value defines the spread distance of a box shadow.
Inner shadows
By default, box shadows are created as drop shadows outside of the element. To create an inner shadow, we can add the keyword value inset
:
.wildlife {
box-shadow: inset 0 0 50px 10px rgba(0, 0, 0, 1);
}
Multiple values
Just like the text-shadow
property you can use multiple box-shadow
values separated with a comma like so:
.wildlife {
box-shadow: 15px 15px 10px 20px rgba(0, 0, 0, .8),
inset 0 0 50px 10px rgba(0, 0, 0, 1);
}
-
0:00
So just like the text-shadow property,
-
0:02
we're also able to cast shadows off elements with the box-shadow property.
-
0:07
So let's see how a box-shadow will look when applied to the wildlife content
-
0:10
container in our design.
-
0:12
So, back in our style sheet,
-
0:14
right under the Layout Styles, let's scroll down to the wildlife rule.
-
0:19
And we're going to add a box-shadow property by typing box-shadow.
-
0:26
So the order of values for box-shadow is exactly like the order of values for
-
0:31
the text-shadow property, except for one optional value we'll use later.
-
0:36
The first value in a box-shadow sets the horizontal offset of the box-shadow.
-
0:41
And like with text-shadow, a positive value moves the shadow to the right, and
-
0:45
a negative value will move the shadow to the left.
-
0:48
So, let's set our horizontal offset to 15 pixels.
-
0:53
So again, like with text-shadow, the second value sets the vertical offset, and
-
0:58
a positive value will move the shadow down,
-
1:01
while a negative value will move the box-shadow up.
-
1:04
So let's set our vertical offset to 15px as well.
-
1:08
Then we can specify the color of the box-shadow.
-
1:11
This time, we're gonna use an RGBA value of black, and
-
1:16
we're gonna set the alpha to .8.
-
1:20
So just like text-shadow, if the box-shadow color is not defined,
-
1:25
the color rule will render the shadow according to the element's color property.
-
1:29
All right, so let's save our style sheet, go back to the browser, and when we
-
1:33
refresh the page, you can see how it renders a shadow behind the wildlife div.
-
1:39
And it's offset by 15 pixels to the right, and 15 pixels towards the bottom.
-
1:44
But as we can see, the shadow seems a little flat and lacking depth.
-
1:48
So let's add a blur radius value to sort of soften it up.
-
1:53
So just like the text-shadow property,
-
1:55
the optional blur value is the third length unit in the list of values.
-
2:01
So if we give it a blur radius value of 10px, when we save and
-
2:07
take a look at it again, notice how this softens up our box-shadow.
-
2:10
>> And there now seems to be a little more depth between the div and
-
2:14
the background of the page.
-
2:16
And notice how the box-shadow has no affect on the layout of
-
2:20
surrounding elements.
-
2:21
It doesn't push any element down or up.
-
2:24
Well, that's because box-shadows, like text-shadows, have no influence on layout.
-
2:29
The shadow could be as tall or
-
2:30
as wide as we want it to be, and nothing around is affected by it.
-
2:35
So there's one more optional box-shadow value that's extremely useful for
-
2:39
defining the spread distance of a box-shadow.
-
2:42
So the fourth length value in a box-shadow property is called the spread value,
-
2:47
because it moves or spreads the shadow away from an element.
-
2:51
And it does it equally in all directions.
-
2:53
So it gives us a little more control over the size of the box-shadow.
-
2:57
So if we want to spread our box shadow by 20 pixels in every direction,
-
3:02
while maintaining the 10 pixel blur radius, let's set the value to 20px.
-
3:08
And when we save our style sheet and refresh the page,
-
3:11
notice how it spreads the shadow by 20 pixels on every side of the div.
-
3:16
Now we can even start to see parts of the shadow on the top and
-
3:20
left sides of our container.
-
3:22
But we can also contract the box-shadow, depending on the spread value we define.
-
3:26
So for instance, if we go back to our box-shadow value and
-
3:31
change the spread value from 20 to negative 5.
-
3:35
When we save our style sheet and refresh the page,
-
3:38
notice how the shadow sort of shrinks and contracts by five pixels on each side.
-
3:43
Now we're only seeing the soft blur of the box-shadow.
-
3:46
And this can create some interesting shadow effects.
-
3:50
So, for example, if we go back to our box-shadow declaration, and
-
3:54
we change the x offset to 0.
-
3:59
And let's pump the blur radius up to 20px, and
-
4:03
change the spread value to negative 12.
-
4:08
When we save and refresh the browser, this sort of squeezes in the box-shadow,
-
4:14
and we get this subtle floating box effect at the bottom of the Wildlife div.
-
4:20
So finally, by default,
-
4:21
box-shadows are created as drop-shadows outside of an element.
-
4:26
But if we want to create an inner-shadow,
-
4:28
we can do that with a box-shadow by adding the keyword value, inset.
-
4:33
The inset value must be declared either first or last, in our box-shadow value.
-
4:38
Otherwise, the line will be ignored.
-
4:40
So let's go back in our box-shadow value, and
-
4:44
let's add the inset keyword as the first value.
-
4:48
And so we can really see how this works,
-
4:49
let's change the horizontal offset to 20px.
-
4:53
Let's also make the vertical offset 20px.
-
4:57
And we're gonna remove the spread value.
-
5:00
And let's make the blur value 5px.
-
5:04
And finally we'll pump the alpha up to 1.
-
5:07
So we can see the full inset shadow.
-
5:11
Okay, so when we save our CSS file and refresh the page,
-
5:16
now we're able to see that the shadow is inside the border area of the div element.
-
5:21
And it's offset from the top by 20 pixels, and over to the right by 20 pixels.
-
5:27
But what if we want our inner shadow to be even on all sides, right.
-
5:32
Well, we can do that by setting both offsets to zero, so let's do that.
-
5:38
We're going to change the vertical offset to zero, and
-
5:41
the horizontal offset to zero.
-
5:44
Then we can use the blur and spread radius values to adjust the shadow.
-
5:48
So, let's change the blur to 50.
-
5:51
So that it's really blurry, and let's widen it by ten pixels.
-
5:55
So we're gonna set the spread distance to 10px.
-
6:00
So when we save and take a look at it again, now we can see that the shadow is
-
6:04
inset evenly on all four sides of the div, which gives us a nice inner shadow effect.
-
6:10
Now, we're also able to use multiple box-shadow values,
-
6:13
just like we were able to do with text-shadow.
-
6:16
So, go ahead and play around with these values on your own to see
-
6:19
all the interesting effects you can create with box-shadows.
You need to sign up for Treehouse in order to download course files.
Sign up