MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Breaking Out of DOM Hierarchies with createPortal0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
11.1Breaking Out of DOM Hierarchies with createPortal11.2Building Modal Dialogs and Overlays with Portals11.3Event Bubbling Through Portals Explained
HomeTutorialsReact.js for IntermediateBreaking Out of DOM Hierarchies with createPortal
Building Modal Dialogs and Overlays with Portals

Breaking Out of DOM Hierarchies with createPortal

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The CSS Clipping and Z-Index Dilemma

In traditional React, a component's JSX markup is rendered directly inside its parent DOM element in the tree.

Normally, this is desirable. However, when building Modals, Tooltips, Dropdowns, and Toast Notifications, standard DOM hierarchy creates severe layout bugs:

  1. 1
    overflow: hidden Clipping: If any ancestor container in the tree has overflow: hidden or overflow: scroll, any modal or tooltip extending beyond the parent container's boundaries will be harshly clipped and cut off!
  2. 2
    z-index Stacking Context Collisions: Even if you set z-index: 999999 on a modal, if its parent container is trapped in a lower stacking context (z-index: 1), the modal will stay buried beneath other elements on the page!
1Problem:
2<div style="overflow: hidden"> <-- Clips everything outside its box!
3 <DashboardCard>
4 <Modal> <-- CUT OFF & INVISIBLE!

2. What is createPortal?

React Portals provide a first-class way to render children into a different DOM node that exists entirely outside the DOM hierarchy of the parent component.

With a portal, your component can live logically in the React component tree (sharing props, state, and context), while its physical HTML DOM nodes are injected directly into document.body or a dedicated <div id="modal-root">.

$$\text{ReactDOM.createPortal}(\text{children}, \text{domNode})$$

1React Component Tree (Logical Hierarchy):
2<App> ──> <Dashboard> ──> <UserProfile> ──> <ModalDialog> (Receives props & state!)
3 │
4 createPortal ▼
5Physical Browser DOM (Rendered Output):
6<body>
7 <div id="root"> (Standard App Mount) </div>
8 <div id="modal-root">
9 <div className="modal-backdrop">...</div> (Rendered here at the very top of the DOM!)
10 </div>
11</body>

3. Implementing a React Portal Primitive

In your index.html, declare a designated portal target right after the root:

1<body>
2 <div id="root"></div>
3 <div id="portal-root"></div> <!-- Dedicated portal mount target -->
4</body>

In your React component, use createPortal from react-dom:

1import React from 'react';
2import { createPortal } from 'react-dom';
3 
4export default function Portal({ children }) {
5 const mountTarget = document.getElementById('portal-root') || document.body;
6 
7 // Teleports children to mountTarget in the real DOM!
8 return createPortal(children, mountTarget);
9}

4. Benefits of React Portals

  • Immune to overflow: hidden: Because the DOM node is attached directly to #portal-root at the document root, no ancestor containers can clip its boundaries.
  • Predictable Z-Index Stacking: Modals and tooltips live at the very top level of the body, guaranteeing correct overlay ordering.
  • Accessibility & Focus Trapping: Screen readers and keyboard navigation utilities can manage focus cleanly on top-level overlays.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What problem do React Portals solve?
Question 2
What package must createPortal be imported from?
Question 3
What are the two mandatory arguments passed to createPortal(children, domNode)?
Question 4
Does a component rendered through a portal lose access to React Context provided by its logical parents?
Question 5
What CSS property on a parent container frequently cuts off child dropdowns or tooltips, requiring a portal?
Next Lesson

Building Modal Dialogs and Overlays with Portals

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Reporting Client Errors to Monitoring ServicesBuilding Modal Dialogs and Overlays with Portals

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext