CSS z-index illustrated
Tags: css, webdev, layout, codenewbie
Published: 2021-07-27 15:21:04

My followers know that I no longer work as a frontend developer, however, I worked as one for nearly 15 years and I was self-taught. I was lucky to have a background in architecture, CAD and graphic and 3d design, so grasping the more mathematical (or in this case geometrical) aspects of putting together a layout, was not difficult for me. However, I see that for self-taught developers coming from completely unrelated backgrounds, the `z-index` is a tough one to grasp.

This is my attempt at explaining it in an illustrated way.

Most people will be familiar with the concept of cartesian [coordinates](https://www.techopedia.com/definition/14290/cartesian-coordinates). They are used to define the location of a point in either a line (2d) or in space (3d). If you drive and use Google Maps, you are probably familiar with geospatial coordinates, or longitude and latitude; the points we use for locations on maps or Earth.

###2d or two dimensional###
In `2d`, we have the `y-axis`, and the `x-axis`. From their [convergence](https://www.techopedia.com/definition/769/convergence), the point (0,0), we can then specify where another point is located. Typically the `y-axis` defines the vertical location, and the `x-axis` the horizontal location of a point. 

![x-y-axis illustrated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5las3igefk54ciebz8ff.jpg)

If we have several points that define a shape, like a box, then we can define its dimensions AND its location, using coordinates.

This is basically how we do layout with css, defining the dimensions and location of elements with respect to those two imaginary lines that come defined by the edge of the viewport.

We understand `y=0` as the top of the browser viewport, and `x=0` as the left edge (when left to right), so (0,0) would be the most top-left point as we load the page (considering it is not programmatically scrolling to another `x,y` location).

![a shape with respect to the x y axis of the viewports](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4rqzbonscyn29iys07j.jpg)

### Layout and the box model

When we are adding new elements to the markup of a page, by default they're added to the default context (I will explain context later in the article). They will follow the document flow and be stacked on top of each other, in the same order they are added to the document.

So if we had 3 block elements like `divs` for example, added to a document like you see in the figure below, if we could see them from the side in terms of an imaginary vertical stack, they would be stacked in this way:

![divs stacked following the document flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atqr531eof4fhw352s9w.jpg)

But what would happen if you added a child to `div` number 2, and give it a top margin to move it on top of `div` 3 in the y-axis? That "overlapping" bit would be hidden by `div` 3, and that's expected, since in terms of markup `div` 3 is coming after `div` 4 in the document flow.

![divs markup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndn3sn0lyl1vtdq0qjzp.jpg)

![divs stacked following the document flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5lc5g3oy0lkuda7laom.jpg)


### z-index: 99999999999 !important will help...NOT

Many developers new to frontend will try to fix this using the `z-index` property, with a very high positive value (or maybe negative, supposign they wanted to completely hide the `div 4`). But that on its own, won't work. 

EDIT: Originally I had written -> "If you don't create a new stacking context, z-index won't work, no matter how many 9's and `!importants` you add." ** I will address [Temani Afif](https://dev.to/afif) comments below, stressing this: the new stacking context is created when position is set to ‘fixed’, ‘sticky’ or ‘relative’ and z-index is set to anything other than ‘auto’. Position on its own removes the element from the document flow, which means it no longer follows the order in the markup as elements are painted. However, it remains in the same stacking context.

## Understanding stacking contexts

Let's port that two-dimensional representation of cartesian coordinates, to a three-dimensional or spatial one. As you probably guessed, now we have 3 points that specify the location of a point: (x, y, z)

![3 dimensional representation of a layout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62rydnsnjpch3stw6mv2.jpg)

A stacking context is each new layer added to that z-axis, that you can now reorder, to bring elements on top or place them below the default context or other contexts. 

![stacking contexts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pip0ucwsr1jt1orb8tnx.jpg)

The order comes defined by the index, that can be 0, a positive integer or a negative integer or auto.

![stack with z-index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rh3rcr87bg5q8hys8byz.jpg)

I advise you now head to the official documentation on MDN, and read all you can about [stacking contexts](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context). But in a nutshell, you create a new stacking context by giving a container div the following properties and values:

- you give it an `opacity` different than 0
- you give it a `position` other than `static`, and a `z-index` other than `auto`
- you give it `display` either `flex` or `grid`, and a `z-index` other than `auto`
- you use `transform`, `perspective`, `filter`, `clip-path` or `mask`
- or you use some of the brand new properties, such as `isolate`(with a value of `isolate`), `will-change`, or `contain`.

That's all folks! I hope you find it useful and don't have to struggle with z-index issues anymore! *Also if this article has an error or you want to suggest an improvement, do it in the comments!*