views:

22

answers:

3

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
I don't get the `.length` bit. What is this supposed to do? Isn't that the character length of the HTML string?
Pekka
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
@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
+2  A: 

There are the

max-height / max-width

CSS properties that will limit the width for block level elements (i.e. not spans). Reference

Note that they are not supported by IE 6.

Pekka
A: 

You mean max width?

max-width: ??px;
hallie