How do I get an inset CSS3 box-shadow
to render on top of its children elements?
Problem:
HTML:
Change the background-color of any children to also be RGBA (this only gets set in browsers that understand it which, conveniently, is any browser that can handle the shadow):
.chatmessage:nth-child(2n) {
background : #EEE;
background : RGBA(0, 0, 0, .066);
}
Note that the two colors (#EEE
, RGBA( 0, 0, 0, .066)
) are identical as long as the background behind them is white.
Demo since people seem to be down voting this for no reason: http://jsfiddle.net/6NrkR/
cant be done directly from css.. (it is not shadow if it goes above overlapping elements)
you would need to rework your html a bit by adding a div (or use a pseudo element as suggested by miguelcobain's answer) to overlay the shadow and your CSS to make the new div have the shadow..
#chatroom {
border: 1px solid #CCC;
height: 135px;
font-size: 0.75em;
line-height: 1.2em;
overflow: auto;
position: relative;
}
.shadow {
position: absolute;
left: 0px;
top: 0px;
right: 0px;
bottom: 0px;
-moz-box-shadow: inset 0 0px 4px rgba(0, 0, 0, .55);
-webkit-box-shadow: inset 0 0px 4px rgba(0, 0, 0, .55);
box-shadow: inset 0 0px 4px rgba(0, 0, 0, .55);
}
.chatmessage {
padding: 4px 2px;
}
.chatmessage b {
margin-right: 2px;
}
.chatmessage:nth-child(2n) {
background: #EEE;
}
<div id="chatroom">
<div class="chatmessage"><b>User 1:</b>Test</div>
<div class="chatmessage"><b>User 2:</b>Test</div>
<div class="chatmessage"><b>User 1:</b>Test</div>
<div class="chatmessage"><b>User 2:</b>Test</div>
<div class="shadow"></div>
</div>
To avoid the use of an additional element, you can use css pseudo-elements. Try this demo.
#chatroom {
position: relative;
}
#chatroom:before {
content: "";
/* Expand element */
position: absolute;
left: 0px;
top: 0px;
right: 0px;
bottom: 0px;
-moz-box-shadow: inset 0 0 8px rgba(0,0,0,.55);
-webkit-box-shadow: inset 0 0 8px rgba(0,0,0,.55);
box-shadow: inset 0 0 8px rgba(0,0,0,.55);
/* Disable click events */
pointer-events: none;
}
Basically this css creates that additional element for you. Notice the pointer-events:none
to allow click events to pass through this element.
Keep in mind that pointer-events:none
doesn't work well on some mobile devices regarding touch scrolling (clicking/taping works well). I ended up not using inset shadows at all because of this.