Exploring z-index with CSS Cookbook

  • 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.
 

Related to Exploring z-index with CSS Cookbook

1. What is z-index in CSS?

Z-index is a CSS property that controls the stacking order of elements on a webpage. Elements with a higher z-index will be displayed on top of elements with a lower z-index.

2. How do I use z-index in my CSS?

To use z-index, you can add the property to a CSS selector and give it a numerical value. The higher the value, the closer the element will appear to the front of the page. You can also use negative values to position an element behind another.

3. Can I apply z-index to any element?

Yes, z-index can be applied to any element that has a position property set to absolute, relative, or fixed. It will not have any effect on elements with a static position.

4. How does z-index work with nested elements?

Z-index will only affect elements that are positioned within the same stacking context. If an element has a higher z-index than its parent, it will be displayed on top of its parent's content. However, it will still be behind any elements with a higher z-index in a different stacking context.

5. What are some common use cases for z-index?

Z-index is often used to control the layering of elements in a webpage layout. It can be particularly useful when creating dropdown menus, modal pop-ups, or overlapping images or text. It can also be used to create a sense of depth and hierarchy in a design.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top