跳到主要內容

[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.

    回覆刪除

張貼留言

這個網誌中的熱門文章

Vibe Coding:為什麼 Junior 更快上手?Senior 要如何追趕?

現象層面(市場觀察) 最近有篇文章討論 junior & senior 開發者在 AI 時代的角色轉變,非常熱門。 身為 Cympack 產品開發團隊 ,我們也一直關注這個議題,在閱讀這篇文章時觀察到一些有趣的現象,對我們來說,這正好反映出 AI 正在改變開發生態,junior 借力 AI 快速成長、senior 則需要在 「架構思維」 與 「多 agent 協作」 中找到新定位,其中有些啟發(insight) 可以跟大家分享。 為什麼 Junior 更容易上手 vibe coding? 心智負擔低 → Junior 沒有太多傳統 code workflow 的框架包袱 敢於嘗鮮 → Gen Z / 年輕工程師天生習慣用 prompt-based 工具、跟 LLM 互動 少「優雅程式設計」的束縛 → 不太糾結「這樣寫會不會不夠優雅」,反而 embrace 快速迭代、快速出成果 反觀 Senior: 熟悉大型系統設計 有豐富的「工程正統流程」知識(架構設計、測試策略、效能優化、設計模式) 對 AI 生成 code 的品質 / 維護性通常比較保留 部分 10+ 年資深工程師,對 prompt engineering 沒那麼熟練,還在觀望 技能面(未來的關鍵能力) Vibe coding 本質上 = prompt engineering + AI co-pilot 管理能力 能力項目 誰目前比較有優勢? Prompt 撰寫 / AI 互動 Junior 較強(熟悉 chat-based 流程) 系統設計 / 架構把關 Senior 較強 AI 生成 code 驗證 / Bug 察覺能力 Senior 較強(能看出潛在問題) 快速疊代 / Hackathon 式開發 Junior 較強 長期維護性 / 穩定性 Senior 較強 總結 Junior 確實更快適應 vibe coding,並且更習慣以 「chat-based coding」 的工作流開發。 Senior 擁有驗證 AI 產物與系統設計的深度能力,但若不主動練習 vibe coding,長期會逐漸落後於新一波開發潮流。 就如同在 GAI 技術年會分享,希望帶給各位的感受, 『與 AI 協...

RAG 和 Prompt 原理超簡單解說!想知道 AI 怎麼找答案看這篇

這篇文章是給對於你已經開始使用所謂的 ChatGPT / Claude / Gemini 之類的 AI 服務,甚至是 Siri (嘿丟,他也是一種 AI 應用服務喔) 簡單來說是非 技術人員, PM,小白,想要趕快惡補的人 ,直接花十分鐘可以看完的一篇科普業配文章。 或者是概念僅止於,AI 這東西會幻想,會有誤差,會對於生活有些幫助但沒有幫助的人們,做個簡單又不是太簡單的介紹,希望用一個非常入門的方式讓你們有個了解。 當然,這篇文章目的很簡單, 就是引流 ,如果你身邊有已經對於 Web 技術開發的人員,歡迎報名分享給他,年末出國不如學一技在身,參加今年我們舉辦最後一場 RAG 實作工作坊,報名連結 , https://exma.kktix.cc/events/ai-for-dev-course-rag-2 注意: 接下來每個大段落結束都會有一段工商導入,但文章絕對精彩,請注意! 為了讓各位容易想像,我們將整個世界的資訊,先濃縮到這本『西遊記』的世界觀當中,我們整個世界都在這個 『西遊記』 ,而 大型語言模型 我們用 『書精靈』 來描述。 PS. 我們先預設各位,應該都有聽過,西遊記!如果沒有聽過西遊記的,請右轉出去,謝謝! 先來談談向量 在《西遊記》的世界裡,我們可以把 向量想像成一種「內容座標」 ,讓系統知道每個角色、場景、法術等的 「位置」和「距離」 。向量幫助語言模型知道不同內容之間的關聯程度。 向量就像內容的「距離」和「位置」 比方說,唐三藏的 「位置」(向量)會接近「佛經」和「取經」 的概念,因為他一路上都是為了取經而前進。孫悟空的 向量位置則會更靠近「金箍棒」和「七十二變」 這些概念,因為這些是他的特徵。 相似內容靠得更近:像「佛經」和「取經」會靠近唐三藏的向量,因為它們彼此有很強的關聯。 相差較大內容會離得較遠:像「取經」和「妖怪」「妖怪的寶藏」就距離比較遠,因為妖怪的寶藏和取經的目標關聯性不大。 是誰決定的這些位置? 簡單來說,這些位置和關係是模型自己學出來的。語言模型會閱讀大量的資料和這世界觀的資訊,觀察哪些詞語經常一起出現,根據「共同出現的頻率」來決定它們的關係,並且自動生成向量。例如: 如果模型看到 「唐三藏」 總是和 「取經」 一起出現,它就會讓「唐三藏」的向量靠近「取經」。 ...

Vibe Coding 協作到自建 Dev Agent?從 Claude / Codex 到 OpenHands

過去一年,越來越多工程師開始 把 AI 真正帶進工作流程 。從一開始用 ChatGPT、Claude 來問語法問題,到後來很多人愛上 Cursor,直接在編輯器裡讓 AI 幫忙改 code、補 test case、甚至自動整理 PR。這樣的開發體驗,已經大大改變了我們寫程式的方式。 更現實的是,在很多企業內部、政府單位、或涉及機密資料的專案裡, 其實根本不能直接用 Cursor 或雲端 LLM 工具。   畢竟這些服務通常會把資料傳到雲端模型做處理,萬一專案裡有未公開的技術、敏感客戶資料,或是受限於法規 (像金融、醫療、政府標案) ,直接用雲端 AI 工具就會踩 紅線 。  因此,許多團隊反而更希望 「自己架一套 Dev Agent」 ,可以在內網執行,資料完全掌握在自己手上,該整合的內部工具、該讀的私有 repo、該串的 CI/CD pipeline,全部客製化、安全可控。 這時候,像 OpenHands 這樣的開源 Dev Agent 框架就特別有價值。它的出發點不是單純的 AI 助手,而是讓你能夠打造出一個真的可以跑在自己環境裡、可以理解整個開發流程的 AI 工程師。從建置到部署,從 CLI 操作到瀏覽器查詢, 從多檔案編輯到自動測試,全部都能自己完成,甚至還能針對不同專案調整專屬的工作流。 對很多開始探索 AI 協作開發的團隊來說,這是一條 從 「AI 幫你寫一段程式」,走向「AI 幫你解決一整個任務」 的進化路徑。而且,還是在可控、可自定義、安全的環境裡完成的。 🧩 主要概述 OpenHands 是由 All‑Hands AI 開發的開源「軟體開發代理人平台」,能模仿人類工程師從建立程式、修改程式碼、執行指令,到瀏覽網頁、呼叫 API……等一整套開發流程 它提供雲端(OpenHands Cloud)與本地 Docker 運行版本,用戶能配置 LLM(如 Claude、OpenAI、Gemini…) 📚 核心特性與怎麼使用 代理人的工具能力 支援代碼編輯、命令行、執行環境、網頁瀏覽、API 呼叫—接近人類開發者完整技能。其中 OpenHands Cloud 版本提供 $50 試用額度讓大家方便使用,又或者如果自己本機有 docker 的話,可以自己Local 版本透過 Docker 自架環境。 ...