How to set max length for Span and Div elements ?
A:
If you mesn width, then use css width properties:
<div style='max-width:100px'>Content</div>
<span style='max-width:100px; display:block;'>Content</span>
If you really mean length of content, You can't, in HTML. You need to use a piece of javascript to do this. Take a look at JQuery, it's really easy to do. It will boil down to something like:
$('#yourDivId').html().length
Palantir
2010-06-24 08:16:35
I don't get the `.length` bit. What is this supposed to do? Isn't that the character length of the HTML string?
Pekka
2010-06-24 08:18:27
Pekka: yes :P At first read it seemed to me he was trying to get the length of the html content. Then I realized he probably meant width...
Palantir
2010-06-24 08:20:42
@Palantir well, on the other hand, he *is* talking about `maxlength`. That could be a typo or a reference to the form element attribute. We'll see when he comments.
Pekka
2010-06-24 08:22:57
+2
A:
There are the
max-height / max-width
CSS properties that will limit the width for block level elements (i.e. not span
s). Reference
Note that they are not supported by IE 6.
Pekka
2010-06-24 08:16:42