Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  34] [ 4]  / answers: 1 / hits: 51066  / 13 Years ago, sun, may 22, 2011, 12:00:00

How to set relative 50% 50% position using JS?



I tried:



document.getElementById(id).style.position = relative 50% 50%;


but it don't seems to work...


More From » css

 Answers
57

You have to set them individually.



I made a variable to point to the style object, because we are modifying more than one property and because with() { ... } is considered harmful.



Also, I set the 50% to both properties because of right to left assignment, and because assignment of this string to the two properties isn't a problem (make sure you understand how this works, e.g. var a = b = [] will set a and b to the same Array object, often not desired).



var elementStyle = document.getElementById(id).style;

elementStyle.position = relative;

elementStyle.top = elementStyle.left = 50%;


jsFiddle.


[#92121] Thursday, May 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
everardo

Total Points: 406
Total Questions: 104
Total Answers: 92

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
;