AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
CSS Class and CSS ID |
As mentioned in the CSS Syntax page, both class and ID are user-defined selectors. ClassClass is specified by including a period (.) before the selector name. The syntax for declaring a Class selector is as follows: .[Class Name] {
For example,
To apply this style to the HTML, using the following code:
The above HTML code renders: We can also specify different instances of a class selector. This is achieved by using the following syntax: [Type Selector].[Class Name] {
For example, if we have the following CSS declaration,
The following HTML,
would render,
Multiple ClassesIt is possible to include multiple classes at the same time. For example, assuming we have the following CSS declaration,
the following HTML,
would render,
IDID is specified by including a number sign (#) before the selector name. The syntax for declaring an ID selector is as follows: #[ID Name] {
For example,
To apply this style to the HTML, using the following code:
The above HTML code renders: Class vs IDThe difference between ID and class is that an ID selector can be called only once in a document, while a class selector can be called multiple times in a document. The second difference is that ID can be called by Javascript's getElementByID function. There is no hard rule on when to use ID and when to use Class. My suggestion is to use class as much as possible for maximum flexibility, with the only exception being when you want to use Javascript's getElementByID function, in which case you need use ID. Class and ID names are both case sensitive. For example, .classone and .ClassOne are two different classes. |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.