Showing posts with label z-index. Show all posts
Showing posts with label z-index. Show all posts

Friday, November 10, 2017

CSS overlapping and z-index

In CSS, there are three different things that impact how html elements overlap each other: stacking contexts, source order, and painting order (a.k.a. stacking level).

Stacking contexts
When you apply z-index to a positioned element, you do two things.

First, you say that the element is in front or behind other elements that share the same stacking context. This is what we normally think about when we change z-index -- we want something to move in front or in back of something else.

Second, you create a new stacking context for anything inside the positioned element. Once you've created a stacking context, any layering that happens inside that stacking context stays in that context. This is the part we forget.

In a normal document, without any positioning, the document has exactly one stacking context -- the one created by html. When dealing with positioning, the stacking context is probably the hardest thing to remember.

Source code
There are two different ways of looking at an HTML document source code. One is by reading, from top to bottom, to see which tags start before which tags. The other way to look at this is in tree order where you keep track of not only which tags start first, but also which tags are inside other tags.

Stacking levels (inside of stacking contexts)
Normally, adjusting the z-index property and keeping track of stacking contexts is all you need to worry about. But occasionally you'll run into a situation where one box paints over another, even though there's no positioning involved. This happens because of how data is stacked inside of a stacking context. Just to make things confusing, the specs call this concept stacking levels.

The specs define 7 painting layers. Starting from back to front, they are:
  1.     The borders and background of the current stacking context
  2.     Positioned descendants with negative z-index (or goes before #1 in modern browsers)
  3.     Non-positioned block-level descendants with no z-index property defined -- paragraphs, tables, lists, and so on
  4.     Floating descendants and their contents
  5.     Non-positioned inline content
  6.     Positioned descendants with no z-index, z-index: auto, or z-index: 0
  7.     Positioned descendants with z-index greater than 0

Some key points to remember:
  1. Source order counts.
  2. Inline elements paint over floats.
  3. Positioned elements paint on top of everything else if they don't have negative z-index.
http://css-discuss.incutio.com/wiki/Overlapping_And_ZIndex
The reference article has details including examples to explain CSS overlapping within stacking contexts.


Thursday, November 5, 2015

Z-index 101

Z-index 101
1. z-index only applies to positioned elements (relative, absolute, fixed)
2. Sets the stacking level to the integer and establishes a new stacking context if gives an integer value to z-index property
3. There are two concepts: stacking level and stacking context

There are seven possible levels on each stacking context, which are listed below.
1. Background and borders — of the element forming the stacking context. The lowest level in the stack.
2. Negative Z-Index — the stacking contexts of descendants elements with negative z-index.
3. Block Level Boxes — in-flow non-inline-level non-positioned descendants.
4. Floated Boxes — non-positioned floats
5. Inline Boxes — in-flow inline-level non-positioned descendants.
6. Z-index: 0 — positioned elements. These form new stacking contexts.
7. Positive Z-index — positioned elements. The highest level in the stack.

See the image for better understanding of 7 stacking level

Stacking context can be explained by the following rules.

1. The default stacking context is the root element
2. Establish a new stacking context with the z-index property
3. Child elements cannot be stacked above (or below) the parent element’s stacking level

References
http://bitsofco.de/2015/how-z-index-works
http://webdesign.tutsplus.com/articles/what-you-may-not-know-about-the-z-index-property--webdesign-16892