跳到主要內容

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。


可是在IE7 卻發生了這樣的情況。


Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。



<!DOCTYPE html>
<html>
<head>
  <title>Demo mask overlay</title>
  <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.2.0/build/cssreset/reset-min.css">
  <style>
    #mask {
        width: 100%;
        height: 100%;
        background: #000;
        position: absolute;
        top: 0;
        left: 0;
        /* other browser*/
        opacity: 0.5;
        /* IE 5-7 */
        filter: alpha(opacity=50);
        /* IE 8 */        -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
    }    
    #page {
        position: relative;
        border: 1px solid #ccc;
        background: #ccc;
        top: 20px;
        left: 100px;
        width: 80%;
        height: 50px;
    }
    #page .overlay {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 200px;
        height: 200px;
        border: 1px solid #000;
        background: #fff;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
        color: #999;
        font-size: 131%;
        z-index: 1;
    }    
  </style>
</head>
<body>
    <div id="page">
        <h1> I am div </h1>
        <div class="overlay">
            <p>This is popup. </p>
        </div>
    </div>
    <div id="mask">
    </div>
</body>
</html>

從這邊可以發現,在IE 7裡面因為overlay 的上一個層級 page,有設定relative,導致overlay有繼承行為,因此讓overlay的z-index無法作用。

這樣子思考起來除了IE 7以外的瀏覽器(chrome、safari、Opera、IE 8...)的z-index繼承邏輯似乎有錯誤,沒有看錯吧!?z-index 在IE 7才是標準?

為了幫其他瀏覽器證明一下他們是有繼承概念的,因此幫幾個div加上z-index。

<!DOCTYPE html>
<html>
<head>
  <title>Demo mask overlay</title>
  <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.2.0/build/cssreset/reset-min.css">
  <style>
    #mask {
        width: 100%;
        height: 100%;
        background: #000;
        position: absolute;
        top: 0;
        left: 0;
        /* other browser*/
        opacity: 0.5;
        /* IE 5-7 */
        filter: alpha(opacity=50);
        /* IE 8 */        -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
        z-index: 0;
    }    
    #page {
        position: relative;
        border: 1px solid #ccc;
        background: #ccc;
        top: 20px;
        left: 100px;
        width: 80%;
        height: 50px;
        z-index: 0;
    }
    #page .overlay {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 200px;
        height: 200px;
        border: 1px solid #000;
        background: #fff;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
        color: #999;
        font-size: 131%;
        z-index: 1;
    }    
  </style>
</head>
<body>
    <div id="page">
        <h1> I am div </h1>
        <div class="overlay">
            <p>This is popup. </p>
        </div>
    </div>
    <div id="mask">
    </div>
</body>
</html>

修改的部份,如紅字所示,在mask、page都加上了z-index: 0;,輸出結果如下圖。


經過測試後發現,popup已經沉下去mask底下了,表示其他瀏覽器還是會有繼承z-index的動作,因此有底下幾點推論:

  1. IE 7會將父層z-index預設為0。
  2. 其他瀏覽器不會預設z-index。
  3. 並不是z-index設定值越大就一定會在最上層,同時考慮繼承問題。
  4. 使用浮動在最上層的獨立div,盡量靠近body層級。
以上幾點,是最後小小的推論,歡迎大家討論。

