ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Internet & the Web

CSS Z Index Tutorial

Updated on September 20, 2012

In this CSS Z Index Tutorial, I am going to describe the Z-index property which specifies to the position of the box along the z axis, which runs perpendicular to the display. The z-index value determines which one shows up on top. This property specifies the stack level of a box whose position value is one of absolute, fixed, or relative. Z-index is a CSS property and has been included in CSS because people asked for a property with the possibility to make HTML elements overlap. The technique is that an element with a higher number overlaps an element with a lower number.

What is a Z-Index

Z-Index is a CSS property to position element on the web page. Every element on the page can be layered above or below of other element. The z- index determines where the position of element in the stack is. The value of z-index may be positive, negative or 0. The element with the highest z-index value is always placed on top, followed by the element with next highest z-index value and so on down to the lowest value.

How to use Z-Index

Z-index uses values to identify which element is more important then the other. For this, you need to have the position style set. I prefer position absolute. Then, you need to set each layered a different z-index value in this CSS Z Index tutorial. The default value is 0. For example, I have three different values. If we give each element the same z-index value, they will display in the order define in HTML.

  • element 1 — z-index of 10
  • element 2 — z-index of 0
  • element 3 — z-index of -10

The Z-index requires properties to style the elements that should get ‘Z-indexed’. Our div elements require the position property is set to absolute to make things works. Otherwise our Z-index will not work. There are three div elements we used in the following example. Here the boxes have been styled with the right properties and is ready to be used. We got two sections. 1.html code and 2.css code. I simply added an position:absolute; and a couple placement values (top, left) to the boxes.

The CSS and HTML codes I have used are given below.

HTML Code:

<div class="z-box1">Red = Div #1 </div> <div class="z-box2">Green = Div #2 </div> <div class="z-box3">Blue = Div #3 </div>

CSS Code:

div.z-box1 {
	position: absolute;
 	top: 20px; left: 20px;
 	height: 250px; width: 250px;
 	color: white;
 	background-color: red;
 	z-index: -10;
div.z-box2 {
	position: absolute;
 	top: 150px; left: 10px;
 	height: 100px; width: 300px;
 	background-color: green;
 	z-index: 10;
div.z-box3 {
	position: absolute;
	 top: 15px; left: 175px;
 	height: 125px; width: 125px;
 	background-color: blue;
 	z-index: 0;

I wish you have understand this CSS Z Index tutorial properly. This method is simple but effective. If you have any questions about this article, don’t forget to leave a comment.


    0 of 8192 characters used
    Post Comment

    No comments yet.