A good way to remember this is a class is a type of item and the id is the unique name of an item on the page.
ids are unique- Each element can have only one
id - Each page can have only one element with that
id
classes are NOT unique- You can use the same
classon multiple elements. - You can use multiple
classes on the same element. - Use a class when you want to consistently style multiple elements throughout the page/site. Classes are useful when you have, or possibly will have in the future, more than one element that shares the same style. An example may be a div of "comments" or a certain list style to use for related links.Additionally, a given element can have more than one class associated with it, while an element can only have one id. For example, you can give a div two classes whose styles will both take effect.Furthermore, note that classes are often used to define behavioral styles in addition to visual ones. For example, the jQuery form validator plugin heavily uses classes to define the validation behavior of elements (e.g. required or not, or defining the type of input format)Examples of class names are: tag, comment, toolbar-button, warning-message, or email.
Javascript cares
JavaScript people are already probably more in tune with the differences between
classes and ids. JavaScript depends on there being only one page element with any particular id, or else the commonly used getElementById function couldn't be depended on.
No comments:
Post a Comment