Alex Rivera | Logout

Get real width of elements with jQuery

Asked 2012-04-23T08:41:42.460
21

I am writing a validator for "visual correctness" of html files. The goal is to detect too wide elements.

Here is a demo of my problem.

The dotted red line is an indicator of the max width of the document (200px in this example). The first paragraph is fine, but the second is too wide. Nevertheless, all of the following commands still return "200px" as the width:

// all return 200, but the value should be larger   
$('#two').width();
$('#two').outerWidth();
$('#two').prop('clientWidth');

Please see the Fiddle for more details.

How can i detect such oversized elements?

Updated question: Better to ask, how can i detect text that exceeds the borders of their parent elements?

Updated requirement: I am not allowed to change anything in the source HTML or CSS. But i can do anything i want with jQuery to modify the document, so that i can detect those too wide elements.

Edit
Report

1 Answer

1

Apply word-wrap: break-word; to it.. so the word will break and there won't be any text going out of the container... btw you can't check the width of the text which is going out of the container.

Example

Update: You can check if the width of text in it is bigger than the width of the container like this

answered 2012-04-23T08:51:18.163

Your Answer