Categories
JavaScript Answers

How to set top and left CSS attributes with JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.