Top layer and transformed ancestors
Definition and purpose
The browser top layer places special UI, such as modal dialogs, above ordinary stacking contexts. A transformed ancestor can instead create a local containing block for fixed descendants.
Transformed containing block
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a { transform: translateX(30px); width: 180px; height: 80px; } #child { position: fixed; top: 10px; left: 10px; } </style> </head> <body> <div id="a"> <span id="child">Badge</span> </div> <div id="b">After</div> </body></html>The fixed child is offset from the transformed ancestor rather than the viewport.
Syntax and mental model
A dialog enters the modal top layer through showModal(); the open attribute alone does not make a modal dialog. ::backdrop styles its backdrop. transform: translateX(20px) can establish a fixed containing block; transforms are introduced here and explored later.
Native modal above document
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { position: fixed; inset: 0; z-index: 9999; background: silver; pointer-events: none; } #open { position: relative; z-index: 10000; } dialog::backdrop { background-color: rgba(0, 0, 0, 0.4); } </style> </head> <body> <div id="a">Background</div> <dialog id="d" aria-label="Example dialog"> Dialog <form method="dialog"> <button>Close</button> </form> </dialog> <button id="open" onclick="document.querySelector('dialog').showModal()">Open</button> </body></html>Activate Open; the dialog enters the top layer despite A’s ordinary z-index.
Common mistakes
Do not simulate modality with an enormous z-index. Use native dialog behavior and a clear close control; verify focus, keyboard closing and accessibility.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Stacking contexts and z-index. Next lesson: Flex containers and axes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Top layer and transformed ancestors — knowledge check
Five questions on top layer and transformed ancestors.
How does a native dialog enter the modal top layer?
Which styles the top-layer backdrop?
Does open alone imply a modal dialog?
What may establish a local fixed reference?
Why use native modal dialog instead of z-index alone?