tkane
tkane

Reputation: 1767

How would you convert pixel dimensions and pixel position of a div into percentage?

I've got a fixed pixel positioned div element that I'd like to take its size and position and convert into relative percentage. How would you do that?

Upvotes: 0

Views: 280

Answers (1)

Snuffleupagus
Snuffleupagus

Reputation: 6725

You want to use parentNode to access the parent, compare their sizes and voila.

var foo = document.getElementById('foo');
var bar = document.getElementById('foo').parentNode;
console.log('Width: ' + (foo.offsetWidth / bar.offsetWidth) + '%')

Upvotes: 1

Related Questions