Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Getting the rendered width and height of a box-model DOM node using javascript without actually adding it to the DOM to be displayed is not possible.</p> <p>To prove this, let's walk through how the rendered height and width of a DOM node is calculated internally to the browser. I will reference how WebKit handles this since it is the most commonly used layout engine.</p> <p>As the document is parsed and DOM nodes are added to the "DOM Tree", <code>Renderers</code> are created for the DOM nodes that need to be displayed. This is how the "Render Tree" gets built.</p> <p>Here is an excerpt from an article entitled <a href="https://www.webkit.org/blog/114/webcore-rendering-i-the-basics/" rel="nofollow">"WebCore Rendering I – The Basics"</a> by Dave Hyatt on the official WebKit Blog:</p> <blockquote> <p>"Renderers are created through a process on the DOM called attachment. As a document is parsed and DOM nodes are added, a method called <code>attach</code> gets called on the DOM nodes to create the renderers.</p> <p><code>void attach()</code></p> <p>The <code>attach</code> method computes style information for the DOM node. If the display CSS property for the element is set to none or if the node is a descendant of an element with display: none set, then no renderer will be created."</p> </blockquote> <p>So, in order to be efficient, the browser does not even bother computing style information for elements with display set to none. As a result, the information is not available to be accessed via javascript. However, if the display property is <em>not</em> set to none, the following occurs:</p> <blockquote> <p>"During attachment the DOM queries CSS to obtain style information for an element. The resultant information is stored in an object called a RenderStyle... The RenderStyle can be accessed from a RenderObject using the style() method... One of the principal workhorse subclasses of RenderObject is RenderBox. This subclass represents objects that obey the CSS box model. These include any objects that have borders, padding, margins, width and height."</p> </blockquote> <p>So if your use case allows for you to retrieve the box-model rendering height and width via C/C++ directly from the browser and pass it to your javascript code via some other means, then you could query the height/width methods of the RenderBox subclass of each DOM element. This is basically how the WebKit Developer Tools gets this information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload