2

I have following mark up:

<div class="one">Content</div>
<div class="two"></div>

and following style sheets:

.one, .two {
    height: 20px;
}
.one {
    background-color: #f00;
}
.two {
    background-color: #0f0;
    margin-top: -10px;
}

Why is the text Content visible, but the red background is not? I would expect the text also to be only partly visible due to the given style sheets.

For your convenience, I also created a jsFiddle.

Amberlamps
  • 39,180
  • 5
  • 43
  • 53

1 Answers1

1

If you want that text of first div would be only partly visible, you need to use position and z-index.

.one, .two {
    height: 20px;
}
.one {
    background-color: #f00;
    position: relative;
    z-index: 1;
}
.two {
    background-color: #0f0;
    margin-top: -10px;
    position: relative;
    z-index: 2;
}

http://jsfiddle.net/v5LfZ/2/

MadCom
  • 241
  • 2
  • 8
  • This solves the problem but it does not explain why my versions.behaves like it behaves?!? – Amberlamps Jan 29 '13 at 13:55
  • Because background is background and he deserves to be in background, but if you want that your background would not be in background - use z-index :) – MadCom Jan 30 '13 at 14:36