Home > Web Front-end > CSS Tutorial > How can I determine if text content overflows a DIV element in JavaScript?

How can I determine if text content overflows a DIV element in JavaScript?

Susan Sarandon
Release: 2024-10-26 10:36:03
Original
605 people have browsed it

How can I determine if text content overflows a DIV element in JavaScript?

Detecting Vertical Overflow in a DIV Element

Determining whether a DIV element's vertical text content exceeds its boundaries can be essential for maintaining interface integrity. To detect this overflow, a comparison between an element's scrollHeight and clientHeight properties is recommended.

Implementation:

Consider the following HTML and CSS code:

<code class="html"><div id="tempDiv" class="rounded">
  Lorem ipsum dolor sit amet,
  consectetur    adipiscing elit. Phasellus vel quam vestibulum orci blandit laoreet.
</div></code>
Copy after login
<code class="css">div.rounded {
  background-color: #FFF;
  height: 123px;
  width: 200px;
  font-size: 11px;
  overflow: hidden;
}</code>
Copy after login

To detect overflow, insert the JavaScript code provided below into the page:

<code class="javascript">function GetContainerSize() {
  var container = document.getElementById("tempDiv");
  var message = "The width of the contents with padding: " + container.scrollWidth + "px.\n";
  message += "The height of the contents with padding: " + container.scrollHeight + "px.\n";

  alert(message);
}</code>
Copy after login

When this function is executed and an alert appears, comparing the scrollHeight and clientHeight values will indicate whether the text overflows the DIV.

Further Resources:

For additional information on this topic, please refer to the following URL:
http://help.dottoro.com/ljbixkkn.php

The above is the detailed content of How can I determine if text content overflows a DIV element in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template