id 选择器可以为标有特定 id 的 html 元素指定特定的样式。
html元素以id属性来设置id选择器,css 中 id 选择器以 # 来定义。
例如:
#para1{ text-align:center; color:red;}
注意: id属性不要以数字开头,数字开头的id在 mozilla/firefox 浏览器中不起作用。
class 选择器
class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用。
class 选择器在html中以class属性表示, 在 css 中,类选择器以一个点"."号显示。
例如:
在以下的例子中,所有拥有 center 类的 html 元素均为居中。
.center {text-align:center;}
你也可以指定特定的html元素使用class。
例如:
在以下实例中, 所有的 p 元素使用 class="center" 让该元素的文本居中。
p.center {text-align:center;}
推荐教程:css入门基础教程
以上就是关于css中的id选择器与class选择器的介绍的详细内容。