createPortal(children, domNode) renders children into a different DOM node (often a container under document.body) while keeping them in the same place in the React tree. Use it for UI that must escape the parent's CSS clipping or stacking context, such as modals, toasts, tooltips, popovers and dropdown menus. Context, state and synthetic events still flow through the React tree, not the DOM tree, so a click inside a portal bubbles to React ancestors even though the DOM parent is body.
import { createPortal } from 'react-dom';
function Modal({ open, onClose, title, children }) {
if (!open) return null;
return createPortal(
<div className="backdrop" onClick={onClose}>
<div
role="dialog"
aria-modal="true"
aria-labelledby="modal-title"
onClick={e => e.stopPropagation()}
>
<h2 id="modal-title">{title}</h2>
{children}
</div>
</div>,
document.body
);
}
Even if <Modal> is rendered deep inside a card with overflow: hidden and transform (which creates a containing block for fixed elements), the dialog appears above everything.
overflow: hidden/auto on an ancestor clips absolutely positioned children.transform, filter, opacity < 1 or z-index + position traps descendants, so no z-index can lift them above siblings of that ancestor.position: fixed is relative to an ancestor with transform/filter, not the viewport.Portaling to body sidesteps all three.
<div onClick={() => console.log('card clicked')}>
<Modal open>…</Modal>
</div>
Clicking inside the modal logs "card clicked". That's usually convenient (a menu's parent can observe clicks) but can surprise you. Stop propagation inside the portal if needed. Native DOM listeners (addEventListener) follow the DOM tree instead.
A portal moves DOM, not focus or semantics. For modals you must:
Escape.inert attribute on the app root, or aria-modal + focus trap).The native <dialog> element with showModal() renders in the browser's top layer, giving you stacking, inertness and Escape handling for free. It's often a better base than a hand-rolled portal modal today, and the Popover API covers many tooltip/menu cases.
document doesn't exist on the server. Render portals only after mount, or target a container that's in the server HTML.contains() treat portal content as outside its React parent. Check both trees or use refs on the portal content.Escape closes.<dialog>, the top layer, and the Popover API.Spotted a mistake or a rights concern? Report an issue