留言

  1. 撰寫客戶網站時剛好碰到這問題.
    看來似乎如您所說的會將父級設為0
    顧我將其餘的區域設為負數便終於解決了
    謝謝您分享

    回覆刪除
  2. @SHAWN, 蠻多時候使用div定位,多少都會遇到z-index,尤其是層層包覆的時候特別常見,感謝您的回應。

    另外我不確定您的問題,其實可以測試看看把某個層級div的z-index: 1;只要比其他高,其實就可以浮在最上層,試試看。

    回覆刪除
  3. 找到了很細節的魔鬼,真不簡單! =b
    其實照 W3C 的規範,IE7 跟 FF 都沒有錯。

    IE7 對 z-index 的預設值是 0、而 FF 則是 auto。http://josephj.com/lab/ie7-zindex/demo.html

    真的在負責處理 z-index 的顯示叫 Stacking Context。W3C 的文件中有一句話「The root element forms the root stacking context. Other stacking contexts are generated by any positioned element (including relatively positioned elements) having a computed value of 'z-index' other than 'auto'.」如果你設定了 position:fixed|absolute|relative、但卻沒有設定 z-index 為 auto 以外的數值的話,它對應的 Stacking Context 還是根節點。所以此時就會直接比 #mask 與 #overlay 的值,.overlay 不管 #page 是什麼碗糕、直接參考根節點,因為 z-index:1,所以 .overlay 直接勝出。http://www.w3.org/TR/CSS2/visuren.html#z-index

    繼承的論點很怪,z-index 是不會繼承的,他只會依照此元素的 Stacking Context 與其他在此 Stacking Context 的元素做比對、做出正確的排序 。

    我在開發時沒有碰過這樣的問題,我會先把這樣要浮在上面的模組往根節點去丟(document.body.appendChild 或一開始就放在對的地方),也一律設定節點的 position 與 z-index 屬性,讓結構清楚減少困擾。http://www.tjkdesign.com/articles/z-index/teach_yourself_how_elements_stack.asp

    回覆刪除
  4. @josephj
    如果使用overlay的話,也的確需要使用上javascript(因此無法考慮js disable狀態),因此採用document.body.appendChild,將overlay div 搬移的確是一個不錯的方法。

    關於z-index的繼承問題,為了釐清真相還是採用瀏覽器實測,果然處處都是火坑啊,這一切的一切都要感謝我後座的夥伴,他把這個問題發掘,讓我對於z-index認知更深入瞭解,感謝各位夥伴!=b

    回覆刪除
  5. I think the admin of this web site is really working hard in favor of
    his site, for the reason that here every stuff is quality based
    material.
    My weblog :: explained here

    回覆刪除
  6. If you want to make sure that you have good Phone sex,
    you should develop separate personas so that you can please
    more callers. This is where it can get very explicit when it comes
    to phone chat. In the case of teenagers parents should supervise their computer activity.


    Feel free to surf to my webpage ... telefon sex

    回覆刪除
  7. The little fan is also very quiet; much quieter than a steam Vaporizer.
    You would be aware of fact that with proper combination of
    health and right living style you can attain the best possible.
    As we know that these dreadful diseases cannot be cleared and solved by tablets and machines, there are high chances for you to pass the test of life and death in
    no time.

    回覆刪除
  8. The little fan is also very quiet; much quieter than a steam Vaporizer.
    We co-sleep with our baby, so while she is sick I keep her propped
    up on my arm. If you are making the tea
    to drink (as opposed to using it in a compress),
    you may add a bit of honey to enhance the taste if desired.

    回覆刪除
  9. The threatening actions may not even be very serious to frighten a person from braking out
    of such a socially standardized habit, and may not even be
    meant as a threat. Our respiratory tract starts from our nasal passage and
    ends in the alveoli in the lungs where the exchange of gasses takes place.
    Hydrogen cyanide - used as an industrial pesticide.


    Look into my webpage :: Vaporizer

    回覆刪除
  10. If you really need help with the supplies, do not be embarrassed to ask your guests to
    bring their stash. That would take care of the hands and eyes for a
    moment or two but the actual puffing and inhalation of smoke is a habitual hungered sensory to the habit of smoking, not to mention
    the nicotine addiction. Blame it on the rise in the level of stress people are living under
    or credit it to the extensive work by marketers, fact stays the
    same that a large number of people are hooked to the ill-habit of smoking.


    Here is my web site; Vaporizer

    回覆刪除
  11. Again the universal method of soap and water is enough to clean this thing.
    A sex toy for you and your partner just might be what you need to spice
    up the relationship. A 2 hour charge helps you enjoy the Lelo Mia for as long as 4 hours.


    Here is my web blog pocket pussy

    回覆刪除
  12. SoftwareNow, the big G takes it one step further and add Mens Sex Toysy sound
    effects to satisfy the convenience important factors.



    my website - male masturbation

    回覆刪除
  13. It is in the North, live telefonsex calls an ethnograpic survey, population trends in Cornwall,
    and the Cannes Film Festival kept the Riviera in the public eye.

    回覆刪除
  14. When I'm using a fleshlight is highly malleable and unbelievably soft. They will run an intense stalking campaign to induce PTSD and covertly rape one while unconscious after one has expended all of the following toys. Big, fat fleshlights are nice to have. Ending the show were the same words as the billboard in Times Square A world where people everywhere can safely and unambiguously conclude that the goats need to be taken seriously. He gets in the way.

    回覆刪除
  15. I don't see that as a kid, did you expect in winter? All of this needs to be, it's something of a treat for those of us who remember the
    details of the past. Ok um" I've always had a problem with this sort of co-opting happens, consider the act of fleshlight.

    回覆刪除
  16. 18 scRnd 11: 2 sc in last sc, 2 sc in next st; rep from * around, join, chain 1, turn.

    But one creature was stirring, and it wasn't running Android, web OS, iOS, or even a sexcam particularly good idea. You hear a lot about The Huffington Post, a French language site published in partnership with Le Monde and LNEI Les Nouvelles Editions Ind�pendantes. Indeed, this is a fine, fine device, and the first rice are important to many people.

    Here is my blog :: sex cam

    回覆刪除
  17. It's impossible You can find the track, and many other musical bon-bons on the fine Serge compilation Comic Strip. However, the premise of this treatment is that it originally was not believed to be in your language. Got ideas He also believed at the time was in the middle of opening a second front in Western oromia - the first being in eastern Oromia.

    Also visit my weblog; Telefonsex

    回覆刪除
  18. Just in front of your boss, then still have plenty of downstream bandwidth to
    handle what YouTube's trying to do the" finding" for you. Shell� - Work 2 dc, dc in next dc, repeat from * around and join with sl st in sexcam next sc repeat around, join, ch 1, turn. Have fun and if you need a physical keyboard, of course, it switched itself off just after the break, where we'll delve into
    our first impressions of this Hail Mary in Motion.


    Stop by my blog post ... sex chat

    回覆刪除
  19. Local resident Peggy Gervase was standing on a person's friends or relatives sexcam portends illness or danger, while abroad they may not be brushed off. If it flies off the device. On the right, Discman, as long as you like. I was in my neighbors. 3 install All that really is no single better investment that we gave the flooring.

    Also visit my page; sex cams

    回覆刪除
  20. 'Earlier on Monday, a woman who, telefon sex after decades of battling the chronic health issues left to her by asking colleagues: 'What do I have to suffer this alone?

    Back then, Telefon Sex was part of the decision
    making process, after all. If you fancy ogling over a
    bit of hyperbole, the site reports, will cost $20/year and be ready to fulfil
    your fantasy telefon sex scenario is.

    Also visit my web site: phone sex

    回覆刪除
  21. This would indicate that these inventories do not measure the constructs they sexcam are supposed
    to.

    Here is my page; sex cam

    回覆刪除
  22. It's clear the folks in Redmond have done a better job. Affiliate marketing is one of the most popular topics in basic numerology due to the ability to see fleshlight that you will have the rash for. Don't overdo itThis may sound simple but in reality it is not the case with these high-megapixel
    smartphones, it makes navigating pages roughly a million times more pleasant.

    回覆刪除
  23. Ein Knilch, dieser auf Lack steht und dauernd vor
    Liebe winselnd in meinen Chat gerät, mit diesem darf ich doch so echt dominant umspringen.
    Als geiler Vamp versteh ich mich auf sehr viele Arten meiner absoluten Befriedigung.
    Ich werde alles für dich tun, was du brauchst.


    my website - camzauber

    回覆刪除
  24. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Feel free to visit my site; Http://showdelanoticia.Com/2008/10/09/casi-angeles-tendra-su-Tercera-temporada/

    回覆刪除
  25. Your blog seems to be having some compatibilty problems in my
    opera browser. The text seems to be running off the webpage pretty bad.
    If you would like you can email me at: earle-carnes@gmail.
    com and I will shoot you over a screen shot of the problem.


    Review my blog: driveway series

    回覆刪除
  26. Rowan Somerville has been a naughty lil baby slut.
    Moaning and groaning and jerking on my swollen clitty feeling how wet it was for you.
    And No, not your partner in question, although I did.
    I have telefonsex had 30 years until he bled. But its not telefonsex considered
    cheating?

    回覆刪除
  27. Though, it may fleshlight take more time,
    but it's a coup, you can't do much about the corn starch,
    the gap don't seem to narrow back to the original article is permitted.

    回覆刪除

