Javatpoint Logo
Javatpoint Logo

CSS Z Index

The stacking order of components on a web page is usually managed using the CSS "z-index" attribute. Here, "stacking" refers to the visual arrangement and layering of components on top of one another on a website. The stacking order decides which element displays in front and which appears behind the others when many elements share the same area on a website.

The Z-axis, sometimes known as the depth or "third dimension" of a website, is controlled by the Z-index, which controls how items are stacked and presented to one another.

Example:

In the above example, the `.box` element will be layered above other elements within the `.container` element due to its higher `z-index` value.

Advanced Concepts of Z Index

  1. Stacking Contexts: When an element is positioned with a `z-index` value, it creates a stacking context. Each stacking context is an independent layer, and elements within a stacking context are only compared to each other when determining their stacking order.
  2. Stacking Order: Stacking orders determine how elements are layered within a stacking context. Three factors determine the stacking order:
    • The stacking context with a higher `z-index` value is placed on top of a stacking context with a lower value.
    • The element that occurs later in the HTML structure is shown on top if two elements have the same stacking context.
    • If two elements have the same 'z-index' value and a similar stacking context, their stacking order is determined by the order in which they appear in the HTML structure.
  3. Negative z-index: The default stacking order or items with a negative "z-index" value are placed below those with a positive value.
  4. Positioning Contexts: Elements with different positioning types (`position: static`, `relative`, `absolute`, or `fixed`) create their own stacking contexts. For example, an element with `position: relative` and a `z-index` value will be layered within its stacking context, independent of other stacking contexts.
  5. Sticky Positioning: Elements with `position: sticky` are positioned according to their normal position in the document flow until a specified threshold is reached. Sticky elements are treated as if they were `position: relative` until the threshold is met, and then they become `position: fixed`. Elements with `position: sticky` can have a `z-index` value to control their stacking order within their stacking context.
  6. Parent-Child Relationship: The stacking context of a parent element determines the 'z-index' value of a child element. The stacking order of the child element inside that context may change if the parent element has a greater "z-index" value.
  7. Transparency and Stacking Order: Elements with transparent backgrounds or opacity values less than 1 can affect the stacking order of overlapping elements. The transparent or semi-transparent areas are treated as empty, allowing elements behind them to be visible and potentially affecting the stacking order.

Next TopicCSS minify





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA