Sub topic 1
The problem: clipped and buried UI
Prerequisites
This chapter assumes you know the following:
- Function components, JSX, props and
children. useStateanduseEffect.- DOM basics: nodes,
document.bodyand event bubbling. - CSS positioning (
position,z-index) at a working level.
You don't need to know portals yet. This chapter explains the problem they solve, and the next chapters cover the API.
The symptom
You build a modal dialog. It works in a demo page. Then you place it inside a card in the real app, and it is cut off at the card's edge. Or it renders, but a sticky header draws on top of it.
Both failures have the same cause. By default, a React component's output is placed in the DOM exactly where the component appears in the JSX. If a parent's CSS affects its descendants, your modal inherits that effect.
function Card() {
return (
<div className="card">
<h2>Order summary</h2>
<ConfirmDialog />
</div>
);
}Here the dialog's DOM node ends up inside div.card. Any CSS rule on .card that clips or layers its children now applies to the dialog.
Cause 1: clipping by overflow
An element with overflow: hidden, overflow: auto or overflow: scroll clips its descendants to its own box. This holds even when a descendant uses position: absolute, as long as the clipping element is also its containing block or sits between it and the containing block.
.card {
position: relative;
overflow: hidden;
width: 300px;
height: 200px;
}
.dialog {
position: absolute;
top: 0;
left: 0;
width: 500px; /* wider than .card, so the right part is cut off */
}The dialog is 500px wide, but the card is 300px wide. The browser paints only the part inside the card.
Using position: fixed avoids clipping in most cases, because a fixed element is positioned relative to the viewport. There is an exception covered in the next section.
Cause 2: stacking contexts
z-index does not compare every element on the page. It compares elements only within the same stacking context. A stacking context is a self-contained layer group. Inside it, children are ordered by z-index. The group as a whole is then ordered against its siblings.
An element creates a new stacking context in several common situations:
- It has
positionset torelative,absoluteorstickytogether with az-indexother thanauto. - It has
position: fixedorposition: sticky. - It has
opacityless than1. - It has a
transform,filterorperspectivevalue other thannone. - It is a flex or grid child with a
z-indexother thanauto.
The consequence is easy to miss. If your dialog sits inside an ancestor that creates a stacking context with z-index: 1, the dialog's own z-index: 9999 only competes inside that ancestor. A sibling of the ancestor with z-index: 2 is painted above the whole group, dialog included.
.sidebar {
position: relative;
z-index: 1; /* creates a stacking context */
}
.header {
position: sticky;
top: 0;
z-index: 2; /* painted above everything inside .sidebar */
}
.sidebar .dialog {
position: fixed;
z-index: 9999; /* only ranks among .sidebar's descendants */
}The header covers the dialog even though 9999 is much larger than 2.
The exception mentioned earlier also comes from this family of properties. If an ancestor has a transform, filter or perspective, a position: fixed descendant is positioned relative to that ancestor, not the viewport. A modal inside an animated card can therefore appear in the wrong place and be clipped.
Why the usual fixes fall short
| Fix | Why it fails |
|---|---|
| Raise `z-index` | Only works inside one stacking context. |
| Remove `overflow: hidden` | Breaks the layout the parent needed it for, such as rounded corners or scroll areas. |
| Move the dialog higher in the JSX | Couples the component structure to CSS, and the dialog's state often lives in the deeper component. |
| Pass state up and render the dialog at the app root | Requires lifting state and props through many levels, even though only the DOM location is wrong. |
The real problem is that the DOM position and the component position are tied together. You want the dialog's logic to stay in Card, and its DOM node to live somewhere else, such as directly under document.body.
Where this is heading
Placing the DOM node under document.body removes it from every ancestor's overflow and stacking rules, except those that body itself has. React provides this through portals. A portal renders a component's children into a different DOM node while leaving the component in the same place in the React tree. The next chapter explains why that distinction matters, and then shows the createPortal API.
Exercise
A dialog has position: fixed; z-index: 9999 and is rendered inside a div with transform: translateX(0). It appears offset and partly clipped. Why, and what would fix it without changing the CSS of the div?
Show answer
A transform on an ancestor makes that ancestor the containing block for fixed-position descendants, and it also creates a stacking context. The dialog is therefore positioned and clipped relative to the div, not the viewport. Moving the dialog's DOM node outside that div, for example under document.body, fixes it. React portals do this without moving the component in the React tree.
Exercise
A sticky header has z-index: 2. A modal inside a .panel with position: relative; z-index: 1 has z-index: 100. Which is painted on top, and why?
Show answer
The header is on top. The .panel creates a stacking context with z-index: 1, so the modal's z-index: 100 only ranks inside the panel. The panel as a whole (1) is below the header (2).