Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>jQuery offsetParent() – Compare Position Context</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background-color: #f0f4f8; } h1 { color: #1a237e; } p { font-size: 15px; color: #37474f; margin-bottom: 25px; } .outer-box { padding: 20px; background: #e3f2fd; border: 2px solid #90caf9; border-radius: 5px; margin-bottom: 30px; } .positioned { position: relative; } .inner-box { position: absolute; top: 20px; left: 20px; background: #bbdefb; padding: 15px; border-radius: 4px; cursor: pointer; } .highlight { background-color: #ffe082 !important; } .label { font-weight: bold; margin-bottom: 10px; display: block; color: #0d47a1; } .info { margin-top: 20px; padding: 12px 16px; background: #ffffff; border: 1px solid #ccc; border-radius: 4px; color: #263238; font-size: 14px; display: inline-block; } </style> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> </head> <body> <h1>jQuery Traversing – <code>offsetParent()</code> Method</h1> <p>Click the inner boxes to highlight their nearest positioned ancestor using <code>offsetParent()</code>.</p> <!-- Positioned ancestor --> <div class="outer-box positioned" id="positionedBox"> <span class="label">Box A – Positioned Ancestor (relative)</span> <div class="inner-box" id="innerA">Click Me (A)</div> </div> <!-- Non-positioned ancestor --> <div class="outer-box" id="nonPositionedBox"> <span class="label">Box B – Non-Positioned Ancestor (static)</span> <div class="inner-box" id="innerB">Click Me (B)</div> </div> <div class="info" id="infoBox">Click any inner box to identify its offset parent.</div> <script> $(function () { $('.inner-box').click(function () { $('.outer-box').removeClass('highlight'); const offsetParent = $(this).offsetParent(); offsetParent.addClass('highlight'); const id = offsetParent.attr('id'); $('#infoBox').text( id ? 'Offset parent: #' + id : 'No positioned ancestor found' ); }); }); </script> </body> </html>
Editor Commands
Ctrl
F
Find
Ctrl
G
Find Next
Ctrl
⇧
G
Find Previous
Ctrl
⇧
F
Find & Replace
Ctrl
]
Indent Code Right
Ctrl
[
Indent Code Left
⇧
Tab
Auto Indent Code
Ctrl
/
Line Comment
Ctrl
Alt
/
Block Comment
TutsInsider Actions
Alt
S
Run Code
Ctrl
C
Copy Editor Code
Ctrl
A
Select All Editor Code
Ctrl
S
Save/Download
Ctrl
Alt
X
Rotate
Ctrl
Alt
T
Switch Theme
Ctrl
⇧
C
Open Console
Esc
Close Console