批改状态:合格
老师批语:总结之后, 是不是发现其实挺简单的
一、选择器类型
1、CSS选择器类型可分为:简单选择器(含属性选择器)以及伪类选择器
2、简单选择器见表一:
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 1 | 元素选择器 | 根据元素标签名称进行匹配 | div {...} |
| 2 | 群组选择器 | 同时选择多个不同类型的元素 | h1,h2,h3{...} |
| 3 | 通配选择器 | 选择全部元素,不区分类型 | * {...} |
| 4 | 属性选择器 | 根据元素属性进行匹配 | *[...] |
| 5 | 后代选择器 | 选择当前元素的所有后代元素空格 |
ul li{……} |
| 6 | 父子选择器 | 选择当前元素的所有自带元素> |
form>section{……} |
| 8 | 同级相邻选择器 | 选择当前拥有共同父级且相邻的元素+ |
li.item+li{……} |
| 9 | 同级所有元素 | 选择拥有共同父级的后续所有元素~ |
li.item~li{………} |
3、常有简单属性选择器:
| 常见属性选择器 | 举例 |
|---|---|
| 类选择器 | .container{………} |
| id选择器 | #first{……} |
4、伪类选择器:结构伪类选择器和表单伪类选择器
5、结构伪类选择器
a、结构伪类选择器分为:不分类型匹配(:nth-child(n))和分类型匹配(:nth-of-type(n));
b、结构伪类参考表(n从1开始计算)
| 不分类匹配 | 分类匹配 | |
|---|---|---|
| 匹配第一个元素 | :first-child{……} |
:first-of-type{……} |
| 匹配最后一个元素 | :last-child{……} |
:last-of-type{……} |
| 匹配任意元素 | :nth-child(n){……} |
:nth-of-type(){……} |
备注:匹配任意元素中n可以通过表达式来表示匹配一组元素,表达式中的n从0开始,且必须写在前面;-n表示获取前面一组元素,正数表示从指定元素获取余下元素;
6、表单选择器:
7、其他伪类选择器
| 选择器 | 描述 |
|---|---|
:active |
向被激活的元素添加样式 |
:focus |
向拥有键盘输入焦点的元素添加样式 |
:hover |
当鼠标悬浮在元素上方时,向元素添加样式 |
:link |
向未被访问的链接添加样式 |
:visited |
向已被访问的链接添加样式 |
:root |
根元素,通常是html |
:empty |
选择没有任何子元素的元素(含文本节点) |
:not() |
排除与选择器参数匹配的元素 |
1、代码部分:
<!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>/* 元素选择器 */body {background-color: #e3e3e3;}/* 类选择器 */.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}/* 父子选择器 */.container > * {background-color: #80ffff;display: flex;justify-content: center;align-items: center;font-size: 20px;}/* 后代选择器 */.container * {/* border: 1px solid #ff0000; */}/* 伪类选择器 */.container > span:first-of-type {background-color: lightblue;}/* ID选择器 */#center {background-color: #ff00ff;}/*伪类选择器*/.container > :first-child {background-color: lightblue;}/*相邻同级选择*/#center + .item {background-color: red;}/* 伪类选择器 */.item:nth-child(n + 7) {background-color: lightgreen;}/*元素选择器*/fieldset {width: 400px;border: none;background-color: lightgreen;display: grid;grid-template-columns: 1fr;justify-items: center;gap: 15px;}/*其他伪类选择器,鼠标移动到选中*/fieldset:hover {box-shadow: 0 0 3px rosybrown;}fieldset > legend {text-align: center;width: 400px;margin: auto;font-size: 20px;color: green;font-weight: bolder;}section {width: 260px;margin: 10px auto;display: grid;grid-template-columns: 60px 1fr;}section:last-child {display: flex;justify-content: space-between;}/*表单选择器*/input:enabled {width: 200px;height: 20px;background: khaki;border-style: none;}/*表单选择器:获取焦点时选中*/input:focus {background-color: #fff;outline: none;}.button {width: 100px;height: 30px;}.button:hover {border: none;outline: none;background-color: royalblue;color: seashell;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><span class="item" id="center">5</span><span class="item">6</span><span class="item">7</span><span class="item">8</span><span class="item">9</span></div><hr /><form action=""><fieldset><legend>用户登陆</legend><section><label for="username">账号:</label><inputtype="text"placeholder="explome@qq.com"id="username"name="username"requiredautofocus/></section><section><label for="pasword">密码:</label><inputtype="password"name="password"id="password"placeholder="输入密码"reuqired/></section><section><button type="submit" class="button">登陆</button><button type="reset" class="button">取消</button></section></fieldset></form></body></html>
2、运行效果图:
1、css选择器虽然非常多,但经常用的并不多;最常见的由:元素选择器、类选择器和id选择器、伪类选择器(:nth-child(n) 、:hover、input:focus)等等
2、伪类选择器重点在::nth-child(n+2){……}在n表达式上的理解,偶数可以用even奇数可以用odd表示;
3、表单伪类选择器和链接伪类选择器,需要练习
4、特殊伪类::hover{……}和input:focus、:not()不包含选择器::not(span){……}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号