# Check if a given DOMRect is contained within another DOMRect

True when the target lies inside the container or on its edges.

Canonical: https://1loc.dev/dom/check-if-a-given-dom-rect-is-contained-within-another-dom-rect/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function isContained({ target, container }) { return target.x >= container.x && target.x + target.width <= container.x + container.width && target.y >= container.y && target.y + target.height <= container.y + container.height; }
```

## Input

```json
{
  "target": {
    "x": 10,
    "y": 10,
    "width": 20,
    "height": 20
  },
  "container": {
    "x": 0,
    "y": 0,
    "width": 40,
    "height": 40
  }
}
```

## Expected output

```json
true
```

## Edge case

Input:
```json
{
  "target": {
    "x": 30,
    "y": 10,
    "width": 20,
    "height": 20
  },
  "container": {
    "x": 0,
    "y": 0,
    "width": 40,
    "height": 40
  }
}
```
Output:
```json
false
```

## Limits

- Pass rects from getBoundingClientRect() or plain { x, y, width, height } objects. The original read left and top, which plain objects lack.
- Assumes non-negative width and height, as getBoundingClientRect() returns. Shared edges count as contained.

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-a-given-dom-rect-is-contained-within-another-dom-rect.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 and y, which DOMRects and plain JSON rects share, instead of left and top; runs in the lab. Original MDX retained as inert source evidence.
