diff --git a/files/en-us/web/api/element/scrollwidth/index.md b/files/en-us/web/api/element/scrollwidth/index.md index 0d6f62c9c4fe15f..75f6981b4799137 100644 --- a/files/en-us/web/api/element/scrollwidth/index.md +++ b/files/en-us/web/api/element/scrollwidth/index.md @@ -26,73 +26,91 @@ without a need for horizontal scrollbar, its `scrollWidth` is equal to ## Value -A number. +An integer. ## Examples +### Detecting overflowing content + +In this example, we use the `scrollWidth` property to check if the content of an element is overflowing its boundaries. We have two `div` elements, the first with a width of `100px`, and the second without a fixed width. Their content is exactly the same, and we display a message about whether each one is overflowing its container. + +#### HTML + ```html - - - - - Example - - - - -
FooBar-FooBar-FooBar-FooBar
- - -
FooBar-FooBar-FooBar-FooBar
- - - - +
FooBar-FooBar-FooBar-FooBar
+ +

+
FooBar-FooBar-FooBar-FooBar
+ +

+```
+
+#### CSS
+
+```css
+div {
+  padding: 0.15em;
+  overflow: hidden;
+  white-space: nowrap;
+  text-overflow: ellipsis;
+}
+
+button {
+  margin: 0.15em 0 0.5em 0;
+}
+
+pre {
+  margin: 0.5em 0;
+}
+
+#div1 {
+  width: 100px;
+}
+
+#log1 {
+  margin-bottom: 2em;
+}
 ```
 
-### Result
+#### JavaScript
+
+```js
+const button1 = document.getElementById("button1");
+const button2 = document.getElementById("button2");
+
+const div1 = document.getElementById("div1");
+const div2 = document.getElementById("div2");
+
+const log1 = document.getElementById("log1");
+const log2 = document.getElementById("log2");
+
+// Check if the scrollWidth is bigger than the offsetWidth or not
+function isOverflowing(element) {
+  return element.scrollWidth > element.offsetWidth;
+}
+
+function checkOverflow(element, log) {
+  if (isOverflowing(element)) {
+    log.innerText = `Content is overflowing, scrollWidth is ${element.scrollWidth}px`;
+  } else {
+    log.innerText = `No overflows, scrollWidth is ${element.scrollWidth}px`;
+  }
+}
+
+button1.addEventListener("click", () => {
+  checkOverflow(div1, log1);
+});
+
+button2.addEventListener("click", () => {
+  checkOverflow(div2, log2);
+});
+```
+
+#### Result
+
+Click the buttons to check if the content is overflowing the containers.
 
-{{EmbedLiveSample('Examples')}}
+{{EmbedLiveSample("detecting_overflowing_content", "100%", "190")}}
 
 ## Specifications