Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to layer box-shadows using z-index?

How to layer box-shadows using z-index?

Problem

codepen

html

bbb
  • a
  • b
  • c
  • d
dddd

css

#b {
  background: orange;
  box-shadow: 0 0 10px black;
  z-index: 2;
  position: relative;
}

#c {
  background: red;
  z-index: 1;
  position: relative;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  z-index: 1;
  position: relative;
}
li {
  display: inline-block;
  padding: 2px 5px;
}

.current {
  background-color: orange;
  z-index: 3;
  position: relative;
}

#d {
  box-shadow: 0 0 10px black;
  z-index: 2;
}

Take a look at the code pen. I've got the layers more or less how I want it, except I want the "b" tab to be above the box-Shadow caused by the orange div above.

To elaborate, the orange #b div should cast a shadow on the red #c div, the .current tab should appear flush with the orange #b div (have no shadow on it), and #d should not cast a shadow on #c at all.

The problem is that the z-index on .current doesn't seem to work.

Problem courtesy of: mpen

Solution

Demo: http://codepen.io/anon/pen/vLgKC

For more info on z-index and stacking context and the priorities please see my answer here.

The above, combined with the inset for box-shadow

inset

If not specified (default), the shadow is assumed to be a drop shadow (as if the box were raised above the content). The presence of the inset keyword changes the shadow to one inside the frame (as if the content was depressed inside the box). Inset shadows are drawn inside the border (even transparent ones), above the background, but below content.

And a negative spread

spread-radius

This is a fourth value. Positive values will cause the shadow to expand and grow bigger, negative values will cause the shadow to shrink. If not specified, it will be 0 (the shadow will be the same size as the element).

(Both here)

Will give you the effect that you are looking for.

So you will need to change the place that the shadows are applied on elements.

So the final CSS:

#b {
  background: orange;
  z-index: 2;
  position: relative;
}

#c {
  background: red;
  -webkit-box-shadow: inset 0 10px 10px -10px black;
  -moz-box-shadow: inset 0 10px 10px -10px black;
  box-shadow: inset 0 10px 10px -10px black;
  z-index: 1;
  position: relative;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  z-index: 1;
  position: relative;
}
li {
  display: inline-block;
  padding: 2px 5px;
}

.current {
  background-color: orange;
  z-index: 3;
  position: relative;
}

#d {
  box-shadow: 0 0 10px black;
  z-index: 2;
}
Solution courtesy of: Arbel

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

How to layer box-shadows using z-index?

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×