-2
let a = document.createElement("div");
a.style.width = 400;
a.style.height = 400;
a.style.backgroundColor = "red"; // num : 1
a.innerText = "Hello World ";   // num : 2 

document.body.append(a);

// After I added the "a" element in the dom . Only "num : 1" and "num : 2 " line works but width and height does not work at all . 

Can anyone please help me with this problemt ?

  • 3
    400 _what_? Lengths in CSS _always_ require a unit (unless the value happens to be 0.) – CBroe May 10 '21 at 12:08

1 Answers1

1

You need to use a unit in order to declare a width or height. Examples of units are 'px', 'em' and '%'. The following should work:

a.style.width = 400 + 'px';
a.style.height = 400 + 'px';

Alternatively, you could try

a.setAttribute("style", "width: 400px, height: 400px");

Check this stackoverflow post for more info.