Use of Z-index property in CSS

Z-Index

Its the stack order of element.
Higher the Z-index value of an Element  means closer to the top of stack order.

Lets see the example :
I have taken Two DIVs (Div1 and Div2)

<div id="yellowblock"></div>
<div id="blueblock"></div>

then I styled it as

<style>
#yellowblock {
z-index: 1;
position: absolute;
width:80px;
height:100px;
top:20px;
left:20px;
border: 1px solid #FFF;
background-color: #f3b02e;
}

#blueblock {
z-index: 2;
position: absolute;
width:100px;
height:80px;
top:50px;
left:50px;
border: 1px solid #FFF;
background-color: #02b9cf;
}
</style>

Its Output :

When I interchange there Z-indexes as:

<style>
#yellowblock {
z-index: 2;
position: absolute;
width:80px;
height:100px;
top:20px;
left:20px;
border: 1px solid #FFF;
background-color: #f3b02e;
}

#blueblock {
z-index: 1;
position: absolute;
width:100px;
height:80px;
top:50px;
left:50px;
border: 1px solid #FFF;
background-color: #02b9cf;
}

</style>

its output is :

The javascript syntax for using Z-index is

document.getElementById("blueblock").style.zIndex = "4";

Some Facts-
z-index only works on elements that are positioned.

Hope this post will be useful for you.
Please share / recommend if you like this post.

Thanks,
Nikhil Ughade
UI Designer
Mumbai[INDIA]

Advertisements

About Nikhil
Name : Nikhil Ughade UI Developer, Accidentally in design field, no turn back since then. Location : Pune (MH, INDIA) From : Amravati (MH,INDIA) College : VYWS's College Of Engg. Badnera, Amravati Hobby : Social Networking, Designing Web Sites, Photography, Reading Books. In Free time works for NGO.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: