0

Say I have the following HTML markup:

<div class="site-banner">
    <div class="parent">
        <div class="children-1">
            some nice text
        </div>
        <div class="children-2">
            <button>button</button>
        </div>
    </div>

    <img class="building"/>
</div>

And say that id looks something like this: enter image description here

Now what I want to happen, is when the user scroll down, the parent stays fixed and and the title/button will have that behavior: enter image description here

So logically the CSS should be something like this:

.parent{
    position: fixed;
}
.children-1{
    position: relative;
    z-index: 2;
}
.children-1{
    position: relative;
    z-index: 1;
}
.children-2{
    position: relative;
    z-index: 3;
}

But it doesn't work, the two children gets the z-index of the parent, if the parent have z-index of 4, then both the title and the button will be in front of the building, if the parent have z-index of 2, then both will be behind the building, I can't separate them.

One thing I did that worked is to separate the parent to two parents, so the button and the title will not have the same parent, and put z-index to the two parents. But I want the button position to be relative to the title, so it will always have the same distance below the title, and after separate them to two parents I had some mess in some resolution, I needed to set different top property to the button/title in a lot of resolutions.

Is there any way to achieve what I try to do, with one parent?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Avishay28
  • 2,288
  • 4
  • 25
  • 47
  • Is the size of `.children-1` or `.children-2` dynamic? – Itay Ganor Dec 16 '18 at 16:40
  • @ItayGanor no.. – Avishay28 Dec 16 '18 at 16:40
  • As far as I know, I think it is impossible 'cause you created a stacking context (with position `fixed`) and the z-index values of its child only have meaning in this parent. => Here you can find a similar question: https://stackoverflow.com/questions/27329962/z-index-with-different-parents, but with the GREAT difference that in that question the parent doesn't create a new stacking context. Unfortunalty, I think you have to find another way. :\ – ReSedano Dec 16 '18 at 21:58

0 Answers0