tags:

views:

64

answers:

1

I'm trying to achieve this effect with css3:

header/main

The HTML code is cleanly something like

...
<header>
    ...
</header>

<div id="wrapper">
    ...
</div>
...

and the css is, for the moment, something like

header {
    display: block;
    width: 900px;
    height: 230px;
    margin: 0 auto;
    border: 1px solid #211C18;
    ...
    box-shadow: 2px 4px 20px #005377;
    -moz-box-shadow: 2px 4px 20px #005377;
    -webkit-box-shadow: 2px 4px 20px #005377;
}

#wrapper {
    width: 820px;    
    margin: 0 auto;
    ...
    border-right: 1px solid #211C18;
    border-bottom: 1px solid #211C18;
    border-left: 1px solid #211C18;
    ...
    box-shadow: 2px 4px 20px #005377;
    -moz-box-shadow: 2px 4px 20px #005377;
    -webkit-box-shadow: 2px 4px 20px #005377;
}

In order to obtain the desired result, I need to:

  1. Hide the top shadow of the main div (no problem with that)
  2. Bring header's bottom shadow in front of the main div, and not behind as it is right now.

I've been reading a lot about box-shadow, and I haven't found a solution that really pleases me... Any idea?

+1  A: 

This jsfiddle does what you want.

The way it works is with a main #wrap element that centres the content and creates a coordinate map for the absolutely positioned #main element. It does this because of its position: relative CSS rule. You end up with the following markup:

<div id="wrap">
    <header></header>
    <div id="main"></div>
</div>

The header is then placed inside of this and given position: relative and a z-index to set it stacking above the #main container.

Finally #main is absolutely positioned below the header. The CSS looks like so:

/* centre content and create coordinate map for absolutely positioned #main */
#wrap {
    width: 300px;
    margin: 20px auto;
    position: relative;
}

header, #main {
    background: #fff;

    /* rounded corners */
    border: 1px solid #211C18;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;    

    /* dropshadows */
    box-shadow: 2px 4px 20px #005377;
    -moz-box-shadow: 2px 4px 20px #005377;
    -webkit-box-shadow: 2px 4px 20px #005377;
}

header {
    display: block;
    width: 300px;
    height: 50px;

    /* stack above the #main container */
    position: relative;
    z-index: 2;
}

#main {
    width: 200px;
    height: 70px;

    /* stack below the header and underlap it...if that's even a word */
    position: absolute;
    z-index: 1;
    top: 40px;
    left: 50px;
}
Pat
Thank you so much, your answer was very detailed and useful...But... what if I have a footer and #main height isn't fixed (it can change A LOT according to page's content)? Adding absolute positions to the layout will move it upwards...
dolma33
If your `header` is fixed height, then you can just swap the CSS around and make the header position absolute and have it overlap the position relative `#main`.
Pat
Oh... looks that the problem is that the wrap collapses, so that it doesn't span for all the contained divs height, like in this jsfiddle http://jsfiddle.net/pbx4L/1/ [Thank you so much Pat for making me know jsfiddle] I used to deal with collapsing parents while using floats, but in this case I don't know how to fix the problem...
dolma33