css id 和 class
id 和 class 选择器
如果你要在html元素中设置css样式,你需要在元素中设置"id" 和 "class"选择器。
id 选择器
id 选择器可以为标有特定 id 的 html 元素指定特定的样式。
html元素以id属性来设置id选择器,css 中 id 选择器以 "#" 来定义。
以下的样式规则应用于元素属性 id="para1":
实例
#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;}
多个 class 选择器可以使用空格分开:
实例
.center { text-align:center; } .color { color:#ff0000; }
类名的第一个字符不能使用数字!它无法在 mozilla 或 firefox 中起作用。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!