HTMLElement: offsetParent property
HTMLElement: offsetParent property
The HTMLElement.offsetParent
read-only property returns a reference to the element which is the closest (nearest in the containment hierarchy) positioned ancestor element.
A positioned ancestor is either:
- an element with a non-static position, or
td
,th
,table
in case the element itself is static positioned.
If there is no positioned ancestor element, the body
is returned.
Note: offsetParent
returns null
in the following situations:
- The element or any ancestor has the
display
property set tonone
. - The element has the
position
property set tofixed
(Firefox returns<body>
). - The element is
<body>
or<html>
.
offsetParent
is useful because offsetTop
and offsetLeft
are relative to its padding edge.
How can I find which element was used as the current element's top 50% position absolute
To find the parent element that a positioned child element is using as its reference, you can use JavaScript:
js
Copy code
let positionedElement = document.querySelector(".positioned");
let offsetParent = positionedElement.offsetParent;
The offsetParent
property will return the closest positioned ancestor that the absolutely positioned element is using to offset itself.
A positioned element is one that has a position
value other than the default static
. This would include relative
, absolute
, fixed
, and sticky
.
So in this example:
html
Copy code
<div class="parent" style="position: relative">
<div class="positioned" style="position: absolute; top: 50%">...</div>
</div>
The .positioned
div is positioned absolutely at 50% from the top.
Its offsetParent
will be the .parent
div, since that has position: relative
applied.
So using offsetParent
allows you to dynamically find that positioned ancestor.
loop to find all positioned ancestors of element with id "userListTable_processing"
function findPositionedAncestors(element) {
const positionedAncestors = [];
let currentElement = element;
while (currentElement !== null) {
const computedStyle = window.getComputedStyle(currentElement);
if (computedStyle.position !== 'static') {
positionedAncestors.push(currentElement);
}
currentElement = currentElement.parentElement;
}
return positionedAncestors;
}
const userListTableProcessingElement = document.getElementById('userListTable_processing');
const positionedAncestors = findPositionedAncestors(userListTableProcessingElement);
console.log(positionedAncestors);
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-11-27 .NET Standard library output doesn't include nuget dependencies
2020-11-27 SHA-256 hash of null input?
2018-11-27 xunit输出output到控制台
2018-11-27 Getting Started with xUnit.net (desktop)
2017-11-27 sql server 数据库展开变慢
2017-11-27 kentico中的page template的使用
2017-11-27 asp.net website 单独编译某个页面,连带编译app_code