Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to layer box-shadows using z-index?

Tags:

css

codepen

#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;
}
<div id="a">
  <div id="b">bbb</div>
  <div id="c">
    <ul>
      <li>a</li>
      <li class="current">b</li>
      <li>c</li>
      <li>d</li>
    </ul>
  </div>
</div>
<div id="d">dddd
</div>

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.

like image 833
mpen Avatar asked Apr 14 '14 18:04

mpen


People also ask

How do you add two box shadows?

To make these multiple shadows we just need to define multiple shadow value and seperate them with a comma. Then we'll position the shadows using different values for x-offset and y-offset values. The shadow value which comes last is positioned at the back of all the shadows. Color : To create an inner shadow.

How do you box shadow on all 4 sides?

If you set the offsets to zero, the shadow will be equal on all four sides.

How do you implement a box shadow?

CSS Syntax box-shadow: none|h-offset v-offset blur spread color |inset|initial|inherit; Note: To attach more than one shadow to an element, add a comma-separated list of shadows (see "Try it Yourself" example below).


2 Answers

If you're using z-index, make sure the element has a defined position attribute or it won't work. Wherever you use z-index in your css, define the position of that element. (Absolute, relative, inherit...)

like image 90
FreeStateNH Avatar answered Sep 24 '22 19:09

FreeStateNH


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

Tip: Make sure position is explicitly set on elements with z-index e.g. relative etc, even if you're fine with the default, it has to be set.

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;
    }
<div id="a">
  <div id="b">bbb</div>
  <div id="c">
    <ul>
      <li>a</li>
      <li class="current">b</li>
      <li>c</li>
      <li>d</li>
    </ul>
  </div>
</div>
<div id="d">dddd
</div>
like image 20
Arbel Avatar answered Sep 23 '22 19:09

Arbel