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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *