# Check if two DOMRects intersect

Two rects intersect when they meet on both axes.

Canonical: https://1loc.dev/dom/check-if-two-dom-rects-intersect/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function intersect({ a, b }) { return b.x <= a.x + a.width && b.x + b.width >= a.x && b.y <= a.y + a.height && b.y + b.height >= a.y; }
```

## Input

```json
{
  "a": {
    "x": 0,
    "y": 0,
    "width": 40,
    "height": 40
  },
  "b": {
    "x": 10,
    "y": 10,
    "width": 20,
    "height": 20
  }
}
```

## Expected output

```json
true
```

## Edge case

Input:
```json
{
  "a": {
    "x": 0,
    "y": 0,
    "width": 40,
    "height": 40
  },
  "b": {
    "x": 40,
    "y": 0,
    "width": 10,
    "height": 10
  }
}
```
Output:
```json
true
```

## Limits

- Touching edges count as intersecting, as the edge case shows. For a strict overlap use < and >, as in Check if a rectangle overlaps another one.
- Assumes non-negative width and height, as getBoundingClientRect() returns.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/check-if-two-dom-rects-intersect.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Reads x, y, width and height so plain JSON rects work; inclusive edges documented; runs in the lab. Original MDX retained as inert source evidence.
