css选择器


前言

HTML元素学习记录


提示:以下是本篇文章正文内容,下面案例可供参考

一、类选择器

1. 语法

可以将div元素看做是盒子,可以往里面加东西。
类选择器:样式点定义,结构类调用

2.示例及显示效果:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .red {
      width: 100px;
      height: 100px;
      background-color: red;
    }
    .green {
      width: 100px;
      height: 100px;
      background-color: green;
    }
  </style>
</head>

<body>
  <div class="red">红色</div>
  <div class="green">绿色</div>
  <div class="red">红色</div>
</body>

</html>

显示

二、类选择器:多类名

提示:vscode中输入标签后,按Tab键可以补全标签尖括号。

1.语法

一个标签可以有多个类名,用空格分开。

2.优点

节省代码,修改方便

3.示例及显示效果:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 100px;
      height: 100px;
      font-size: 40px;
    }
    .red {
      background-color: red;
    }
    .green {
      background-color: green;
    }
  </style>
</head>

<body>
  <div class="red box">红色</div>
  <div class="green box">绿色</div>
  <div class="red box">红色</div>
</body>

</html>

显示

三、id选择器

1.语法

#id名 {
	属性1:属性值1;
	...
}

id = id名

2.示例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 100px;
      height: 100px;
      font-size: 40px;
    }

    #pink {
      color: pink;
    }
  </style>
</head>
<body>
  <div class="box" id="pink">盒子</div>
</body>
</html>

显示

3.区别

id选择器只能调用一次,一般用于页面唯一性的元素上和js搭配使用;
class选择器可以调用多次。

四 通配符选择器

* {
	color : red;
	margin : 0;
	padding : 0;
	} 

五 标签选择器

1.语法

p {
color : red;
}

2.效果

选出所有相同的标签,不能差异化选择

总结

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值