Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a child element a higher z-index than the parent element

Tags:

html

css

z-index

I've run into a small problem with z-indexing.

I have a header and inside that a link. The header has a z-index of 5, and the link has a z-index of 15. When I open up a modal, the dark overlay should be above the header, so I gave it a z-index of 10. The problem is that the link within the header should be above the overlay, but it's behind it. I've even given the link a position of relative, but it does nothing.

Can anyone help me with this problem?

like image 763
flobar Avatar asked Jul 11 '13 18:07

flobar


People also ask

Can a child have a higher z-index than its parent?

This is impossible as a child's z-index is set to the same stacking index as its parent.

Can a child have a lower Z-index than parent?

How to get a child element to show behind (lower z-index) than its parent? is to: Make the parent not a stacking context. Make the child a positioned stacking context , whose z-index smaller than 0.

Do child elements inherit Z-index?

No, it isn't inherited.

Is Z-index relative to parent?

The z-index of elements inside of a stacking context are always relative to the parent's current order in its own stacking context.

How to use z index with image in CSS?

Z index with an Image. As you can see in the above which ever z index value is greater that is always overlapping lower index value box. Z index in CSS property is used to put the any element on top of the other element. But Z index can be applied with only positioned element like absolute, relative and sticky etc.

What is the difference between stack order and z index?

An element with greater stack order is always in front of an element with a lower stack order. Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky).

Is it possible to change the z-index of a child element?

This is impossible as a child's z-index is set to the same stacking index as its parent. You have already solved the problem by removing the z-index from the parent, keep it like this or make the element a sibling instead of a child. Nothing is impossible.

How to make a child <div> element wider than the parent element?

Making a child <div> element wider than the parent <div> can be done with some CSS properties. In the example below, we set the position of the parent <div> to “static”, which means that elements are placed according to the document’s normal flow. Here, we add the width of the parent container and specify its background-color and margin as well.


1 Answers

z-index is relative to the siblings within the same parent. you will have to take the <a> out of the <header> to create the desired effect.

If the parent has lower z-index than the modal, everything in it will be behind the modal, regardless of the children's z-index

Please also attach a jsFiddle or something so we can show you how to fix this.

like image 113
Yotam Omer Avatar answered Oct 01 '22 00:10

Yotam Omer