class id 类

id 和 class 选择器
(1)、id 选择器
id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式,id 属性具有唯一性。HTML元素以 id 属性来设置 id 选择器,CSS 中 id 选择器以 "#" 来定义。注意: id 属性不能以数字开头。

<head>
<style>
#heading{
     color:red;
     text-align:center;
}
</style>
</head>
<body>
<h1 id="heading">CSS 选择器</h1>
</body>
class 选择器
  class 选择器用于描述一组元素的样式,也叫类选择器,class 可以在多个元素中使用,并且一个元素也可以指定多个类名。class 选择器在 HTML 中以 class 属性表示,在 CSS 中,类选择器以一个点 "." 号来定义。可以设置所有带有指定 class 的 HTML 元素,也可以指定特定的 HTML 元素使用 class。注意:类名的第一个字符也不能使用数字。
<head>
<style>
.center{
     text-align:center;
}
.col{
     color:red;
}
.font{
     font-size:18px;
     font-family:"Microsoft YaHei";
}
</style>
</head>
<body>
<h1 class="center">class 选择器</h1>
<p class="center col">我是一个段落。</p >
<p class="center font">我是另一个段落。</p >
</body>
他们俩的区别呢?
ID 选择器只能在文档中使用一次。与类选择器不同,在一个 HTML 文档中,ID 选择器只能使用一次,而且仅一次。而类选择器可以使用多次
还存在优先级 id优先级大于class

猜你喜欢

转载自liujun11.iteye.com/blog/2382672