0

I have tried the following and gotten the viewport width:

document.documentElement.clientWidth

and

window.innerWidth

However, what I want to get is the width of the content including overflow, for example if I had an image on the page <img src=".." style="width: 4000px !important;" />. In other words, the entire scrollable width. How would I do this?

1
  • The Element.scrollWidth read-only property is a measurement of the width of an element's content, including content not visible on the screen due to overflow. If the element's content can fit without a need for horizontal scrollbar, its scrollWidth is equal to clientWidth. Commented May 13, 2019 at 11:23

2 Answers 2

2

Use scrollWidth for the body scrollWidth docs . Also please don't use !important . Only use it as a last resort

console.log(document.body.scrollWidth)
 <img src="https://via.placeholder.com/150" style="width: 4000px;" />

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

Comments

-1

You can do this with Element.offsetWidth

document.documentElement.offsetWidth

You can read more about offsetWidth here

2 Comments

Why is this downvoted? I am kind of curious as well and this seemed a solution I stumbled upon as well.
@Barrosy Just check it and see if it works. jsfiddle.net/c1p2k3of

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.