Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a background break a box-shadow inset effect?

Tags:

Im trying to achieve an inner-shadow effect on a simple box, something like: alt text http://gotinsane.com/test.jpg

where the green box is the content inside another box.

My problem is that if i give the content box any kind of background, the outer box box-shadow effect vanish!

Here an example of my problem (with markup and css), i've set the content height smaller to evidence the problem - atm i really dont care about IE*, this is just a test.

Any idea?

UPDATE

The content inside the box is a somewhat kind of slide, here an example (original problem). thirtydot's answer does the trick, but it forces me to make a little hack, changing the wrapper background in function of the content: example here (thirtydot trick).

This can be a solution, but i dont like it too much and still dont understand why the outer box shadow get behind the inner box background (color, image)

UPDATE 2

Talking about this problem on another forum, i found another way: basically, instead of use box-shadow on the wrapper, that will act as a mask, I use box-shadow and border-radius directly on the content (.step elements) However, the 'mask' effect is exactly what i was trying to accomplish, so this isnt the solution neither.

I still don't understand how and why an inner element background interfere with an outer element design, or why the shadow dropped from the outer element get behind the inner one. Could this be a css bug?

UPDATE3

Someone opened a bug on mozilla, and got this answer that clearify the 'problem':

From http://www.w3.org/TR/css3-background/#the-box-shadow :

In terms of stacking contexts and the painting order, the outer shadows of an element are drawn immediately below the background of that element, and the inner shadows of an element are drawn immediately above the background of that element (below the borders and border image, if any).

In particular, the backgrounds of children of the element would paint above the inset shadow (and in fact they paint above the borders and background of the element itself).

So the rendering is exactly what the spec calls for.

UPDATE4

Fabio A. pointed out another solution, with css3 pointer-events. Looks good and works on IE8 too ;)

like image 928
Strae Avatar asked Mar 23 '11 02:03

Strae


People also ask

Can I use box shadow inset?

To use box-shadow in Internet Explorer 9 or later, you must set border-collapse to separate . inset must be the last keyword in the declaration.

Which property is used to create a drop shadow effect?

We can add a drop shadow to any HTML element using the CSS property box-shadow .

What is blur radius in box shadow?

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.

How does box shadow work?

The box-shadow CSS property adds shadow effects around an element's frame. You can set multiple effects separated by commas. A box shadow is described by X and Y offsets relative to the element, blur and spread radius, and color.


2 Answers

Since I am having this problem too and I too don't see this behaviour being normal, I filed a bug report over at mozilla

I can reproduce the problem in Google Chrome too, though, so I wonder whether this is really a bug. But it could be.

edit:

Indeed it's not a bug, but just the way it's meant to work. So, on the basis of this information, I forked your jfiddle example and came up with this solution:

The markup now looks like this:

<div id="box">     <div id="wrapper">         <div id="box_content">             Content here         </div>         <div id="mask"></div>     </div> </div> 

The mask becomes another div, which is layered on top of the #box_content one by means of being absolutely positioned. This is the CSS:

#wrapper{     position: relative;     display: inline-block;     width: 280px;     height: 280px;     border-radius: 5px;     margin: 10px; } #mask {     position: absolute;     top: 0px; left: 0px;     width: 100%;     height: 100%;      pointer-events: none; /* to make clicks pass through */      box-shadow: 0 0 10px #000000 inset; } #box_content{     background-color: #0ef83f;     height: 100%; } 
like image 64
Fabio A. Avatar answered Sep 29 '22 02:09

Fabio A.


I'm a little confused what you're actually after. If it's not quite right, let me know :)

This is my best guess.

Live Demo

CSS:
(I added in the vendor prefix rules.)

#box {     -moz-border-radius: 5px;     border-radius: 5px;       -webkit-box-shadow: 0 0 10px #000;     -moz-box-shadow: 0 0 10px #000;     box-shadow: 0 0 10px #000;      width: 280px;     height: 280px;     padding: 10px }  #wrapper {     background-color: #0ef83f;      -moz-border-radius: 5px;     border-radius: 5px;       -webkit-box-shadow: inset 0px 0px 18px #000;     -moz-box-shadow: inset 0px 0px 18px #000;     box-shadow: inset 0px 0px 18px #000;      width: 240px;     height: 240px;     padding: 20px } 

HTML:

<div id="box">     <div id="wrapper">         Content here     </div> </div> 
like image 32
thirtydot Avatar answered Sep 29 '22 01:09

thirtydot