Categories
JavaScript Answers

How to style element create with createElement with JavaScript?

Spread the love

Sometimes, we want to style element create with createElement with JavaScript.

In this article, we’ll look at how to style element create with createElement with JavaScript.

How to style element create with createElement with JavaScript?

To style element create with createElement with JavaScript, we set the style.cssText property.

For instance, we write

const obj = document.createElement("div");
obj.id = "img";
obj.style.cssText = `
  position: absolute;
  top: 300px;
  left: 300px;
  width: 200px;
  height: 200px;
  -moz-border-radius: 100px;
  border: 1px solid #ddd;
  -moz-box-shadow: 0px 0px 8px #fff;
  display: none;
`;

to set the style.cssText property of the obj element to a string with the styles we want to apply.

Conclusion

To style element create with createElement with JavaScript, we set the style.cssText property.

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 *