-2

i'm new to html/css and completed the css/html tutorial on codeacademy. I've reread over everything i've done and have a good knowledge around why everything works the way it does except the class/id tag. If i understand correctly the class tag is useful for when a bunch of elements should all recieve the same styling and id's are useful when you have exactly one element that should receive it's own styling. The thing i don't get is then what is the point of the id tag if i can get the same result using the class tag. For e.g. i have boxes 1,2 and 3 and i want them all to be the color black aka all recieve the same styling i would use the class tag. But i change my mind and now i want box2 to be white so in theory i should change box2 to an id tag so it can receive it's own styling but the thing is i can still use the class tag and get the same result by typing .box2 color:white;

My question is what is the point of the class and id tag if i can do the same thing for both using just the class tag.

Sorry if this is a difficult question to understand. I tried to word it as best as i could.

user3619057
  • 375
  • 1
  • 3
  • 8
  • I suspect the reason your question is being down voted has nothing to do with the fact that you are learning and more to do with the fact that this particular question and it's millions of subsets has been asked, again... and again... and, again. It's frustrating when anyone asks a question and it's obvious from the start that they didn't even attempt to research it first. Here's the best duplicate link I can find. Please read. http://stackoverflow.com/questions/544010/css-div-id-vs-div-class – squashriddle Jun 30 '15 at 06:36

3 Answers3

0

Ids are unique, you use id only for one element

<div id="me"></div>

On the other hand classes can be used to target more than one element

<div class="book">The Alchemist</div>
<div class="book">Harry Potter</div>
Muhammet
  • 3,288
  • 13
  • 23
  • Ok i understand, but can you not replicate the id tag with a class tag just by targeting only one element instead of several? so why would someone choose to use an id tag over a class tag i guess is what i'm asking. – user3619057 Jun 30 '15 at 05:11
  • @user3619057 Yes you can use class only for one element also, but ids are built in to use for only single elements. – Muhammet Jun 30 '15 at 05:20
  • Ok thanks, seemed like i was missing something and thought there might be more to it if classes can do the same thing, was thinking why make another different type of selector for it. But that's just my opinion. – user3619057 Jun 30 '15 at 05:29
  • @user3619057 You are welcome. Just remember if the element you are applying id or class is going to be unique, apply an id, otherwise a class. – Muhammet Jun 30 '15 at 05:49
0

id tag is actually utilized the best in JavaScript, it is used to identify a tag uniquely among a bunch of tags. You would realise the importance of id tag when you start working with JavaScripts. Suppose you have around 50 <p> tags in your HTML code. But you want to get value of one particular <p> tag, then the obvious way to do this is making you use of id.

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Date();
</script>

The above code checks for the tag whose id is "demo" and then assigns the output returned by Date( ) to that corresponding tag.

Greenhorn
  • 580
  • 6
  • 20
0

id is for single element. class is for group. if you want to change color of box2. you can alse give inline css on them because inline css priority is higher then another type