CSS Id 和 Class

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;}

类名的第一个字符不能使用数字!它无法在 Mozilla 或 Firefox 中起作用

posted @ 2021-10-19 08:37  李新乾  阅读(46)  评论(0编辑  收藏  举报