What is the difference between id and class in CSS, and when should I use them?(CSS 中的 id 和 class 有什么区别,什么时候应该使用它们?)
问题描述
#main {
background: #000;
border: 1px solid #AAAAAA;
padding: 10px;
color: #fff;
width: 100px;
}
<div id="main">
Welcome
</div>
在这里,我为 div
元素提供了一个 id
,它正在为其应用相关的 CSS.
Here I gave an id
to the div
element and it's applying the relevant CSS for it.
或
.main {
background: #000;
border: 1px solid #AAAAAA;
padding: 10px;
color: #fff;
width: 100px;
}
<div class="main">
Welcome
</div>
现在我给 div
提供了一个 class
,它也为我做同样的工作.
Now here I gave a class
to the div
and it's also doing the same job for me.
那么Id和class之间的确切区别是什么,我应该什么时候使用id
,什么时候应该使用我使用 class
.?我是 CSS 和网页设计的新手,在处理这个问题时有点困惑.
Then what is the exact difference between Id and class and when should I use id
and when should I use class
.? I am a newbie in CSS and Web-design and a little bit confused while dealing with this.
推荐答案
更多信息点击这里.
示例
<div id="header_id" class="header_class">Text</div>
#header_id {font-color:#fff}
.header_class {font-color:#000}
(请注意,CSS 使用前缀 # 表示 ID,. 表示类.)
(Note that CSS uses the prefix # for IDs and . for Classes.)
然而 color
是 HTML 4.01 标签属性,在 HTML 5 中已弃用.在 CSS 中没有font-color",样式是
color
所以上面应该是:
However color
was an HTML 4.01 <font>
tag attribute deprecated in HTML 5.
In CSS there is no "font-color", the style is color
so the above should read:
示例
<div id="header_id" class="header_class">Text</div>
#header_id {color:#fff}
.header_class {color:#000}
文本将是白色的.
这篇关于CSS 中的 id 和 class 有什么区别,什么时候应该使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS 中的 id 和 class 有什么区别,什么时候应该使用它们?


- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01