Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ID and Class

Tags:

html

css

I was going through CSS :Class and ID . And I wont felt any differnce between them when I tried the examples given by them.

<!DOCTYPE html>
<html>
<head>
<style>
#para1
{
text-align:center;
color:red;
} 
</style>
</head>

<body>
<h1 id ="para1">Hai</h1>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>

gives me the output as I wish where the say The id selector is used to specify a style for a single, unique element.

Am I looking to this in a wrong angle ?


1 Answers

One uniquely identifies and the other classifies.

In the first case, it is nonsense to call two things to same thing.

In the second case it is common to want to give a group of things the same look and feel.

In the context of CSS, you should only rely on classes. Validators of CSS should balk at using ids for applying styles. You can still define a unique style used by only one distinct element.

like image 79
Grant Thomas Avatar answered May 09 '26 23:05

Grant Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!