Skip to content

Commit 14b481c

Browse files
authored
Merge pull request #554 from phuocng/overflow
feat: Check if an element has overflow
2 parents 89f1869 + ba5a38b commit 14b481c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
category: DOM
3+
created: '2023-12-16'
4+
openGraphCover: /og/1-loc/check-element-has-overflow.png
5+
title: Check if an element has overflow
6+
---
7+
8+
**JavaScript version**
9+
10+
```js hasOverflow.js
11+
const hasOverflow = (ele) => ele.scrollHeight > ele.clientHeight || ele.scrollWidth > ele.clientWidth;
12+
```
13+
14+
**TypeScript version**
15+
16+
```ts hasOverflow.ts
17+
const hasOverflow = (ele: HTMLElement) => ele.scrollHeight > ele.clientHeight || ele.scrollWidth > ele.clientWidth;
18+
```
19+
20+
The `hasOverflow` function checks if an HTML element has overflow in its content. It does this by comparing the element's `scrollHeight` and `scrollWidth` properties to its `clientHeight` and `clientWidth` properties, respectively.
21+
22+
If the scroll height or width is greater than the client height or width, it means there's overflow in the content. In this case, the function returns `true`, indicating overflow. If there's no overflow, the function returns `false`.
23+
24+
This function is useful when you want to check if an element needs scrolling. You can use it to determine whether a scroll bar should be displayed for an element based on whether it has overflow or not.
25+
26+
## See also
27+
28+
- [Check if an element has CSS overflow](https://phuoc.ng/collection/1-loc/check-if-an-element-has-css-overflow/)

0 commit comments

Comments
 (0)