Skip to main content

id vs class - CSS usage

This is a common question on css- ID vs Class.


The key thing to know is that IDs identify a specific element and therefore must be unique on the page - you can only use a specific ID once per document. Many browsers do not enforce this rule but it is a basic rule of HTML/XHTML and should be observed. Classes mark elements as members of a group and can be used multiple times, so if you want to define a style which will be applied to multiple elements you should use a class instead.

Also, an element can have multiple class values (by space-separating the various values), while an ID must be a single value. In this way, a single element can "inherit" from multiple classes.


Comments

Popular posts from this blog

How to deselect all the selection in a select ( combo ) box?

In a HTML page sometimes we may have to deslect all the selection in a select control of html. but as a user we can't do anything. But using Javascript programatically we can achieve this. Here is the sample code in Javascript. function clearSelect( selectObj ){ for (var i = 0; i < selectObj .options.length; i++) { selectObj .options[i].selected = false; } } note: we need to pass the select object as a parameter to this function. the alternative to this code is function clearSelect( selectObj ){ selectObj.selectedIndex = -1; }