Arif's writing

Just trying to give by write something worthwhile to others..

Sunday

Difference between "id" and "class"

(Image:Screenshot of tizag.com page)
Both terms are used to create custom CSS, that applied to HTML tags, but there are differences between "class" and "id".

We can use either the attributes "id" and "class" to apply CSS styles to page elements, but :
1. id only applied to a single object: it can't be two same id(s) in a page.
2. class can be used for more objects (identical or not).

For example, it is possible to have this code :
<div class="author"></div> <div class="author"></div>
But it is not possible to have this code :
<div id ="author"></div> <div id ="author"></div> 
Then, for what was id attribute made?

By the way, what is the reason of "id" can only be used once on a page? while "class" can be used several times, and has the same function with "id"?  There are several reasons for this, here are :

1. Javascript function getElementById using the id attribute to get or fetch the content inside of its id.
2. The id can be used as name anchor, as it allows to replace (for this feature) the attribute "name" of <a tag. For example :
<div id="header">... <a href="#header">Top of page</a>

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home