| 类别 | 说明 |
|---|---|
| :link | <a> 初始状态/为访问过;静态样式 |
| :visited | 访问过/点击后状态;较少使用 |
| :hover | 鼠标置于元素上时;使用最广泛;移动端无效 |
| :focus | 获取焦点时[使用 tab 查看获取焦点效果] |
| :focus-within | 如果有子元素获取焦点时,即:选择包含有获得焦点元素的祖先元素 |
| :active | 单击时的状态;多用于提示;较少使用 |
| :target | 当前锚点选中时;使用 ID 指定锚点 |
a {
font: inherit;
color: inherit;
text-decoration: none;
}
a:link,
a:visited {
border-bottom: 1px solid #ccc;
}
a:hover,
a:focus {
border-color: #f40;
color: #f40;
}
a:active {
background-color: #f40;
color: #fff;
}
.wrap:focus-within {
border-color: #ff0;
}
| 类别 | 说明 |
|---|---|
| :focus | 获取焦点 - received focus |
| :checked | 元素选中 - checked or toggled to an on state
radio (<input type="radio">) checkbox (<input type="checkbox">) option (<option> in a <select>) |
| :valid | 验证有效 - contents validate successfully |
| :invalid | 验证无效 - contents fail to validate |
| :in-range | 在指定范围 - current value is within the range limits |
| :out-of-range | 超出指定范围 - current value is outside the range limits |
| :disabled | 元素功能被禁止生效时 |
input[type='text']:focus+label {
color: #f40;
}
input[type='radio']:checked+label {
color: #f40;
}
button {
padding: 10px;
border: none;
background-color: transparent;
font: inherit;
color: #f40;
cursor: pointer;
}
button:disabled {
color: #ccc;
cursor: not-allowed;
}
| 类别 | 说明 |
|---|---|
| :nth-child(n) | 第几个孩子;孩子从1开始
matches elements based on the indexes of the elements in the child list according to their position |
| :first-child | 第一个孩子 - the first element |
| :last-child | 最后一个孩子 - the last element |
| :only-child | 唯一的子元素/孩子 |
. 根据文档结构来选择,通常配合父级元素一起使用
. nth-child 可以使用关键字;odd 选中序号为奇数的孩子;even 选中序号为偶数的孩子
. nth-child 可以使用 表达式 An+B;n 只能是非负数字,从0开始;n 必须是表达式的第一项 - n is all nonnegative integers, starting from 0
. 以上选择器不区分元素类型 - according to their position
:nth-child(even)//选择序号为偶数的元素 :nth-child(odd)//选择序号为奇数的元素
:nth-child(2n)//同even;选择序号为偶数的元素 :nth-child(2n+1)//同odd:选择序号为奇数的元素
:nth-child(3n)//选择序号为3、6、9等元素 :nth-child(3n+1)//选择序号为1、4、7、10等元素
:nth-child(-n+3)//前3 - 选择序号为3、2、1的元素;:nth-child(3-n)错误 :nth-last-child(-n+3)//后3
[] 表格 - 斑马纹表格
tr:nth-child(odd) {
background-color: #dcb0ff;
}
tr:nth-child(even) {
background-color: #faccff;
}
. 指定类型的元素按照序号 position 选择
. 根据类型选择符合条件的元素;适合多种元素类型并存的情况
. 同样可以使用关键字或表达式
. 不可以依据类名或其它选择器类型
| 类别 | 说明 |
|---|---|
| first-of-type() | 选择指定类型第一个 |
| last-of-type() | 选择指定类型最后一个 |
| nth-of-type() | 选择指定类型第几个 |
.item:nth-of-type(n+2) {
color: #f40;
}
都是<div>
<div>和<p>
[] 同样的类名不同的标签
. 选中元素范围:分别从<div>的第2个开始和<p>的第2个开始
<div>和<p>
//定义
:root {
--title-h: 100px;
--main-color-white: #f3f3f3;
--main-color-red: #ff0f5b;
--main-color-purple: #be01fe;
--main-color-blue: #01b4ff;
--main-color-green: #2dfc52;
--main-color-orange: #ff7f50;
--main-color-gray: #333849;
accent-color: var(--main-color-red);
}
//使用
.warn {
font-weight: 600;
color: var(--main-color-red);
}
li {
//
}
li:empty {
display: none;
}
//传统方案
ul li:not(:last-child) {
margin-right: 10px;
}
//优化方案:使用相邻兄弟选择器实现
ul li + li {
margin-left: 10px;
}
//优化方案:使用flex的gap实现
ul {
display: flex;
gap: 10px;
}
h2:not(header h2, section h2) {
color: #f40;
}
.box h1,
.box h2,
.box h3 {
color: #f40;
}
.box :is(h1, h2, h3) {
color: #f40;
}
//或者当前元素是哪些状态,如表单的伪类选择器
input:is(:hover, :focus) {
border: 1px solid #f40;
outline: none;
}
tr:has(th) {
background-color: #fff;
}
select:has(option[value="jd"]:checked)+.cont img.jd {
display: block;
}
select:has(option[value="jd"]:checked)+.cont img:not(.jd) {
display: none;
}
//如果包含有一个图形img和一个紧跟着图形的段落p,则选中
.card:has(img + p) {
}
//匹配到包含有<ol>或<ul>的文章
article:has(ol, ul) {
}
//选中直接兄弟是dt的dt
dt:has(+ dt){
}
//选中<h2>,其相邻兄弟是:<p>或类为red的<ul>
h2:has(+ p, + ul.red) {
}
//某个ID被选中时
body:has(#p0:checked) {
}
//自适应布局-没有--left-width就使用48px;当元素left处于hover状态时,定义--left-width变量为30%
.grid {
display: grid;
transition: 300ms;
grid-template-columns: var(--left-width, 48px) auto;
}
.grid:has(.left:hover) {
--left-width: 30%;
}
//为多个元素指定样式
:where(.left, .center, .right):hover {
background: crimson;
}
//为<body>和<main>指定水平垂直居中
:where(body, main) {
display: flex;
justify-content: center;
align-items: center;
}
//使用公共类更加直观
.center-item {
display: flex;
justify-content: center;
align-items: center;
}
select:has(option[value='blue']:checked)+.goods div:not(.blue) {
display: none;
}
select:has(option[value='red']:checked)+.goods div:not(.yellow) {
display: none;
}
select:has(option[value='yellow']:checked)+.goods div:not(.yellow) {
display: none;
}