Jump to content

what is the difference between the id&class?


mmf

Recommended Posts

I did not understand clearly the difference between id and class.How is The id specifies a unique id for an HTML element?!

<html><head> <style type="text/css">#myheader{ color: red; text-align: center;} </style></head><body><h1 id="myheader">Assumed that id is not used again</h1><P id="myheader">but here was used again on the other element easily</P></body></html>

Link to comment
Share on other sites

A class is used for multiple items in css. Bascally if you have two tables or divs in your webpage that you want to be the same you would use a class. An id is suppose to be used as an unique identifier. If you use the same id it will cause issue if you use javascript.

Link to comment
Share on other sites

Each HTML document can only have one element with a certain ID.

Link to comment
Share on other sites

For your example, if you have multiple headers that you want to use on one page (and paragraphs for that matter), do it with classes. That way, you don't have to assign different CSS code for multiple headers with ID's. Something like:<h1 id="header1">This is unique ID #1</h1><p class="content">This class can be used multiple times in your document</p><h1 id="header2">This is unique ID #2</h1><p class="content">This class can be used multiple times in your document</p><h1 class="headers">This H1 is a class, which means you can use this class name multiple times on a page</h1><p class="content">This class can be used multiple times in your document</p>To be a valid document, you can only use ID's once on each page. But with classes, you can use that same class multiple times in each document.

Link to comment
Share on other sites

  • 1 year later...

Is there any benefit to using an id over a class? Based on what I've read here, I'm thinking you should always just use classes, just in case you want to use the same CSS again. Can anyone think of a scenario where an id would be completely necessary?

Link to comment
Share on other sites

I can't remember the amount of times I've answered this. The ID makes it easier to access individual elements in Javascript. In CSS, ID selectors have priority over all other selectors when there are conflicting rules.

Link to comment
Share on other sites

First of all: Thanks for answering despite having answered this very question many times before, I know I'd be frustrated.Is there any simple examples you can share of IDs making it easier to access elements in JavaScript? Perhaps a link to a post of this topic you have already responded in depth to?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...