Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect overflow in div element?

How can I detect vertical text overflow in a div element?

CSS:

div.rounded {    background-color:#FFF;    height: 123px;    width:200px;    font-size:11px;    overflow:hidden; } 

HTML:

<div id="tempDiv" class="rounded">     Lorem ipsum dolor sit amet,     consectetur     adipiscing elit. Phasellus vel quam vestibulum orci blandit laoreet.  </div> 
like image 225
Vladimir Perković Avatar asked Aug 21 '11 14:08

Vladimir Perković


People also ask

How do you know if you are overflowing?

The rules for detecting overflow in a two's complement sum are simple: If the sum of two positive numbers yields a negative result, the sum has overflowed. If the sum of two negative numbers yields a positive result, the sum has overflowed. Otherwise, the sum has not overflowed.

What is overflow in Div?

The overflow property specifies what should happen if content overflows an element's box. This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area. Note: The overflow property only works for block elements with a specified height. Show demo ❯

What is overflow in inspect element?

Overflow is what happens when there is too much content to fit in a container.


2 Answers

You can easily do that by comparing scrollHeight with clientHeight, try the following:

<script type="text/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); } </script> 

For more information please take a look at: http://help.dottoro.com/ljbixkkn.php

like image 162
DivinesLight Avatar answered Sep 19 '22 17:09

DivinesLight


A variation on Chamika's answer is to, in your actual html, have an inner and outer Div. The outer Div would have static height and width and overflow hidden. The inner Div only has the content and will stretch to the content.

You can then compare the height and width of the 2 Divs, without the need to dynamically add anything.

<div id="tempDiv" class="rounded">     <div class="content">         Lorem ipsum dolor sit amet,         consectetur     adipiscing elit. Phasellus vel quam vestibulum orci blandit laoreet.      </div> </div> 
like image 27
Josh Russo Avatar answered Sep 18 '22 17:09

Josh Russo