Resolved style and geometry APIs
Definition and purpose
Resolved CSS style and measured geometry answer different questions. getComputedStyle returns a live read-only resolved-style object; getBoundingClientRect returns a viewport-relative rectangle enclosing rendered border boxes.
Content versus border box
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { box-sizing: content-box; width: 100px; padding: 10px; border: 2px solid; } </style> </head> <body> <div id="a">Card</div> <output id="report"></output> <script> const card=document.querySelector("#a");const s=getComputedStyle(card);const r=card.getBoundingClientRect();document.querySelector("#report").textContent=`width ${s.width}; rectangle ${r.width}`; </script> </body></html>The report reads width 100px and rectangle 124.
Syntax and mental model
A content-box width of 100px with 10px padding and 2px border on each side gives a 124px border box. Transforms affect the bounding rectangle but do not change that reserved layout width. clientWidth includes padding but excludes border and vertical scrollbar; offsetWidth includes border and scrollbar and is integer-valued. getClientRects returns individual fragment rectangles. A geometry read after a style mutation may require synchronous style/layout work; batch reads and writes.
Transform geometry
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 100px; transform: scaleX(2); transform-origin: left; } </style> </head> <body> <div id="a">Card</div> <output id="report"></output> <script> const card=document.querySelector("#a");const s=getComputedStyle(card);const r=card.getBoundingClientRect();document.querySelector("#report").textContent=`width ${s.width}; rectangle ${r.width}`; </script> </body></html>The visual rectangle doubles without changing CSS width.
Common mistakes
Do not call the returned rectangle the CSS width. Its coordinate space and transforms matter. Resolved style is not always the abstract computed value.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: CSSOM and stylesheet APIs. Next lesson: Style resolution and invalidation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Resolved style and geometry APIs — knowledge check
Five questions on resolved style and geometry apis.
What is getComputedStyle returning?
What coordinate space is a bounding rectangle relative to?
What is the border-box width of 100px content plus 10px padding and 2px border each side?
Which measurement includes transformed geometry?
Why group reads and writes?