Producing web pages
* HTML CSS DHTML XHTML A to Z of tags        Accessibility          Design

z-index

The z-index property sets the order in which the browser draws tags. By default, the browser draws tags in the order in which they appear; tags that appear later in the page appear over the top of earlier ones. The z-index property lets you control the order in which you want things layered; tags are drawn from lowest to highest z-index values.

For example, you have the following styles:

h1 {position: relative;left: 10px; top: 0px; z-index: 2;}
h2 {position: relative;left: 30px; top: -45px; z-index: 1;}

You then use the headings on the page:

This HTML…

Displays as…

<h1><font color="red">Overlapping</font></h1>

<h2>Text</h2>

In the example below, both paragraphs are at almost the same absolute location on the page. The second paragraph is 10 pixels lower than the first. Normally, the second tag would be put on top of the first tag, but you can override this by setting the z-index properties for the tags.

You have the following styles:

p.over {position: absolute;left: 20px; top: 80px; 
             z-index: 2; background-color:gray}
p.under {position: absolute;left: 20px; top: 90px; 
             z-index: 1;background-color:red}

You use the styles on the page:

This HTML…

Displays as…

<p class="over">This little box is promoted to show you how to overlay one tag with another.</p>

<p class="under">This little box gets left behind. It would usually be on top so it is a bit miffed</p>