0

I try to get the width of an element but the returned value is wrong

   let container = document.querySelector('.content')
    console.log(container.offsetWidth) //this will log wrong value 1182px instead of 960px

  console.log(getComputedStyle(container).width) //1182px anyway I don't have any idea from where the value comes

enter image description here

I can not get an idea why the returned value is wrong

fefe
  • 8,467
  • 22
  • 90
  • 168

1 Answers1

0

Try this. This Worked for mebefore getting offset width you can set the width to

let container = document.querySelector('.content')
container.style.width = "fit-content";
console.log(container.offsetWidth);
Maheer Ali
  • 34,163
  • 5
  • 36
  • 62