z-index 應用簡單總結

謙行發表於2013-08-19

做過頁面佈局的同學對z-index屬性應該是很熟悉了,z-index是針對網頁顯示中的一個特殊屬性。因為顯示器是顯示的圖案是一個二維平面,擁有x軸和y軸來表示位置屬性。為了表示三維立體的概念如顯示元素的上下層的疊加順序引入了z-index屬性來表示z軸的區別。表示一個元素在疊加順序上的上下立體關係。

z-index值較大的元素將疊加在z-index值較小的元素之上。對於未指定此屬性的定位物件,z-index 值為正數的物件會在其之上,而 z-index 值為負數的物件在其之下。

簡單演示

<div style="width:200px;height:200px;background-color:#0e0;"></div>
    <div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;"><div>

兩個DIV,第二個向上移動50px,正常情況應該是這樣的

image

第二個div遮住了第一個div,對第二個新增z-index屬性

<div style="width:200px;height:200px;background-color:#0e0;"></div>
    <div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;z-index:-5;"><div>

結果就會變成這個樣子,z-index 最簡單的應用就是這樣

image 

只對定位元素有效

z-index屬性適用於定位元素(position屬性值為 relative 或 absolute 或 fixed的物件),用來確定定位元素在垂直於螢幕方向(稱為Z軸)上的層疊順序,也就是說如果元素是沒有定位的,對其設定的z-index會是無效的。

<div style="width:200px;height:200px;background-color:#0e0;z-index:30"></div>
<div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;z-index:10;"><div>

 

 

 

 

雖然第一個div的z-index比第二個div大,但是由於第一個div未定位,其z-index屬性未起作用,所以仍然會被第二個div覆蓋。

image

相同z-index誰上誰下

相同的z-index其實有兩種情況

1.如果兩個元素都沒有定位發生位置重合現象或者兩個都已定位元素且z-index相同發生位置重合現象,那麼按文件流順序,後面的覆蓋前面的。

<div style="position:relative;width:200px;height:200px;background-color:#0e0;"></div>
<div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;"><div>

image

2.如果兩個元素都沒有設定z-index,使用預設值,一個定位一個沒有定位,那麼定位元素覆蓋未定位元素

<div style="position:relative;top:50px;width:200px;height:200px;background-color:#0e0;"></div>
<div style=" width:100px;height:100px;background-color:#00e;"><div>

image

父子關係處理

如果父元素z-index有效,那麼子元素無論是否設定z-index都和父元素一致,會在父元素上方

<div style="position:relative;width:200px;height:200px;background-color:#0e0;z-index:10;">
        <div style="position:relative;width:100px;height:100px;background-color:#00e;z-index:-5;"><div>
    </div>

雖然子元素設定z-index比父元素小,但是子元素仍然出現在父元素上方

image

如果父元素z-index失效(未定位或者使用預設值),那麼定位子元素的z-index設定生效

<div style="position:relative;width:200px;height:200px;background-color:#0e0;">
        <div style="position:relative;width:100px;height:100px;background-color:#00e;z-index:-5;"><div>
</div>

子元素z-index=-5生效,被父元素覆蓋

image

兄弟之間子元素

如果兄弟元素的z-index生效,那麼其子元素覆蓋關係有父元素決定

<div style="position:relative;width:100px;height:100px;background-color:#0e0;z-index:5;">
        <div style="position:relative;width:50px;height:250px;background-color:#00e;z-index:50;"></div>
    </div>

    <div style="position:relative;width:100px;height:100px;background-color:#0e0;z-index:10;margin-top:4px;">
        <div style="position:relative;width:30px;height:150px;background-color:#e0e;z-index:-10;"></div>
    </div>

雖然第一個div的子元素的z-index比較高,但是由於其父元素z-index比第二個div低,所以第一個div子元素會被第二個div及其子元素覆蓋

image

應用

經常會有這樣一種錯誤在table中最後各行一個td放一個div,點選彈出子選單做一些刪除、修改什麼的操作,但是每次彈出的選單都會被下面各行的div覆蓋,像下面這張圖一樣,彈出的選單沒有在頁面最上方。

image

寫個簡單的例子看看

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <style type="text/css" >
            html,body
            {
                height:100%;
                width:100%;
                padding:0;
                margin:0;
            }
            
            .menu
            {
                background-color:#0e0;
                position:relative;
                z-index:10;
            }
            
            .options
            {
                display:none;
                position:absolute;
                top:
                z-index:30;
            }
            
            .options div
            {
                background-color:#00e;
            }
        </style>
    </head>
    <body>
    <table border="1" cellpadding="4px" cellspacing="0">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Options</th>
        </tr>
        <tr>
            <td>Byron</td>
            <td>24</td>
            <td>
                <div class="menu" >
                    <div>Options</div>
                    <div class="options" style="display:block;position:absolute;top:20px;">
                        <div>Opion1</div>
                        <div>Opion2</div>
                        <div>Opion3</div>
                        <div>Opion4</div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>Byron</td>
            <td>24</td>
            <td>
                <div class="menu">
                    <div>Options</div>
                    <div class="options" >
                        <div>Opion1</div>
                        <div>Opion2</div>
                        <div>Opion3</div>
                        <div>Opion4</div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>Byron</td>
            <td>24</td>
            <td>
                <div class="menu">
                    <div>Options</div>
                    <div class="options" >
                        <div>Opion1</div>
                        <div>Opion2</div>
                        <div>Opion3</div>
                        <div>Opion4</div>
                    </div>
                </div>
            </td>
        </tr>
    </table>
    </body>
<html>

 

期望樣式 image 實際樣式 image

 

這時候習慣於增大options 的z-index卻發現於事無補,為什麼呢?因為每個menu的z-index相同,它們的層疊順序按文件流順序,無論子元素z-index調到多大,上面menu的options還是會被下面menu遮蓋。這時候我的做法一般是把options放到外面,所有的menu用一個,使menu與options沒有父子關係,或者乾脆在點選menu的時候把它的z-index調大,這樣其子元素就不會被遮蓋住了。

最後

本文的例子都是以符合W3C的Chrome瀏覽器做驗證,但在IE6,7 z-index的預設值並不是auto而是0,這樣會導致很多奇怪現象,這時候就需要考慮這點了。