Z-Index : Overlap HTML elements with CSS
Author: Stefan Vervoort | Published: 06 May 2008 | RSS | LINK
Font size: 

Via this guide, you will learn what a Z-Index is and how to use this property to overlap HTML elements with CSS.
What is a Z-Index
Z-index is an property in CSS and has been included in CSS because people asked for an property with the possibility to make HTML elements overlap. Overlapping HTML elements can have a lot of advantages. You can make the important parts of your website fall out from the background some more.
How to use Z-Index?
Z-index uses values to identify which element is more important then the other. The default value is 0. Usually, I give the item that should be on top value 20, the less important 10 and the least important item 1.
It doesn’t really matter which numbers you give to the Z-index. The only thing you should remember is that your top item should have to highest value. But adding Z-index to items isn’t enough to make things work.
For our example, we will use the following HTML structure. Those boxes should get ‘Z-indexed’ later on!
<div class="box1">Blue = Div #1 </div>
<div class="box2">Pink = Div #2 </div>
<div class="box3">Yellow = Div #3 </div>
Click here to take a look at an example I made. The item that should be on top (because of the Z-index value) is not on top: the boxes are displayed under each other instead of on top of each other. It looks like the Z-index is not working in this example, right?
The codes I have used in this example. It should work was my first guess as well.
div.box1 {
height: 250px;
width: 250px;
background-color: blue;
z-index: 1;
}
div.box2 {
height: 100px;
width: 300px;
background-color: pink;
z-index: 20;
}
div.box3 {
height: 125px;
width: 125px;
background-color: yellow;
z-index: 10;
}
What’s up?
The Z-index requires an other property to style the elements that should get ‘Z-indexed’. Our element requires the position property is set to absolute to make things works. Otherwise our Z-index won’t work, as can be seen in the example above.
It’s now time for an example that works. The boxes has been styled with the right properties and is ready to be used. A working Z-index example can be found here.
These are the codes I used here. I simply added an position:absolute; and a couple placement values (top, left) to the boxes. It works!
div.box1 {
position: absolute;
top: 20px; left: 20px;
height: 250px; width: 250px;
color: white;
background-color: blue;
z-index: 1;
}
div.box2 {
position: absolute;
top: 150px; left: 10px;
height: 100px; width: 300px;
background-color: pink;
z-index: 20;
}
div.box3 {
position: absolute;
top: 15px; left: 175px;
height: 125px; width: 125px;
background-color: yellow;
z-index: 10;
}
We are done
I hope you now have fully understood the Z-index principle. You should always remember to add a position:absolute; property, or your Z-index will not work! If you have any questions regarding this subject, contact me or leave a comment.
15 Comments
Leave a Reply
This blog is a DoFollow blog. This means your URL counts as a backlink. Some basic HTML is allowed. Please keep all comments constructive, polite and on-topic. Any spam or offensive comments will be deleted.









12:35 am on May 8th, 2008
great post!! your blog is very good… congratulations…
8:11 pm on May 10th, 2008
Hmmmm…
Pink on white, never a good idea. and I am almost certain that height is not a cross browser compliant command anymore.
But at least you have taken the time to write something which is more than me.
6:47 am on May 11th, 2008
@Avangelist - Pink on white? What are you talking about? Height not cross browser compliant? I don’t understand your comment.
tincho 1492 | Martín - Thanks for that comment.
7:54 pm on May 13th, 2008
Helle Stefan
I stumbled over your blog following a link from a Danish weblog. I actually like the content here - especially your CSS guides. Keep’em comming!
Tot ziens from Denmark
8:17 pm on May 13th, 2008
Hello!
Thanks for visiting and thank you for your compliment! I have visited your blog as well, but unfortunately I can’t understand Danish.
Tot ziens from Holland!
2:45 pm on May 27th, 2008
[...] [...]
7:55 pm on June 3rd, 2008
Is there a way to do this without using style sheets?
Would you try content or something like that?
7:57 pm on June 3rd, 2008
oops i put something in code before and after the word “content” in that comment, so you cant see it :\
12:23 pm on June 4th, 2008
@Rob - There is a way, when using Inline CSS. You should style the div like this:
< *div style="z-index:1; position:absolute;">< */div>
and div 2:
< *div style="z-index:10; position:absolute;">< */div>
It sure is available without CSS, (via Javascript I guess) but that makes things a lot harder. Try using this way, and if you want to know anything else, let me know.
* remove the star (Wordpress won’t allow me to post codes.)
8:39 am on June 11th, 2008
Thank you for this tip. Will have to experiment with this when I come to design some web pages. I never really knew that this was possible but it looks interesting.
9:38 pm on June 11th, 2008
Thanks, you do that.
2:28 am on July 14th, 2008
Is css worth taking a year to learn or how long does it actually take to learn. I am just wondering cause this article really dragged me into it and im just wondering if it is easier to hire someone to do css work for me, or should i learn it and therefore save money. Let me know your opinion Please.
ps - for Webwork visit http://www.CoderPhase.com and meet the best freelancers now.
Regards
11:05 pm on August 2nd, 2008
Most Excellent! This will definitely help me when i’m creating wordpress themes!!
Thank you
2:22 am on August 13th, 2008
thanks for this, i have bookmarked your site and look forawrd to seeing more tutorials
thanks once again.
Bebo Skins !
2:30 am on August 13th, 2008
Awesome tip. This will do wonders for our themes. thanks again admin