当前位置:网站首页>27 选择器的分类
27 选择器的分类
2022-07-25 14:41:00 【hello_sunny123】
一:选择器分类
选择器分为基础选择器和复合选择器两个大类,我们这里先讲解一下基础选择器。
基础选择器是由单个选择器组成的
基础选择器又包括:标签选择器、类选择器、id选择器和通配符选择器
二.(1)标签选择器
标签选择器(元素选择器)是指用 HTML 标签名称作为选择器,按标签名称分类,为页面中某一类标签指定统一的CSS样式。
语法:
标签名{
属性1:属性值1;
属性2:属性值2;
属性3:属性值3;
...
}
代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基础选择器之标签选择器</title>
<style> /* 标签选择器 :写上标签名 */ p {
color: green; } div {
color: pink; } </style>
</head>
<body>
<p>男生</p>
<p>男生</p>
<p>男生</p>
<div>女生</div>
<div>女生</div>
<div>女生</div>
</body>
</html>
运行结果:

作用
标签选择器可以把某一类标签全部选择出来,比如所有的<div>标签和所有的<span>标签。
优点
能快速为页面中同类型的标签统一设置样式。
缺点
不能设计差异化样式,只能选择全部的当前标签。
二.(2)类选择器
如果想要差异化选择不同的标签,单独选一个或者某几个标签,可以使用类选择器.
类选择器在 HTML 中以 class 属性表示,在 CSS 中,类选择器以一个点"."号显示。
语法
.类名 {
属性1: 属性值1;
...
}
例如,将所有拥有red类的HTML元素均为红色。
.red {
color: red;
}
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基础选择器之类选择器</title>
<style> .red {
color: red; } </style>
</head>
<body>
<ul>
<li class="red">冰雨</li>
<li class="red">痴心绝对</li>
<li>甜蜜蜜</li>
<li>过火</li>
<li>爱情转移</li>
<li>断点</li>
</ul>
</body>
</html>
运行结果

类选择器的语法:
结构需要用 class 属性来调用 class 类的意思
<div class="red">变红色</div>
注意
(1)类选择器使用"." (英文点号) 进行标识,后面紧跟类名(自定义,我们自己命名的)。
(2)可以理解为给这个标签起了一个名字,来表示。
(3)长名称或词组可以使用中横线来为选择器命名。
(4)不要使用纯数字、中文等命名,尽量使用英文字母来表示。
(5)命名要有意义,尽量使别人一眼就知道这个类名的目的。
(6)命名规范:见附件( Web 前端开发规范手册.doc )
记忆口诀:样式点定义,结构类调用。一个或多个,开发最常用。
案例
通过这个案例练习两个地方:
1.选择器的使用
2. div 就是一个盒子,用来装网页内容的.

代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>盒子</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>
运行结果

类选择器-多类名
我们可以给一个标签指定多个类名,从而达到更多的选择目的。这些类名都可以选出这个标签.
简单理解是一标签有多个名字.
1.多类名使用方式
<div class="red font20">亚索</div>
(1)在标签 class 属性中写多个类名
(2)多个类名中间必须用空格分开
(3)这个标签就可以分别具有这些类名的样式
代码示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>类选择器多类名的使用</title>
<style> .red {
color: red; } .font35 {
font-size: 35px; } </style>
</head>
<body>
<div class="red font35">刘德华</div>
</body>
</html>
运行结果

2.多类名开发中使用场景
(1)可以把一些标签元素相同的样式(共同的部分)放到一个类里面.
(2)这些标签都可以调用这个公共的类,然后再调用自己独有的类.
(3)从而节省 CSS 代码统一修改也非常方便.
多类名选择器在后期布局比较复杂的情况下,还是较多使用的
代码示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多类名使用场景</title>
<style> .box {
width: 100px; height: 100px; font-size: 35px; } .red {
/* 背景颜色 */ background-color: red; } .green {
background-color: green; } </style>
</head>
<body>
<div class="box red">红色</div>
<div class="box green">绿色</div>
<div class="box red">红色</div>
</body>
</html>
运行结果

二.(3) id 选择器
id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式。
HTML 元素以 id属性 来设置 id 选择器, CSS 中 id 选择器以 “#" 来定义。
语法
#id名 {
属性1: 属性值1;
...
}
例如,将 id 为 nav 元素中的内容设置为红色。
#nav {
color: red;
}
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基础选择器之id选择器</title>
<style> /* id选择器的口决:样式#定义,结构id调用,只能调用一次,别人切勿使用 */ #pink {
color: pink; } </style>
</head>
<body>
<div id="pink">迈克·杰克逊</div>
<div >pink老师</div>
</body>
</html>
运行结果

注意: id属性只能在每个 HTML 文档中出现一次。口诀:样式#定义结构 id 调用,只能调用一次,别人切勿使用.
id 选择器和类选择器的区别
(1)类选择器( class )好比人的名字,一个人可以有多个名字,同时一个名字也可以被多个人使用。
(2) id 选择器好比人的身份证号码,全中国是唯一的,不得重复。
(3) id选择器和类选择器最大的不同在于使用次数上。
(4)类选择器在修改样式中用的最多,id 选择器一般用于页面唯一性的元素上,经常和 JavaScript 搭配使用。

二.(4)通配符选择器
在 CSS 中,通配符选择器使用 “*” 定义,它表示选取页面中所有元素(标签)。
语法
* {
属性1: 属性值1;
...
}
通配符选择器不需要调用,自动就给所有的元素使用样式
特殊情况才使用,后面讲解使用场景
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基础选择器之通配符选择器</title>
<style> /* *在这里html body div span li 等等都改成了红色 */ * {
color: red; } </style>
</head>
<body>
<div>我的</div>
<span>我的</span>
<ul>
<li>还是我的</li>
</ul>
</body>
</html>
运行结果

三.总结

每个基础选择器都有使用场景,都需要掌握
如果是修改样式,类选择器是使用最多的
边栏推荐
- Gonzalez Digital Image Processing Chapter 1 Introduction
- Awk from getting started to digging in (20) awk parsing command line parameters
- 41 图片背景综合-五彩导航图
- The main function of component procurement system, digital procurement helps component enterprises develop rapidly
- MySQL的登陆【数据库系统】
- Writing standard of physical quantities and unit symbols
- Gateway reports an error service_ UNAVAILABLE
- QObject source code analysis -d pointer and Q pointer
- CTS test introduction (how to introduce interface test in interview)
- LeetCode-198-打家劫舍
猜你喜欢

filters获取data中的数据;filters使用data中的数据
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

PS制作加载GIF图片教程

PS making and loading GIF pictures tutorial

牛客多校 E G J L

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

Melodic + Realsense D435i 配置及错误问题解决

51 single chip microcomputer learning notes (1)

关于ROS2安装connext RMW的进度条卡在13%问题的解决办法

Leetcode-198- house raiding
随机推荐
The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
awk从入门到入土(21)awk脚本调试
The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
QObject源码剖析-d指针和q指针
awk从入门到入土(20)awk解析命令行参数
二维数组赋初值你会几种方法?
Overview of cloud security technology development
awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
Idea error failed to determine a suitable driver class
Summary of some problems about left value and right value [easy to understand]
easygui使用的语法总结
Qt connect 中, SIGNAL,SLOT 与 lambda 对比
Thymeleaf setting disabled
Thymeleaf controls whether display is displayed through style
AS查看依赖关系和排除依赖关系的办法
关于RDBMS和非RDBMS【数据库系统】
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
Yes, UDP protocol can also be used to request DNS server
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%