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

  • HTML/CSS
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Css
In summary, the author is following a guide to learn CSS and is seeing no difference with or without z-index. He recommends using something more up to date, such as the W3 schools guide.
  • #1
shivajikobardan
674
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
  • #2
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
 
  • #3
example of w3schools wasn't very clear. anything else you can recommend? mdn docs was too vast.
 
  • #5
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
  • #6
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).
 
  • #7
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.
 
  • #9
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.
 
  • Like
Likes .Scott
  • #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
880
Replies
5
Views
707
Replies
4
Views
2K
Replies
3
Views
3K
Replies
1
Views
3K
Replies
2
Views
4K
Replies
9
Views
3K
Back
Top