ID's and Classes


#1

Please can someone explain in detail the difference and purpose of using ID and Classes within div's.??
thank you.


#2

you can use id's only once on a page (so you can't have two id's named header), classes on the other hand you can use multiply times. a html element can have both a class and id at the same time

stackoverflow - difference class and id


#3

the below code work for me.. so here i had used the same ID called rogue twice and it works.. how come.??*

 <h3 class="red">I'm an h3 header in the red class!</h3>
	<h3>I'm just a regular old h3 header.</h3>
	<p class="red">I'm a paragraph in the red class!</p>
	<p id="rogue">I'm just a regular old paragraph.</p>
	<p id="rogue">I'm a rogue paragraph! I do what I want!</p>

#4

Well, i simplified a bit, but it seems i shouldn't have done that. w3c (The World Wide Web Consortium) make the guidelines (note: guidelines, not rules) for the web. They recommend to use id's only once, and browser didn't support to use the same id twice. Today browser do support the use of one id name multiply times, but w3c recommendation is still to use an id only once (i personally follow there guidelines)


#5

Thank you very much @stetim94 . since i'am just a beginner i come across so many doubts.. now i understand well about ID's and Classes. :+1: :relieved:


#6

Class selectors are used to select and apply the same style to group of elements in a div, whereas ID's are used to select and target individual elements in a div.
See the below tutorial for more understanding.

www.snoopcode.com/css/css-id-selector

www.snoopcode.com/css/css-selectors