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>
Read the spec for the attributes and for CSS.
id
must be unique. class
does not have to beid
has higher (highest!) specificity in CSSid
id
can be used as an anchor target (using the fragment of the request) for any element. name
only works with anchors (<a>
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With