-2

I have two divs, one parent and other child both have their own background color. Child background color is overlapping the parent background color but I want parent background to overlap child background. Please follow the JS-Fiddle

<div id="p" >
    <div id="c"/>
</div>

Here is Fiddle link http://jsfiddle.net/26husf4v/2/

Red color (parent background) should come on top of blue(child) background, while the exceeding red color remain visible

  • 1
    Welcome to Stack Overflow! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Paulie_D May 12 '20 at 09:39
  • edited the question, Basically i need parent background color on top of child background @Paulie_D – Hassan Afzal May 12 '20 at 09:45
  • But the elements are the same size...what "exceeding blue" would there be? You can layer the parent on top of the child but I'm not sure that's exactly what you want - http://jsfiddle.net/t8gndueh/ – Paulie_D May 12 '20 at 09:49
  • http://jsfiddle.net/26husf4v/2/ check latest fiddle – Hassan Afzal May 12 '20 at 09:56
  • What are you expecting.? What is the "exceeding blue"? – Paulie_D May 12 '20 at 10:05

1 Answers1

0

Actually this is achievable with z-index, but in order to:

while the exceeding blue color remains visible

(If I got you right!) You should make your child element larger than the parent instead of what you have done earlier in the snippet.

So your final code should be something like this:

#p {
  width: 200px;
  height: 200px;
  background: red;
}

#c {
  position: relative;
  z-index: -1;
  width: 300px;
  height: 300px;
  background: blue;
}
<div id="p">
  <div id="c"></div>
</div>
SMAKSS
  • 9,606
  • 3
  • 19
  • 34
  • 1
    This *might* be what the OP wants (that's still not clear) but this is not overlaying **backgrounds**, this is overlaying the **elements**. I'm suspecting what the OP wants may be more complex....but I could be wrong. ☺ – Paulie_D May 12 '20 at 10:11
  • @Paulie_D Yea, you are totally right. It is based on my understanding from the OP :D – SMAKSS May 12 '20 at 10:13