CSS Z-index


The z-index property is used to move elements along the z-axis, i.e. in or out of view. It is used to specify the order of elements when they overlap. The z-index only works on positioned elements (position: absolute, relative, fixed, or sticky) and flex items (elements that are direct children of display: flex elements).

The z-index property can be specified using the keyword auto or as an <integer>.

1. Auto

The box creates no new local stacking context. In the current stacking context, the generated box has a stack level of 0.


2. Integer

In the current stacking context, this <integer> represents the stack level of the generated box. In addition, the box creates a local stacking context. This means that the z-indices of descendants are not compared to the z-indices of elements other than this one.

Syntax

Example

Preview

FAQs

z-index is a CSS property that controls the stacking order of elements. It only works on positioned elements (elements with a position value other than static). The z-index property sets the stack order of a positioned element and its descendants or flex items. An element with a higher z-index value will appear on top of an element with a lower z-index value. If two elements have the same z-index, the element that comes later in the HTML source order is placed on top. You can use positive or negative integer values to set the z-index property.

In CSS, the z-index property controls the stacking order of elements on a web page. Its maximum value is 2147483647. This value is the maximum integer value that can be represented in 32 bits, which is the limit of the z-index property. If you set a z-index value higher than this number, the browser will ignore it and treat it as the maximum value. It's important to note that using a high z-index value can cause stacking context and layout issues, so it's important to use it judiciously.

Debugging z-index issues in CSS can be challenging, but there are a few strategies you can use. First, use a browser's developer tools to inspect the elements and their z-index values. This can help you identify any conflicts or unexpected behavior. Second, try adjusting the z-index values of the conflicting elements to see if that resolves the issue. Finally, consider restructuring your HTML and CSS to avoid complex nesting and overlapping, which can cause z-index issues.