id 和 class 选择器

前端开发HTML元素中设置CSS样式,可以在元素中设置"id" 和 "class"选择器。

1.id 选择器

id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式。HTML元素以id属性来设置id选择器,在CSS 中 id 选择器以 "#" 来定义。

例1

#copyright {text-align: center;color: #ffffff;font-size: 12px; width: 250px;}

例2

#abc { text-align:center; color:red; } 

2.class 选择器 

class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用。

class 选择器在HTML中以class属性表示, 在 CSS 中,类选择器是以一个点"."号显示的

例1

.textcenter {text-align:center;}

例2

可以指定特定的HTML元素使用class。让所有的 p 元素使用 class="center" 让该元素的文本居中

p.textcenter {text-align:center;}

猜你喜欢

转载自blog.csdn.net/zjpjay/article/details/108592706