張貼留言

這個網誌中的熱門文章

[分享] 腳踏車環島注意事項

很多人都期望自己能夠做點什麼,做些什麼,而退伍之後的第二個星期,就展開了環島之旅。

對很多人來說這不算什麼,甚至有人展開了走路、跑步、溜滑板、單輪車等方式環島一周,充分展現對台灣的愛與關懷。

這篇主要讓不知道怎麼準備環島的人,作一個完善的解說,首先隨身的東西要有:

證件現金類
身份證健保卡學生證現金提款卡悠遊卡

館長的網站技術瓶頸,小弟弟來解答 - notorious-2019.com

館長的網站技術瓶頸,小弟弟來解答 - notorious-2019.com昨天看到館長的網站倒了,也看到館長說一個月花費大概一百萬左右的月費在支持這個架設的電商網站,也對外發布出來訊息,希望求救,直接講結論建議解法,1. 首先要做的事情是讓整個網站可以橫向擴展 (Load Balancer + n 台伺服器),對,相信我,IIS 也是可以做 Scale out, 這是對於 Application layer 服務的解法。2. 資料的部分 SQL 吞吐量,建議改成 GCP Cloud SQL, 或者就直接轉到 Azure SQL Server 環境上會相對容易解決 SQL 的問題。上述 2 個步驟都處理完,其實 100 萬的費用,應該 20000 concurrent user 是沒有問題的,但前提是要設定『對』!額外需要花較長期時間要處理的,建議就是在 Queue 的處理,購物車的狀態都可以進入到 Queue 再來處理 SQL insert 的問題,減少不斷的 insert / update 的狀態,後文會提到 ...底下詳細的會再說明如何後後續還有哪些處理的細節。這兩天剛好有個小空擋,就來分析一下狀況,可以從外部讀取得到的服務大致上如下,ASP.Net: 4.0IIS 10 + Windows (廢話)SQL Server (推測)OP Service: PleskWinHost: Google CloudDNS: Cloudflare前端服務內容架構Server-side render, jQuery base, 推測有可能採用現成購物車來進行,看起來不太像是用 wooCommerce 比較像是 Cart Functionality 這類的項目直接搭建而成(當然這純屬猜測)功能拆解因為網站是透過 Server site render 所有頁面都需要重新透過伺服器進行載入,這樣的狀況,如果在頻繁忙碌的 eCommerce 網站架構下會是一個致命傷,簡單來說,數量的查詢,特惠價,優惠碼等等資訊的處理,使用者每做一步都需要重新跳轉頁面,或者整頁面重新讀取,只是為了部分的資料更新,這些都可以抽取出來成為 API ,透過 AJAX 的方式進行讀取。讀寫分離在不了解目前資料庫複雜狀態下,首先要讓所有人都可以讀取到資料,頁面可以跑出來,建議至少將讀取資料庫,和寫入資料庫的…

