Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Element
  4. scrollWidth

Element: scrollWidth property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

ThescrollWidth read-only property of theElement interface is a measurement of the width of an element's content, including content not visible on the screen due to overflow.

ThescrollWidth value is equal to the minimum width the element would require in order to fit all the content in the viewport without using a horizontal scrollbar. The width is measured in the same way asclientWidth: it includes the element's padding, but not its border, margin or vertical scrollbar (if present). It can also include the width of pseudo-elements such as::before or::after. If the element's content can fit without a need for horizontal scrollbar, itsscrollWidth is equal toclientWidth.

Value

An integer.

Examples

Detecting overflowing content

In this example, we use thescrollWidth property to check if the content of an element is overflowing its boundaries. We have twodiv elements, the first with a width of100px, 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
<div>FooBar-FooBar-FooBar-FooBar</div><button>Check for overflow</button><pre></pre><div>FooBar-FooBar-FooBar-FooBar</div><button>Check for overflow</button><pre></pre>

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;}

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 clientWidth or notfunction isOverflowing(element) {  return element.scrollWidth > element.clientWidth;}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.

Specifications

Specification
CSSOM View Module
# dom-element-scrollwidth

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp