Jump to content

Recommended Posts

An ID indicates that the element is unique and that there are no other elements on the page like it. A class indicates that an element has the same meaning as other elements of its same class name. You use ID to get one single element from the page, while you use class to get several of them.

  • Like 2
Link to post
Share on other sites

I'm not sure what there is to say that isn't well covered in the tutorials on w3schools. Id's are used to identify an element that will only appear once on a page. a class can be used for multiple elements.

 <html><head><style type="text/css"> #header{  width: 400px;  height: 400px;  margin: 0px auto;} .text{  color: black;  background-color: orange;}</style><body><div id="header">  <p class="text">Happy</p>  <p class="text">Halloween</p>  <p class="text">W3Schools Forums</p></div></body></html>

Edited by thescientist
  • Like 2
Link to post
Share on other sites

What I don't get is why ID is relevant. I mean, class is basically the same as ID except it has more functionality. So really, why not just use class all the time? Does it eat up more space or something, is there any kind of advantage to using ID instead of class?

Edited by JustRob
Link to post
Share on other sites

In Javascript it's far easier to access en element by its ID than looking for one by its class name. In CSS, selectors with an ID in them have the highest priority.

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...