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>
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.
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 ❯
Overflow is what happens when there is too much content to fit in a container.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With