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 ?

1
  • 3
    400 what? Lengths in CSS always require a unit (unless the value happens to be 0.) Commented May 10, 2021 at 12:08

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.