+ 1

how to get css top value in a var in js ?

i have a div id="name" with absolute position of top:20px ; and left :20px; now i want get its value in variable "positiont" and "positionl" using js how can i get it. i have tried it as-- var mydiv = document.querySelector("#name") ; var postiont = mydiv.style.top ; var positionl = mydiv.style.left ; and console them separately but its not working. please help me out.

29th Jun 2020, 6:57 AM
Divya Mohan
Divya Mohan - avatar
1 Respuesta
+ 4
style method can only get inline css property. To get css perperty from css file or style tag, use getComputedStyle method. To overwrite style, use style method, getComputedStyle is read only.
29th Jun 2020, 7:07 AM
Calviղ
Calviղ - avatar