Is There a Limit to the Length of HTML Attributes

Is there a limit to the length of HTML attributes?

HTML 4

From an HTML 4 perspective, attributes are an SGML construct. Their limits are defined in the SGML Declaration of HTML 4:


QUANTITY SGMLREF
ATTCNT 60 -- increased --
ATTSPLEN 65536 -- These are the largest values --
LITLEN 65536 -- permitted in the declaration --
NAMELEN 65536 -- Avoid fixed limits in actual --
PILEN 65536 -- implementations of HTML UA's --
TAGLVL 100
TAGLEN 65536
GRPGTCNT 150
GRPCNT 64

The value in question here is "ATTSPLEN" which would be the limit on an element's attribute specification list (which should be the total size of all attributes for that element). The note above mentions that fixed limits should be avoided, however, so it's likely that there is no real limit other than available memory in most implementations.

HTML 5

It would seem that HTML 5 has no limits on the length of attribute values.

As the spec says, "This version of HTML thus returns to a non-SGML basis."

Later on, when describing how to parse HTML 5, the following passage appears (emphasis added):

The algorithm described below places
no limit on the depth of the DOM tree
generated, or on the length of tag
names, attribute names, attribute
values
, text nodes, etc. While
implementors are encouraged to avoid
arbitrary limits, it is recognized
that practical concerns will likely
force user agents to impose nesting
depth constraints.

Therefore, (theoretically) there is no limit to the length/size of HTML 5 attributes.

Limit in IE for length of data attribute?

You have to remove the " within the selector to make it work in IE:

So '.contentBlock[data-zip*='+ userZip +']' instead of '.contentBlock[data-zip*="'+ userZip +'"]'. Tested in IE version 9-11 (jsFiddle doesn't work well in IE8).

'[data-zip*='+ userZip +']' will also work, by the way.

What is a practical maximum length for HTML id?

Just tested: 1M characters works on every modern browser: Chrome1, FF3, IE7, Konqueror3, Opera9, Safari3.

I suspect even longer IDs could become hard to remember.

max length for value attribute of a html input text field

There are no limits, but be logical if you think there is too much data than there probably is. Check out this codepen and just keep typing into the input it can go on for well... a long time.

http://codepen.io/anon/pen/qdpgGQ

Jquery to show length and update view:

$('#name').keyup(function () {
$('#display').text($(this).val());

var myLength = $("#name").val().length;
$('#display1').text(myLength);

});

Difference between maxlength & size attribute in html?

The maxlength (not max-length) attribute specifies the maximum length of the input string in characters or, more exactly, in code units. The browser is expected to enforce this, by refusing to accept more characters. However, this is not meant to act as a security measure, since it can be overridden trivially. Rather, it tells the user that no more characters will be accepted in processing the data. This is useful when you have to set an upper limit, e.g. your database can store only a fixed number of characters fpr some information, and also when there is a logical limit on the length (e.g., if the data is a two-letter code for a US state, it has the logical upper limit of 2).

The maxlength attribute is thus logical, and it is expected to work even in non-visual user interface. It is not meant to affect the visual appearance of the input field in any way.

The size attribute, in contrast, is for visual rendering only. It suggests a visible width for the field, in terms of “average” characters. This vague concept has not been clarified in specifications, and browsers implement it inconsistently. It works best when a monospace font is used. This attribute does not limit the amount of characters entered, but it affects usability: it is difficult to enter, say, a 30 characters long string in a field that lets you see only 10 characters at a time. The width of a field is also a signal to the user: it indicates the expected maximum width of the input.

It is often suitable to use both attributes, often with the same value. For example, if the field is for a 5-digit postal code, size=5 maxlength=5 is suitable, especially if you also set font-family: monospace, so that the actual width is more or less exactly five digits.

However, the values may differ. E.g., when asking for a line in a postal address, you might set size=30, since this is normally sufficient for a line, but maxlength=80, if this corresponds to the limitations set by your database or data processing and you have no particular reason not to allow such long lines.

The size attribute can in principle be replaced by CSS, since it deals with visual rendering only. However, the width is usually best set in characters, and there is no universally supported unit for the average width of a character in CSS; the new ch unit comes close, but isn’t quite the same and isn’t supported by old browsers.



Related Topics



Leave a reply



Submit