mmf Posted May 28, 2010 Share Posted May 28, 2010 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 More sharing options...
ShadowMage Posted May 28, 2010 Share Posted May 28, 2010 In order to be valid you have to have unique id's. CSS will (I think) style everything with the specified id but it won't be valid markup. Link to comment Share on other sites More sharing options...
Ccssrs69 Posted May 28, 2010 Share Posted May 28, 2010 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 More sharing options...
mmf Posted June 5, 2010 Author Share Posted June 5, 2010 you mean that i can use the same id by document. Link to comment Share on other sites More sharing options...
Synook Posted June 6, 2010 Share Posted June 6, 2010 Each HTML document can only have one element with a certain ID. Link to comment Share on other sites More sharing options...
optimus203 Posted June 6, 2010 Share Posted June 6, 2010 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 More sharing options...
Randomishlying Posted December 21, 2011 Share Posted December 21, 2011 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 More sharing options...
Ingolme Posted December 21, 2011 Share Posted December 21, 2011 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 More sharing options...
Randomishlying Posted December 21, 2011 Share Posted December 21, 2011 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 More sharing options...
Ingolme Posted December 21, 2011 Share Posted December 21, 2011 Just this: http://w3schools.com/jsref/met_doc_getelementbyid.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.