CSS: Is there a limit on how many classes an HTML element can have?

Tony the Pony picture Tony the Pony · Dec 4, 2010 · Viewed 33.5k times · Source

CSS allows an HTML element to have multiple classes:

<div class="cat persian happy big"> Nibbles </div>

But is there a limit on how many classes are allowed per item?

Answer

meagar picture meagar · Dec 4, 2010

You're only limited by the maximum length of an (X)HTML attribute's value, something covered well by this answer.

Browsers are often very forgiving of standards violations, so individual browsers may allow much longer class attributes. Additionally you are likely able to add a practically infinite number of classes to a DOM element via JavaScript, limited by the amount of memory available to the browser.

For all intents and purposes, there is no limit. I'm assuming you're asking out of curiosity; it goes without saying that if you're seriously worried about hitting this limit, you've done something very wrong.