Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which to use <div class="name"> or <div id="name">? [duplicate]

Tags:

html

css

I am learning HTML. Can someone please tell me what is the difference between class and id and when to use one over the other? They seem to do the same thing

<!DOCTYPE HTML>
<html>
<head>

<style>

#mycolor1 {color: red;}    
.mycolor2 {color: red;}

</style>

</head>
<body>

<div id="mycolor1">     hello world </div>
<div class="mycolor2">     hello world </div>

</body>
</html>
like image 718
steve h Avatar asked May 30 '13 15:05

steve h


1 Answers

Read the spec for the attributes and for CSS.

  • id must be unique. class does not have to be
  • id has higher (highest!) specificity in CSS
  • Elements can have multiple non-ordinal classes (separated by spaces), but only one id
  • It is faster to select an element by it's ID when querying the DOM
  • id can be used as an anchor target (using the fragment of the request) for any element. name only works with anchors (<a>)
like image 84
Explosion Pills Avatar answered Nov 16 '22 02:11

Explosion Pills