Skip to content

fix: [#2222] Calculates the size in getBoundingClientRect() based on the computed style - #2308

Open
sahiljagtap08 wants to merge 1 commit into
capricorn86:masterfrom
sahiljagtap08:fix/issue-2222-bounding-client-rect
Open

fix: [#2222] Calculates the size in getBoundingClientRect() based on the computed style#2308
sahiljagtap08 wants to merge 1 commit into
capricorn86:masterfrom
sahiljagtap08:fix/issue-2222-bounding-client-rect

Conversation

@sahiljagtap08

Copy link
Copy Markdown

Fixes #2222

What it does

Implements the size calculation that @capricorn86 suggested in #1416: getBoundingClientRect() now returns the width and height from the computed style of the element instead of always returning zeros. getClientRects() returns the same rect, as before.

  • Works for sizes set through stylesheets, the style attribute, and font relative units (rem, em), since the computed style resolves those to pixels.
  • Returns the border box: when box-sizing is content-box, padding and border widths are added to the size.
  • Returns a zero rect for disconnected elements and for display: none.

Limitations (documented in the JSDoc)

Happy DOM has no layout engine, so values that depend on the layout of other elements (percentages, auto, fit-content, transforms, grid tracks) can't be calculated and are treated as 0. The position (x/y) also stays 0.

Tests

New tests cover the stylesheet case from the issue, the style attribute, unit conversion, both box-sizing modes, disconnected elements, display: none, and non-resolvable values. Expected values were verified in a real browser.

Claude Code assisted with this pull request. All code has been reviewed and tested by me.

…) based on the computed style

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sahiljagtap08
sahiljagtap08 force-pushed the fix/issue-2222-bounding-client-rect branch from 5c0d248 to 4a3012e Compare August 23, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Styled width and height not retrieved by getBoundingClientRect and getClientRects

1 participant