On http://jsfiddle.net/Xs9e2/ there are two elements, named ONE and TWO. The element ONE is positioned by assigning to the style property of the DOM element using JavaScript. The element TWO is positioned using CSS.
The HTML renders as expected.
There is a button one can press to have the left and top properties of each of the two elements alerted. We only see the properties that were set in JavaScript, not the ones set in CSS.
What is the reason for this exactly? Shouldn’t we be able to read the CSS properties via JavaScript? I may have specified something incorrectly. I know about the “offset” properties and I know about JQuery; I’m just puzzled why this old-fashioned DOM manipulation isn’t working as I would expect. What am I missing?
The
styleattribute is, well, an attribute. That is you can only access the values given in the HTML element’s attributestyle:Styles given with CSS don’t set the element’s attribute, thus you cannot access it with
.style.However, you can use getComputedStyle.