HTML/CSS What is the role of z-index in CSS positioning?

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Css
AI Thread Summary
The discussion revolves around the use of the z-index property in CSS, particularly in the context of a code example from "CSS Cookbook" by Christopher Schmitt. Participants express confusion about the functionality of z-index, noting that it should control the stacking order of elements but seems ineffective when applied to a single element. It is clarified that z-index only affects elements within the same stacking context, which is determined by shared parent elements. The default z-index is zero, meaning elements without a specified z-index are placed in the background. To bring an element to the foreground, a positive z-index must be assigned, while elements with a negative z-index will be rendered behind those with a zero or positive value. Additionally, it is noted that z-index does not function within SVG contexts as it does in standard HTML, where the order of elements is more critical. Recommendations for more current resources, like MDN and W3Schools, are provided to aid in understanding z-index and its application.
shivajikobardan
Messages
637
Reaction score
54
TL;DR Summary
Can't see what is z-index doing in CSS.
I'm following "CSS Cookbook" by Christopher Schmitt to learn CSS.
I am going through this example.

HTML:
<div class="image">text goes here</div>

CSS:
.image {
  position: relative;
  z-index: 20;
  width: 130px;
  height: 140px;
  background-image: url(circle.png);
  border: 1px solid black;
  background-repeat: no-repeat;
  background-size:cover;
}
I'm seeing no difference with or without z-index. z-index should be stacking elements but not sure what does that mean here. (I don't have access to book's images that's why I'm using my own images).

Here's the image that I'm using:
https://upload.wikimedia.org/wikipe...imple.svg/800px-Circle_-_black_simple.svg.png
 
Technology news on Phys.org
shivajikobardan said:
I'm following "CSS Cookbook" by Christopher Schmitt to learn CSS.
The most recent edition of that book was published in 2009. Use something more up to date e.g. the W3 schools guide https://www.w3schools.com/cssref/pr_pos_z-index.php.

shivajikobardan said:
I'm seeing no difference with or without z-index. z-index should be stacking elements but not sure what does that mean here.
What does a stack of one element look like?

shivajikobardan said:
Here's the image that I'm using:
You should not use WikiMedia images for this, use free images designed for this purpose e.g. Link Removed
 
example of w3schools wasn't very clear. anything else you can recommend? mdn docs was too vast.
 
Think of web elements as existing on a layer with the page theoretically consisting of infinite layers. z-index allows you to move elements forward and back between those layers.
 
  • Like
Likes shivajikobardan and FactChecker
The z-index has a long history in computer graphics. It tells which objects are in front of others so you can't see what is behind. I imagine it is used the same way in CSS. If one z-index level is completely opaque then it will block anything behind it (lower z-index) but not anything in front of it (higher z-index).
 
One important detail to note is that each stacking context is independent of each other, so elements can only be z-ordered relative to each other if they are within same context, i.e. they share a common ancestor that act as stacking context.
 
The default z-index is zero (far background).
So if you only assign a z-index to one element, and that z-index is a positive value, it will be position to the foreground.
So, in a sense, you can't use z-index to send something into the background - because with the default, it is already there. You need to assign the z-index to the images you want in front.

Also, don't expect the z-index parameter to work inside an svg section. For svg, only the order of the elements matters - but there are methods for rearranging the elements.
 
  • #10
.Scott said:
The default z-index is zero (far background).
The defaut z-index value is 0, but this is not the far background: elements with a negative z-index will be rendered behind elements with z-index of 0 in the same stacking context.
 
  • #11
It's also helpful* to recognize that it does not work unconditionally. I've worked with it a lot and found its applicability rather finicky. See Filip Larsen's post #7 for example.

*actually not helpful at all

Better to come with extant examples and figure out how it's borked and how to fix it.
 

Similar threads

Replies
9
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
5
Views
2K
Replies
3
Views
2K
Replies
3
Views
3K
Replies
2
Views
5K
Replies
9
Views
4K
Back
Top