/    Sign up×
Community /Pin to ProfileBookmark

Getting the position of a centered div…

I’m trying to figure out how to find the position of a centered div. I tried document.getElementById(id).style.top and document.getComputedStyle(theScreen, null).top neither of which worked. I can’t seem to get the position of this div unless I set it explicitly which doesn’t let me have it centered using margin: 0px auto. Is there another way to find the position of this thing? Thanks,

-Ryan

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@smeezekittyMay 23.2007 — try document.getElementById(id).style.center
Copy linkTweet thisAlerts:
@mrhooMay 23.2007 — [CODE]function locate(who){
var L=0, T=0; // L is left, T is top (integers)
while(who){
L+= who.offsetLeft || 0;
T+= who.offsetTop || 0;
if(who== document.body) break;
who= who.parentNode;
}
var A= [L, T];
return A;
}[/CODE]

//test case:

var who= document.getElementsByTagName('*');

who= who[who.length-1];

alert(locate(who));
×

Success!

Help @randomfool spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...