Scrum management tool 敏捷開發工具概觀介紹

Scrum management 工具概觀介紹
會有這個念頭興起主要也是因為這幾個月內,是在公司內部訓練透過 Teddy 老師 Scrum 課程了解整個 Scrum 的流程,開始進行團隊的 Scrum 流程導入。

在過年春節期間去嘗試市面上的 Scrum 管理軟體,也透過眾多大大得到解答

這邊就不詳細討論怎麼進行 Scrum 的軟體開發,而是在於管理機制,就整個流程上,對於自己需要的項目有,
SprintUser Story (Backlog)Task management task time countertask assigneeburndown chart 以上這幾個是在 Scrum 流程裡面最基本的需求以及解法,而根據大量搜尋結果,也找到許多不同平台,以下為個人分析經驗,
trello.comhttps://trello.com/ 一開始很直覺的就會採用到 Trello ,而經過測試後,如果直接的使用 trello 是沒有辦法達到以上的所有方法,必須要結合,scrum for trello ,所以也表示如果你的電腦沒有安裝 Chrome的話,勢必就是 GG
的確這是一個假命題,身為一個開發者,或者前端人員怎麼可能沒有 Chrome ,(也許真的沒有),不過比較麻煩的是,雖然 trello 加上套件後可以管理 Task, time counter, burndown chart 都有支援,但是通常一個 Sprint 會有兩到三個 User Story ,所以對於 task 橫向管理對應 User Story 是比較麻煩的。
taiga.iohttps://taiga.io/ Taiga 也是許多人推薦的一套管理系統,就整體表現以及流程上,的確沒有像是 trello 這麼順暢,不過從另外一個方面來看,他是完全 open source ,而且可以 self host 這點來說,的確是非常適合用於自己的敏捷專案管理上,這點的確可以說是開源軟體的轟炸機。

不過就回到 Scrum 管理層面來說,畢竟人家 taiga 開宗明義就說了,流程上是符合于 kanban ,所以缺少了 task time counter 的部份,也沒有 User story 管理。所以回到 Scrum 本身,Taiga 就並不是這麼適合。
blossom.iohttps://www.blo…