SpyreStudios

Web Design and Development Magazine

  • Design
  • Resources
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

What is Z-Index and How Does It Work?

June 18, 2018 by Alex Fox

Elements on a web page can stack for a variety of reasons. With basic CSS positioning tools, it’s possible to stack elements on top of one other with negative margins, floats, and other tools. Even without specific position, elements stack, with div stacking on backgrounds on table cells stacking on tables. But default stacking is difficult to control, relying on hard-to-remember natural stacking rules and the order of elements within the DOM. This controls the way objects are painted on the canvas of the web page, and the order in which they’re processed. In order to stack elements more effectively, we should use the CSS property Z-index. This specifies rules for object stacking.

What is Z-index?

Z-index is a CSS property that is used to organize elements in terms of relative depth. If you imagine the viewport as a two-dimensional plane, you could call the height “X” and the width “Y”, just like in mathematics. If you add a third line for depth coming “out” of the screen, you could call it Z. It’s very similar to settings like “Send to Front” and “Send Back” found in applications like Word and Photoshop. It allows for positioning overlapping elements on virtual layers, just as you might do in Photoshop. Higher z values are closer to the top of the stack of objects, while low values are closer to the bottom of the stack objects

With z-index, you can control how elements stack without your web page, allowing you to override default stacking behavior. This allows you to overlap elements with precision instead of luck. Elements on the same z-index level will interact with their x and y coordinates, but elements on different z indexes are ignorant of one another. This means that you can have multiple layers of objects that “stack” without moving one another around, allowing for easy pop-overs, pop-unders, sliders and more.

How Does Z-index Work?

The value you set for z-index is arbitrary. You can give an object a z-index of 5000 without setting another other z-index values. This would ensure that it will almost certainly appear on top of other objects. Z-index can also be set with negative values, allowing you to specify objects as “always behind” other objects. You can think of these z index specifications as adding to or subtracting from the element’s default z-index value.

You can also set an “auto” value for z index. This takes the z-index from any parent properties. The box does not establish a new local stacking context. A stacking context is based upon a variety of factors, which are explained in part below. The stack level of the generated box in the current stacking context is the same as its parent’s box.

Like many CSS properties, child elements always taken the Z-index of their parent objects through inheritance. Further, nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be higher than element B.

Stacking context is also determined by parent and child element relationships. The z index you set only applies within the parent element. If you have one div inside another, for example, you might find that stacking doesn’t obey the z index rules you set. This is often do to other positioning rules or parent-child relationships. Check out the example below to see how that works.

Note that the z index of DIV #1 is lower than the z index of DIV #4, yet #1 appears on top. This is because #4 is stacked within its parent DIV, inheriting its properties.

Here are some examples of how you might set z-index in your CSS:

/* Keyword value */
z-index: auto;

/*  values */
z-index: 0;
z-index: 3;
z-index: 289;
z-index: -1; /* Negative values to lower the priority */

/* Global values */
z-index: inherit;
z-index: initial;
z-index: unset;

How Does Z-index Stack With Other Positioning Rules?

In an HTML page, z-index can interact with other positional rules based on their stacking context. The list below, cited from the MDN documentation on stacking context, specifies how objects stack together based on their criteria, starting at the bottom of the stack. It’s a complicated policy, which is why z-index is often set at extreme values for only some objects.

  • Root element of document (HTML).
  • Element with a position value “absolute” or “relative” and z-index value other than “auto”.
  • Element with a position value “fixed” or “sticky” (sticky for all mobile browsers, but not older desktop).
  • Element that is a child of a flex (flexbox) container, with z-index value other than “auto”.
  • Element with a opacity value less than 1 (See the specification for opacity).
  • Element with a mix-blend-mode value other than “normal”.
  • Element with any of the following properties with value other than “none”:
    • transform
    • filter
    • perspective
    • clip-path
    • mask / mask-image / mask-border
  • Element with a isolation value “isolate”.
  • Element with a -webkit-overflow-scrolling value “touch”.
  • Element with a will-change value specifying any property that would create a stacking context on non-initial value (see this post).

You might also like the following posts:

Using CSS Pseudo-elements and Pseudo-classes like ::before and ::after

Coding Buttons in CSS

3 Features Every CSS Navigation Menu Must Have

Filed Under: CSS Tagged With: css, css3, layout

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022