add multiple styles to element in javascript

To add multiple styles to an element in JavaScript, the style property of the element can be manipulated. It is important to note that the CSS styles should be in camelCase format instead of hyphen-separated format.

Here's an example code snippet that demonstrates how to add multiple styles to an element:

<div id="myDiv">Hello world!</div>
35 chars
2 lines
index.tsx
const myDiv = document.getElementById("myDiv");
myDiv.style.color = "red";
myDiv.style.backgroundColor = "yellow";
myDiv.style.fontSize = "24px";
146 chars
5 lines

In the above code, we first grab the element using document.getElementById(), and then we set various styles using the style property. Each style is set by accessing a property of the style object, using camelCase CSS property names.

So in summary, to add multiple styles to an element in JavaScript, we simply access the style property of the element and set the desired styles as individual properties of that object.

related categories

gistlibby LogSnag