Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Forbidden ID and Class Names

I was putting the finishing touched on a site I'm working on and was going through and cleaning up the code...deleting unnecessary markup, renaming elements to better describe their function, etc...

For some reason my page layout became broken in IE6 as a result. IE6 is still the primary browser on the company computers. After wasting a lot of time I finally determined the source of the problem.

I renamed the DIV element that contains the main content of the page to "content". For some reason, IE didn't appreciate this and I don't know why.

Are there certain words that are not allowed for use as ID or Class names? If so, what are they and what browsers are affected?

like image 624
SharpBarb Avatar asked Nov 09 '11 19:11

SharpBarb


People also ask

Can we use ID and class together in CSS?

Yes you can. You just need to understand what they are for, the class is more general and can be used several times, the id (is like your id's) you can use it only once.

Can you use numbers in CSS class names?

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

Can you have spaces in class names?

The class name can't contain a space, but it can contain hyphens or underscores. Any tag can have multiple space-separated class names.

Can CSS class have colon?

A CSS pseudo-element is a keyword added to a CSS selector that lets you style a specific part of the selected HTML element. In CSS3, they are usually denoted by two colons — for example, ::first-line — to differentiate them from pseudo-classes. In contrast, CSS2 syntax uses one colon (e.g., :first-line ).


2 Answers

Be certain that, there is no other ID called "Content" anywhere else on the site. Having 2 objects with the same ID (In this case "content") will make CSS fail.

And remember to check your CSS has changed ID as well! :)

like image 198
MicBehrens Avatar answered Oct 07 '22 09:10

MicBehrens


For an ID, according to the W3C:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

It should also go without saying, that two elements cannot have the same ID in the DOM.

For CSS class names, take a look at this StackOverflow answer.

like image 27
vcsjones Avatar answered Oct 07 '22 07:10

vcsjones