Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Drop shadows are another design element that has eluded designers, in terms of having a pure CSS solution. Over the years, designers would use images, customized borders, sometimes they would even layer multiple elements on top of each other, just to achieve the look of drop shadows. Recent additions to CSS allow us to apply drop shadows directly on elements and text, using the box shadow and text shadow properties respectively. Now, before we get into this exercise and working on these values ourselves, I want to go over the syntax just for a moment. And you can see in our code block, right there we have an example of box-shadow.
It's got four values separated by white space, and then a color declaration after that. Now, those four values in order. The first value is the horizontal offset; the second value is vertical offset; the third value is the blur or how sharp the shadow should be; and then the fourth value is the spread, or how big the shadow is going to be. Now, as far as the text shadow goes, the text specification says that it uses the exact same syntax, but that is not exactly true. That was a recent change to the syntax.
So as you're going to see when we do text shadow, we'll only have the three values, because most browsers don't support spread value for text shadows. Okay, we have three elements that we're going to style with drop shadows. You can see them just below our scripted text. We have the headline that says Drop A Drop Shadow On Me Please, and then we have two blocks, and the first one says The key to drop shadows is subtlety! I couldn't agree more. And the second one says, My inner shadow is showing, which should give you some indication as to how we're going to style our shadows. Okay, so I've opened up the shadows.htm file.
You can find this in 08_04 directory. Just to give you an idea of the elements that we're going to be styling, I am going to scroll down into the code. You can see that you have a section of shadows. The first one has the h2. Then we have the div tag of class of outer, and we have another div tag with a class of inner, and that is who we are going to be styling. Okay. So I am going to go up a little bit and you can see we have some empty selectors here and I am just going to start on the shadows h2. We're going to do some text shadows. So I am going to do the text-shadow property, and for the text-shadow I am going to do a 2-pixel offset for horizontal.
I am going to do a 2-pixel offset for vertical. I am going to end up with a 10-pixel blur. And then I am going to give it a color of, let's say #666, which is sort of a dark-gray color. All right! So I am going to save that, go into my browser, refresh the page, and there is our drop shadow. You can see when text is involved, your blur really matters, and it can make the object look like it has sort of a glow to it. I mean there are lots of different things that you can do to it. If I were to come back and make that even darker, say I go black, save that, and test it, you can see, because of the blur, we're not really seeing black.
Now, if I go back in and change this blur value, let's say I change that to say 5 pixels and I save that and I preview it again, now the shadow is getting a little bit clearer. But because of the way that we've done the text--the text is a light gray itself--it's really making the text look really blurry. And you have to be very careful when using drop shadows on text because you don't want to negatively impact the readability. Now, one technique that a lot of people are using with drop shadows on text is to make the text look either a little beveled or a little embossed, depending upon whether it's light text on a black background or dark text on a light background.
So if I go back into my text-shadow and I change a few things, let's say I change the offset to only 1 pixel for both horizontal and vertical, and then I change the blur to 0, no blur whatsoever, and then change the color value from the current black to that dark gray again, save that, go back out and preview it, and you can see what's happening here now. We've got a slight offset to the shadow. We've got that really sort of clearly defined shadow, and it's giving us appearance of the text being a little beveled.
And you can work with those values to further refine them. And another thing that you can do is you can add multiple text shadows. You can have comma-separated lists of them, so you could offset a lighter value to the top, and you could offset a darker value on the bottom and really sort of complete that illusion. Okay. So those are text shadows. I want to go down and talk about box shadows now. So I am going to go to my outer and my inner ones, and I am just going to start off with the box-shadow property. Now, I want to go back up to text- shadows for just a moment because I forgot something that's really, really important.
Text-shadow, you'll notice I didn't use any vendor prefixes there. It's fairly well supported among modern browsers. Unfortunately, currently Internet Explorer does not support text-shadow. All right! So for box-shadow, I am going to do box- shadow and I am going to do 5 pixels for the horizontal offset, 5 pixels for the vertical offset. If you're wondering, can I use negative values to move it up and over? You absolutely can. You're not restricted to positive values here. Then I am going to do 5 pixels for the blur and then again 5 pixels for the spread.
So I am making that very even. I am going to do #333 for the color. Now, box-shadow is something that is also well supported by modern browsers, including Internet Explorer 9; however, we do have to use vendor prefixes. So I am going to copy this, and then I am going to paste it a few times, and I am going to add the -moz for box- shadow and the -webkit-box-shadow. That should cover my bases for right now. So I am going to save that, go up and preview it, and you can see our drop shadow right down here.
It's a pretty strong drop shadow. It's 5 pixels over, 5 pixels down. Fairly dark. You can see the blur and the spread. Now, if you're wondering exactly what the difference is between blur and spread, I am going to flip over to the CSS3 background module, the Backgrounds Level 3 module. They have a great illustration here in this specification. Essentially, if you look at the border box shape, the spread is applied outside of that. So it goes outside of that based on the number of pixels you are asking it to go. And then the blur radius is applied sort of midway between the spread.
So you can see midway between the spread and the edge of the shadow, that's where the blur is applied. Same thing for the inner shadows. And we're going to apply an inner shadow in just a moment. All right! So I am going to go back on to my page, I am going to move back into my code, and I am going to start modifying some things here. Now, the vendor prefixes that we have here, the Mozilla and the WebKit one, they are really for older versions of Firefox and Chrome and Safari. Their most recent versions support it without the prefix on it. So I am going to experiment with these values a little bit. just so you can see kind of what they're doing, but I am not going to modify these vendor prefixes here because there's no reason for me to do it three times just because I am testing it. All right! I am going to take the blur and I am going to move the blur down to 0, and then I am going to take the spread and I am going to crank the spread up to 10 pixels.
So I am going to save that, go back in and refresh, and you can see big difference between spread and blur. We offset the shadow, but because of the distance in the spread, it's actually popping out a little bit on the top there as well. Now you can use negative values, as I mentioned before. So if I come in, for example, I could say -5 pixels for the spread. If I save that and test it, you can see it's hiding it altogether. Now, I could come in for the blur and say, okay, give me 10 pixels on the blur, and if I save that and test that, you can see it's kind of restricting it; just the very edges of that blur are sticking out.
So the more I increase the value of the spread, I mean even if I take the spread from a negative value to 0 and save it, you can see the effect it's having on it, and it's now starting to once again poke out the other side up there as well. So if you want to restrict it to one side, you might want to think about using a bigger blur and maybe a negative spread. Okay. Now, we also have the ability to do inner shadows. So what I am going to do is I'm going to copy these particular styles and then I'm going to paste them in the inner shadow, because creating an inner shadow is actually extremely easy.
All you need to do is add the inset keyword. So right in front of each of these properties, I am just going to do inset. So it's a new keyword, and it just shows up right there in front of them. And then finally I'm going to modify these values a little bit, because inset shadows, if I left it the way it is right now, if I save this and previewed it, you can see that inset shadow is really, really harsh. That's an interesting view. It applies the shadow obviously on the inside, and it sort of creates almost a three-dimensional effect on your elements.
But again, I really believe in subtlety when it comes to things, and if you're trying to style buttons, this is a property that you definitely want to experiment with. So I am going to come in and I am going to set all of my offsets to 0. So I am just going to go ahead and do 0 for all of my offsets, and I'm going to go ahead and make all the syntax match, just because. And then what I am going to do for the blur is I am going to do a 10-pixel blur. So I am going to do a 10-pixel blur, and if I was a smart guy, I would just do it once and then copy and paste it.
And then for my spread, I'm going to do -2 pixels for the spread, and then I will just copy those values and then paste them for each one of these. And that's a little bit smarter. Okay, so again, no offsets whatsoever, and then for the blur, we've got two of them. We don't need three. For the blur, I am going to do a 10-pixel blur and for the spread, I am doing -2 pixels, which again is going to lessen the severity, if you will, of the drop shadow. So I am going to save this and then when I preview it, you can see the shadow is applied equally all the way around because we don't have an offset.
Because the spread is negative, the shadow doesn't go too far into the element, and that blur is creating a real soft edge for this. So it's a really easy way to create buttons and things like that by using the sort of inner shadows, especially if you're also using gradients for the background fill. Okay. So that's just a brief introduction into drop shadows. If you want to learn a little bit more about them, I have got some links here to the text level 3 specification, which has text-shadow and the CSS backgrounds and borders module level 3, which has box-shadow in it. Honestly, everybody has got their own really distinct opinion about drop shadows.
They are one of the more polarizing aspects of design. Period. Not just web design. Some designers love them, while others, not exactly fond of them, but as with all stylistic choices that you are making, whether you use them or not is really going to come down to personal choice and your own design aesthetics.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.