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 有什么区别,什么时候应该使用它们?


基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01