
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element offsetParent Property
The HTML DOM Element offsetParent property is used to retrieve the nearest offset parent (or ancestor) of an element. The offset parent is the "closest ancestor" element that has a position other than static, such as relative, absolute, or fixed.
If an element is hidden or not visible (set as display = "none"), it will return null. If no ancestor is present, it returns the document HTML element.
Syntax
Following is the syntax of the HTML DOM Element offsetParent property −
element.offsetParent
Parameters
Since this is a property, it will not accept any parameter.
Return Value
This property returns an element that affects the positioning of another element. If the element is hidden, it returns null.
Example 1: Displaying the Offset Parent of an Element
The following is the basic example of the HTML DOM Element offsetParent property. It retrieves the of the <div> element −
<!DOCTYPE html> <html lang="en"> <head> <style> .c { position: relative; width: 300px; height: 150px; border: 1px solid black; } .b { position: absolute; top: 30px; left: 30px; width: 200px; padding: 10px; background-color: lightblue; } </style> </head> <body> <h3>HTML DOM Element offsetHeight Property</h3> <div class="c"> <div class="b" id="e">Example Element</div> </div> <br> <button onclick="s()">Show offsetParent</button> <p id="o"></p> <script> function s() { const e = document.getElementById('e'); const p = e.offsetParent; const o = document.getElementById('o'); o.textContent = `Offset Parent: ${p ? p.tagName : 'null'}`; } </script> </body> </html>
The above program displays the offset parent of the "div" element.
Example 2: Handling Hidden Element
Following is another example of the HTML DOM Element offsetParent property. We use this property to retrieve the offset parent of a hidden element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element offsetParent</title> <style> .con { position: relative; width: 300px; height: 200px; background-color: green; border: 1px solid black; } .hid { display: none; } button{ padding: 10px; } </style> </head> <body> <h3>HTML DOM Element offsetParent Property</h3> <p>Handles when the element is hidden...</p> <div class="con"> <div class="hid" id="ele">Hide Element</div> </div> <button onclick="offp()">Show offsetParent</button> <script> function offp() { const ele = document.getElementById('ele'); const Pt = ele.Pt; alert(`Offset Parent: ${Pt ? Pt.tagName : 'null'}`); } </script> </body> </html>
After executing the above program, it will display "null" as the offset parent of a hidden element.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
offsetParent | Yes | Yes | Yes | Yes | Yes |