Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...