Sometimes, we want to set top and left CSS attributes with JavaScript.
In this article, we’ll look at how to set top and left CSS attributes with JavaScript.
How to set top and left CSS attributes with JavaScript?
To set top and left CSS attributes with JavaScript, we can call the setProperty method.
For instance, we write
document.getElementById("divName").style.setProperty("top", "100px");
to select the element with getElementById.
Then we call its style.setProperty method with the CSS property name and value to set.
As a result, we set the top style to 100px.
Conclusion
To set top and left CSS attributes with JavaScript, we can call the setProperty method.