Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

html - Difference between ID and Class

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...