Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.innerWidth vs document.documentElement.clientWidth

Regarding window.innerWidth and document.documentElement.clientWidth,

  1. Webkit (Chrome / Safari) claims innerWidth is smaller than clientWidth.

  2. Trident and Presto claim innerWidth is bigger than clientWidth.

  3. Gecko claims innerWidth is the same size as clientWidth.

What is the correct behavior stated by W3C (or silimar "authority")?

Test Script (on JSFiddle) (on GoogleHost):

setInterval(function() {    var inner_w = window.innerWidth;    var inner_h = window.innerHeight;    var client_w = document.documentElement.clientWidth;    var client_h = document.documentElement.clientHeight;    var debug_msg = "inner: " + inner_w + "-" + inner_h + "<br>client: " + client_w + "-" + client_h;    document.getElementById("d").innerHTML = debug_msg;    document.title = debug_msg;    document.body.style.background = (client_w === inner_w && client_h === inner_h ? "green" : "red");  }, 60);
<div id="d"></div>

(Run the snippet in full page mode and un-maximize or "restore" the window. Observe debug_msg while dragging the edge of the window to resize it.)

like image 460
Pacerier Avatar asked Aug 04 '11 14:08

Pacerier


People also ask

What is Document body clientWidth?

body. clientWidth for getting the entire width of the browser. It is the core javascript method to fetch the width, this can be also used to fetch the specific element with. If you are using jQuery, can try with $(window). width() to get the window width.

What is innerWidth?

The read-only Window property innerWidth returns the interior width of the window in pixels. This includes the width of the vertical scroll bar, if one is present. More precisely, innerWidth returns the width of the window's layout viewport.

What is documentElement?

documentElement returns the Element that is the root element of the document (for example, the <html> element for HTML documents).

What is innerHeight in Javascript?

The read-only innerHeight property of the Window interface returns the interior height of the window in pixels, including the height of the horizontal scroll bar, if present. The value of innerHeight is taken from the height of the window's layout viewport.


1 Answers

According to the W3C specification (17 March 2016):

The innerWidth attribute must return the viewport width including the size of a rendered scroll bar (if any), or zero if there is no viewport.

...

The clientWidth attribute must run these steps:

  1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.
  2. If the element is the root element and the element's document is not in quirks mode, or if the element is the HTML body element and the element's document is in quirks mode, return the viewport width excluding the size of a rendered scroll bar (if any).
  3. Return the width of the padding edge excluding the width of any rendered scrollbar between the padding edge and the border edge, ignoring any transforms that apply to the element and its ancestors.
like image 171
approxiblue Avatar answered Sep 22 '22 17:09

approxiblue