Inset box-shadow beneath content

henryaaron picture henryaaron · Nov 2, 2012 · Viewed 19.7k times · Source

I don't understand why, but an inset box shadow is beneath my content.

Here's an example:

http://jsfiddle.net/MAckM/

You see the a is on top of the box shadow.

How can I get the box shadow to be on top of the a?

Answer

fncombo picture fncombo · Nov 2, 2012

You need to make a new element inside the div, with absolute positioning and height and width of 100%, then give that element the box shadow.

div {
    height:300px;
    color:red;
    position:relative;
}

div div {
    box-shadow:inset 0 0 10px black;
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}
<div>
    <div></div>
    a
</div>

Edit:

Alternatively, you can use a pseudo element:

div {
    height:300px;
    color:red;
    position:relative;
}

div:before {
    content:'';
    display:block;
    box-shadow:inset 0 0 10px black;
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}​
<div>
    a
</div>​