When I first started using AJAX I soon came across a problem that had me stumped for a while. The problem was that some of the AJAX tools like hover menus, popup windows, calendars etc were appearing behind other controls on the page so that they would either be only partially displayed or not even displayed at all.
Now a lot of you reading probably already know the simple fix for this, but just in case there are some out there like me who got stuck at this point I hope this little tip can help.
There is a little property in CSS called Z-Index and to use the official description the z-index property sets the stack order of an element. An element with greater stack order is always in front of another element with lower stack order.
What this means is that the higher the Z-Index number of an element on the page the higher it will appear in the stack, so if you think of your page as a three dimensional element, the item with the highest Z-Index will be on the top of the pile of elements.
By default if you don’t specify your Z-Index for any css element then they all have a default of 0 and they will be stacked in the default order. So if you have a relatively simple requirement where you only need to ensure one item is always on top, all you need is this in the elements CSS properties:
.myelement
{
Z-Index: 1;
}
As long as no other elements have the same or higher Z-Index your element should appear at the top of the stack. Conversely you can also set a negative Z-Index number if you want to ensure an element is pushed lower down the stack.
My blog is mainly about programming in .Net 2, website promotion and affiliate marketing although I do have the odd ramble on about anything that comes to mind.


May 22nd, 2008 at 2:35 pm
Our site had this exact problem. However, the z-index issue made it even worse when dealing with different cssclass divs over several pages with different layouts. Cross-browser compliance was also an issue. My advice is to use z-index sparingly; try to design your site to avoid the issue in the first place.
May 23rd, 2008 at 2:04 am
I agree, if you can avoid needing it at all you should do that. I still get the odd problem with it, especially when the control needs to appear over another DIV. Sometimes just a simple page reorganisation is all that is needed.