Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make box-shadow appear above a div?

Tags:

html

css

Here is an example of what I'm trying to accomplish.

The box-shadow from the upper div won't appear on top of the div below it. From what I understand, I need to set the z-index so it will appear on top and that only works on elements with position: relative; but it's still not appearing.

What am I doing wrong?

#top {
    width: 100%;
    height: 100px;
    box-shadow: 3px 3px 3px #333;
    background-color: blue;
}

#middle {
    width: 100%;
    height: 200px;
    z-index: 0;
    position: relative;
    background-color: orange;
}

#innerMiddle {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    background-color: green;
}
<div id="top">
</div>

<div id="middle">
    <div id="innerMiddle">
    </div>
</div>
like image 230
Vecta Avatar asked Feb 08 '12 18:02

Vecta


People also ask

How do you make a div appear on top of everything else on the screen?

Set the DIV's z-index to one larger than the other DIVs. You'll also need to make sure the DIV has a position other than static set on it, too. position relative and z index set to 999999999999999999999999999999999999999999999999999.

How do I add a shadow to a block in CSS?

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

It's #top and its box-shadow that you want to appear on top, so give that position: relative instead of giving position: relative to #middle. There's no need for z-index: 0.

http://jsfiddle.net/thirtydot/QqME6/1/

#top {
    width: 100%;
    height: 100px;
    box-shadow: 3px 3px 3px #333;
    background-color: blue;
    
    position: relative;
}

#middle {
    width: 100%;
    height: 200px;
    background-color: orange;
}

#innerMiddle {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    background-color: green;
}
<div id="top">
</div>

<div id="middle">
    <div id="innerMiddle">
    </div>
</div>
like image 177
thirtydot Avatar answered Sep 29 '22 13:09

thirtydot


Change your CSS to:

#top {
    width: 100%;
    height: 100px;
    box-shadow: 3px 3px 3px #333;
    background-color: blue;
    position:relative;
    z-index:1;
}

#middle {
    width: 100%;
    height: 200px;
    z-index: 0;
    position: relative;
    background-color: orange;
}
<div id="top">
</div>

<div id="middle">
    <div id="innerMiddle">
    </div>
</div>
like image 22
Sagar Patil Avatar answered Sep 29 '22 13:09

Sagar Patil