10

How many number of CSS classes can be assigned for an HTML element.

Ugesh Gali
  • 2,042
  • 4
  • 20
  • 24
  • 5
    You intend to store your database as CSS classes or what? – Darin Dimitrov Sep 06 '10 at 06:51
  • Some interviewer asked me like this.And i want the exact number of it which browser support how many classes. – Ugesh Gali Sep 06 '10 at 06:54
  • 13
    It's a horrible interview question. There's no reason why anybody would need to *know* this. For all practical intents and purposes, if you're using more than 5 on a single element, then most likely you're doing something wrong. – Yi Jiang Sep 06 '10 at 06:58
  • +1 on "it's a horrible interview question". – janosrusiczki Sep 06 '10 at 06:59
  • 1
    I thought it was good practice to create a class for each and every styling, then add that class to the element? class=" fontred sizelarge alightleft5px alignright150px" /end scarcasm – Sphvn Sep 06 '10 at 07:01
  • 1
    I guess the interviewer was expecting an answer: "infinity unless the browser has a practical limitation". If this is not the answer he is expecting then don't join the company. If the company hired him then it must be full of such idiots. – sv_in Sep 06 '10 at 08:17
  • possible duplicate of [CSS: Is there a limit on how many classes an HTML can have?](http://stackoverflow.com/questions/4354921/css-is-there-a-limit-on-how-many-classes-an-html-can-have) – user229044 Jan 17 '14 at 14:36

5 Answers5

10

I don't think there's a limit. A quick Google search yielded this.

janosrusiczki
  • 1,920
  • 2
  • 20
  • 41
7

The standard does not specify any limitation, but individual browsers may have practical ones.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
4

There are no such limitations imposed by the specification:

class

The attribute, if specified, must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

A set of space-separated tokens is a string containing zero or more words separated by one or more space characters, where words consist of any string of one or more characters, none of which are space characters.of which are space characters.

Community
  • 1
  • 1
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
2

I'm pretty sure IE6 only supports up to 2. Modern browers have no limit as far as i know.

cHao
  • 84,970
  • 20
  • 145
  • 172
1

One of the lesser known tricks with CSS is the fact that you don't have to limit your elements to just one class. If you need to set multiple classes on an element, you add them simply by separating them with a space in your attribute. For example:

<p class="pullquote btmmargin left">...</p>

So there is no limit on no of classes you apply.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263