Home / Learn React / Portals: Render Outside the Parent

Portals: Render Outside the Parent

Modals, tooltips and dropdowns must not be cut off by a parent with overflow: hidden. A portal renders the children into another place in the DOM, but keeps them in the React tree.

In shortcreatePortal(children, domNode) draws the children inside domNode, usually document.body.

Theory in plain words

Same React tree, different DOM placeReact treeCard > Modalprops and context workevents bubble to CardReal DOMModal node is in bodynot clipped by Cardz-index is simple
Same React tree, different DOM place

What it is

createPortal(jsx, container) renders JSX into a DOM node that is outside the normal parent. The component stays in the same place in the React tree, so props, state and context work as usual.

Why we use it

CSS problems such as overflow: hidden, z-index and transforms on a parent can clip or hide a modal that is rendered inside it. A portal places it at the top of the page where nothing can cut it.

How it works

React puts the real DOM nodes in the target container. Events still bubble through the React tree, so a click inside a portal reaches the React parent's handlers.

Where it is used

  • Modals and dialogs
  • Tooltips, popovers and dropdown menus
  • Toast notifications
  • Full screen overlays
A modal with a portalTry it Yourself

The parent has overflow hidden and a tiny height. The modal still covers the whole page because it lives in document.body.

Tip:For real modals prefer the native <dialog> element (focus handling is built in) or a tested library such as Radix UI. Always return focus to the button that opened the modal.

Quick quiz

1. What does createPortal do?

2. A click inside a portal bubbles to...

3. Typical use of portals?

Common questions

What does createPortal do?

Renders children into a different DOM node. The React tree stays the same.

Typical use of portals?

Modals and tooltips. They escape overflow and z-index problems.