css-id和class

id和class选择器

如果你想要在HTML中设置CSS样式,你需要在元素中设置id和class选择器。

1、id选择器

id选择器可以为标有特定id的HTML元素指定特定样式;

HTML元素以id属性来设置id选择器,CSS中id选择器以"#"来定义;

以下样式规则应用于元素属性id = "para1":

案例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
#para1{
color: red;
text-align: center;
}
</style>
</head>
<body>
<p id="para1">hello world</p>
<p>这个段落不受该样式影响</p>
</body>
</html>

扫描二维码关注公众号,回复: 3414413 查看本文章

2、class选择器:

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

class选择器在HTML中以class属性表示,在CSS中,类选择器以一个点“.”号显示;

在以下的例子中,所有拥有center类的HTML元素均为居中。

实例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.center{
text-align: center;
}
</style>
</head>
<body>
<h1 class="center">标题居中</h1>
<p class="center">段落居中</p>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/mylove-821717420/p/9726309.html