Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use camelCase in CSS class names

I want to name a CSS class and call it imgSuper. Can I use camelCasing in CSS classes?

like image 918
KJai Avatar asked Oct 10 '09 13:10

KJai


People also ask

Should CSS classes be camelCase?

It is ok yes, but be aware there are some general technical case sensitive issues to be aware of. From a technical perspective, if you're consistent in your css and html you should be fine.

Should class names be camelCase?

Class names should be nouns in UpperCamelCase , with the first letter of every word capitalised. Use whole words – avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

What characters are allowed in CSS class names?

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.


2 Answers

Technically yes, but it's risky because while CSS syntax is mostly case-insensitive, in some browsers under certain conditions, class names are treated as case-sensitive, as the spec does not specify how browsers should handle case when matching CSS rules to HTML class names.

From the spec, section 4.1.3:

All CSS syntax is case-insensitive within the ASCII range...

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

...the case-sensitivity of values of the HTML attributes "id" and "class", of font names, and of URIs lies outside the scope of this specification.

Rather than learn the browsers and conditions where case is sensitive, it's best that you just realize that the best approach is the most compatible: use the same case in all code for a given CSS class, and don't create two or more CSS class names that differ only in case.

like image 155
Warren Young Avatar answered Oct 10 '22 11:10

Warren Young


Sure. Here are the official rules; basically you shouldn't start the name with a number, and you can use letters, numbers, hyphen, underscore, and escaped or encoded characters.

However, as a matter of convention, hyphens are generally used instead of camel case.

like image 34
Jacob Mattison Avatar answered Oct 10 '22 11:10

Jacob Mattison