DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
The box-shadow
property in CSS is for putting shadows on elements (sometimes referred to as “drop shadows”, ala Photoshop/Figma).
.card {
box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);
}
That syntax is:
box-shadow: [horizontal offset] [vertical offset] [blur radius] [optional spread radius] [color];
- The horizontal offset (required) of the shadow, positive means the shadow will be on the right of the box, a negative offset will put the shadow on the left of the box.
- The vertical offset (required) of the shadow, a negative one means the box-shadow will be above the box, a positive one means the shadow will be below the box.
- The blur radius (required), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be, and the further out the shadow will extend. For instance a shadow with 5px of horizontal offset that also has a 5px blur radius will be 10px of total shadow.
- The spread radius (optional), positive values increase the size of the shadow, negative values decrease the size. Default is 0 (the shadow is same size as blur).
- Color (required) – takes any color value, like hex, named, rgba or hsla. If the color value is omitted, box shadows are drawn in the foreground color (text
color
). But be aware, older WebKit browsers (pre Chrome 20 and Safari 6) ignore the rule when color is omitted.
Using a semi-transparent color like rgba(0, 0, 0, 0.4)
is most common, and a nice effect, as it doesn’t completely/opaquely cover what it’s over, but allows what’s underneath to show through a bit, like a real shadow.
Examples
Inner Shadow
You use the inset
keyword:
.shadow {
box-shadow: inset 0 0 10px #000000;
}
Ancient History
Here’s a snippet with vendor prefixes giving as deep of browser support as easily possible:
.shadow {
-webkit-box-shadow: 3px 3px 5px 6px #ccc; /* Safari 3-4, iOS 4.0.2 - 4.2, Android 2.3+ */
-moz-box-shadow: 3px 3px 5px 6px #ccc; /* Firefox 3.5 - 3.6 */
box-shadow: 3px 3px 5px 6px #ccc; /* Opera 10.5, IE 9, Firefox 4+, Chrome 6+, iOS 5 */
}
You can even get a box shadow to work in IE 8. You need an extra element, but it’s do-able with filter
.
<div class="shadow1">
<div class="content">Box-shadowed element</div>
</div>
.shadow1 {
margin: 40px;
background-color: rgb(68,68,68); /* Needed for IEs */
-moz-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
-webkit-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30);
-ms-filter: "progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30)";
zoom: 1;
}
.shadow1 .content {
position: relative; /* This protects the inner element from being blurred */
padding: 100px;
background-color: #DDD;
}
One-Side Only
Using a negative spread radius, you can get squeeze in a box shadow and only push it off one edge of a box.
.one-edge-shadow {
box-shadow: 0 8px 6px -6px black;
}
Multiple Borders & More
You can comma separate box-shadow any many times as you like. For instance, this shows two shadows with different positions and different colors on the same element:
box-shadow:
inset 5px 5px 10px #000000,
inset -5px -5px 10px blue;
The example shows how you can use that to create multiple borders:
By applying the shadows to pseudo elements which you then manipulate, you can get some pretty fancy 3D looking box shadows:
Super smooth shadows via multiple comma-separated values:
filter: drop-shadow()
Comparison with They can do similar things! But also, wait for it, different things. This is a deep dive article on this. Perhaps the biggest thing to know is that a filter
will hug the edges of an element moreso than the rectangular box-shadow
will do (even though box-shadow does hug rounded corners).
Browser support
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
4* | 3.5* | 9 | 12 | 5* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
127 | 127 | 4 | 4.0-4.1* |
Good to see this, But this is very basic.
I was looking for something advanced.
Anyways thanks for this!
Is there anything like outset, to counteract inset value (using LESS mixin and need to fill the variable set for inset).
HTML SCSS Result
.box {
box-shadow:
0 2.8px 2.2px rgba(0, 0, 0, 0.034),
0 6.7px 5.3px rgba(0, 0, 0, 0.048),
0 12.5px 10px rgba(0, 0, 0, 0.06),
0 22.3px 17.9px rgba(0, 0, 0, 0.072),
0 41.8px 33.4px rgba(0, 0, 0, 0.086),
0 100px 80px rgba(0, 0, 0, 0.12)
;
min-height: 200px;
width: 50vw;
margin: 100px auto;
background: white;
border-radius: 5px;
}
body {
background: #EEF2F7;
}
Hi, first i must say i love you site and it has helped me many times.
one thing about box shadow i am see is that there is a difference between FF and chrome in terms of the distance.
what i mean is, when useing this code
in FF it shows a nice shadow on the bottom of the box, but in chrome it is not showing. in order to show i need to change it to this:
would love to hear what you say about this.
Thanks
i like css3 and your site :) but i hate INTERNET EXPLORER :@
Cute
Here is a test page for comparison. View this page in Chrome, IE, then FireFox (current versions).
http://www.danwenjiang.com/box_shadow_compare.php
Notice that Chrome and IE (wow!) display it correctly, while FireFox does not render the same way.
Here is the css:
td.greybutton {
background: #696059;
}
td.greybutton:hover {
background: #944F1B;
}
.unselected {
color:#fff;
}
Here is the markup:
Menu Item 1
Menu Item 2
Menu Item 3
Any ideas on how to get FireFox to play nice?
BTW – Safari seems fine too, but Opera renders similarly to FireFox.
could you post the code to make Effect 2, I cant figure it out nor understand what you meant by pseudo elements?
Ok, so I feel like a dufus lol. found where the html and css was lol. But I tried it and it doesnt do anything? I will play with it for a while maybe I am doing something wrong
Thank you very much for sharing, the effects are very cool.
In the rule for Effect 7 I don’t really understand how the code works:
Why does the subsequent skew and rotate change the shadow the way it does?
And me too…
@Chris Coyier
Would you mind leaving a suggestion/compliment on my website @leftree.webs.com? Yes,I have used the Sitebuilder,but I have a pretty well knowledge of HTML5 & CSS3 as can be demonstrated here: http://www.codecademy.com/kerpoof/codebits/NsulFX/edit
How to:Just click on the green lightbulb on the bottom left corner.Thanks!
This page is way over the edge. See above, under “The example shows how you can use that to create multiple borders”… What an incredible HTML/CSS/PRIVIEW app. Granted, div id=”init-data” is a hairy translation of the browser surface. But this is the first I’ve seen of this kind of thing. This is the stuff browser programmers look at in wonder (and pride). 10 years maybe and this inspiration surfaces in a literal DOM. So we can write this kind of content in plain English. Another good one, people! This post box has Write and Preview tabs that are also working faultlessly. We’ve all seen this site struggle. Today, you all shine. CSS-Tricks, amazing!
I’m trying to apply effect3 on my own div, but it doesn’t work,
PS : when i put my div outside of all the containers the effect works
Can you help me please!
The PlaceKitten image in your multiple borders example isn’t showing in my browsers (tested in Chrome and FF so far). However, after researching PlaceKitten, I discovered that adding a “g modifier” would display the greyscale version, and sure enough, when I inspected the image in Chrome and changed its source from http://placekitten.com/200/200 to http://placekitten.com/g/200/200, it showed up just fine. So, I presume there’s no color version of that image available.
Anyway, the article is quite useful and worthy of bookmarking.
Cheers
When combined with bootstrap, bootstrap breaks the shadow, how could it be?
Hi there! Cool snippets on your site, thank you very much!
I have an issue with the shadow. As I put this values the page loads very slow and gets stuck on chrome. Are the values to high?
body{
-moz-box-shadow: inset 0 200px 430px #000000;
-webkit-box-shadow: inset 0 200px 430px #000000;
box-shadow: inset 0 200px 430px #000000;
}
Regards, Felix
Hi Felix,
Testing your rule locally in Chrome 40.0.2214.91 m on my Windows XP Professional desktop didn’t cause any issues. Could you provide a link to the problem page? There may be a conflict of some sort with other code there because the page in which I tested it had just the HTML5 boilerplate code found at http://csswizardry.com/2011/01/the-real-html5-boilerplate/.
Cheers
Hi Wayne thank you very much for the testing and the replying. At the end I just took the shadows away. I am also happy that way; no need to look further. Here is the page: http://www.empiresofevil.tv
yes, cheers for you to!
I am using shadow effect 5 and I really like all the different shadow effects. But I cannot figure out to make my web page body a different color and keep the white color in my wrapper. When I try to do this I lose the shadow completely. I have tried adding a z-index to my wrapper but that did not change anything. Any ideas or suggestions on how to do this would be greatly appreciated.
Here is my wrapper code for the wrapper…
.wrapper {
background-color: #ffffff;
color: #000000;
margin: 0 auto;
padding: 0;
text-align: left;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.4);
}
Johnna
I had the same issue, here’s what I did:
Get the parent container with bg color.
Give it “position: relative” and “z-index: -1”
That worked for me.
its good
Multiple borders .. I really like that one
Very good, thank you
Hello people from earth, I am from another world. but I want to learn html an css =P
my question is about the effect 2, Is it possible to do that with a youtube videoplayer, Thanks in advances! xD
hi I’m using this code on my site to highlight images on hover, but some of the boxes are not showing correctly. Maybe because they are different sizes??
here’s the code I’m using,
and here’s the rest of the code… its doing exactly what i want when its a square box. If can get it to work on anysize box, that would be great
Is there an easy way to make the shadow conform to the size of the box… this is probably happening because if have a hard coded with, and when the box is a different size maybe the shadow isnt working… please hover on
weavekingdom.com where you can see the affect when you hover on the images in the grid (hover images below the slideshow and below the categories)
how to get the 4th effect on top fixed navbar ?
this is what happens is you change position from relative to fixed:
Hey Keith, you can read this article (https://fluentthemes.com/create-box-shadow-css/) which I found very helpful to learn Box-Shadow in detail. Specially the Syntax provided there is just too handy.
Tried effect 2, z-index:-1 on pseudo elements didn’t show anything, z-index:0 works but it’s on top. Maybe something about ancestors stacking context? I can’t really change the ancestors, only the element & parent :(