|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title> |
| 6 | + Organizing My Application Layers Using Z-Index Stacking Contexts In CSS |
| 7 | + </title> |
| 8 | + <style type="text/css"> |
| 9 | + .stacking-context { |
| 10 | + /* |
| 11 | + In order to create a stacking context, we have to have to use a non- |
| 12 | + static layout for the trapping element. |
| 13 | + */ |
| 14 | + position: relative ; |
| 15 | + } |
| 16 | + |
| 17 | + .widget { |
| 18 | + background-color: #f0f0f0 ; |
| 19 | + border-radius: 4px 4px 4px 4px ; |
| 20 | + box-shadow: 0px 0px 7px 3px rgba( 0, 0, 0, 0.3 ) ; |
| 21 | + height: 80px ; |
| 22 | + padding: 20px 20px 20px 20px ; |
| 23 | + position: fixed ; |
| 24 | + width: 200px ; |
| 25 | + } |
| 26 | + </style> |
| 27 | +</head> |
| 28 | +<body> |
| 29 | + |
| 30 | + <!-- |
| 31 | + Each stacking context below TRAPS all of its descendant elements in the same |
| 32 | + layer. It doesn't matter what z-index a descendant element uses at this point - |
| 33 | + it will never be able to escape the z-index defined at the stacking context. |
| 34 | + --> |
| 35 | + |
| 36 | + <!-- Main stacking context for all body-level elements. --> |
| 37 | + <div class="stacking-context stacking-context--body" style="z-index: 1 ;"> |
| 38 | + |
| 39 | + <h1> |
| 40 | + Organizing My Application Layers Into Stacking Contexts In CSS |
| 41 | + </h1> |
| 42 | + |
| 43 | + <p> |
| 44 | + It turns out, layering an application over the long-term is challenging, |
| 45 | + especially when you start introducing 3rd-party widgets that add their own |
| 46 | + DOM elements (like Chat widgets and On-boarding widgets). |
| 47 | + </p> |
| 48 | + |
| 49 | + </div> |
| 50 | + |
| 51 | + <!-- |
| 52 | + Stacking context for a "flyout widget". I would likely create a new stacking |
| 53 | + context for each unique "widget" that needs to be layered above the body. |
| 54 | + --> |
| 55 | + <div class="stacking-context stacking-context--flyout" style="z-index: 2 ;"> |
| 56 | + <!-- |
| 57 | + CAUTION: The z-index on this element (99999999999) won't interfere with the |
| 58 | + z-index of an element contained within a different stacking context. |
| 59 | + --> |
| 60 | + <span class="widget" style="top: 80px ; left: 80px ; z-index: 99999999999 ;"> |
| 61 | + This is my fly-out widget. |
| 62 | + </span> |
| 63 | + </div> |
| 64 | + |
| 65 | + <!-- Stacking context for all modal windows. --> |
| 66 | + <div class="stacking-context stacking-context--modal" style="z-index: 3 ;"> |
| 67 | + <!-- |
| 68 | + CAUTION: The z-index on this element (55555555555) won't interfere with the |
| 69 | + z-index of an element contained within a different stacking context. |
| 70 | + --> |
| 71 | + <span class="widget" style="top: 140px ; left: 140px ; z-index: 55555555555 ;"> |
| 72 | + This is my modal widget. |
| 73 | + </span> |
| 74 | + </div> |
| 75 | + |
| 76 | + <!-- Stacking context for all alerts. --> |
| 77 | + <div class="stacking-context stacking-context--alert" style="z-index: 4 ;"> |
| 78 | + <!-- |
| 79 | + CAUTION: The z-index on this element (22222222222) won't interfere with the |
| 80 | + z-index of an element contained within a different stacking context. |
| 81 | + --> |
| 82 | + <span class="widget" style="top: 200px ; left: 200px ; z-index: 22222222222 ;"> |
| 83 | + This is my alert widget. |
| 84 | + </span> |
| 85 | + </div> |
| 86 | + |
| 87 | +</body> |
| 88 | +</html> |
0 commit comments