Skip to content

CSS 选择器

基本选择器

基本选择器说明用法
通配选择器选中所有标签,一般用于清除样式* {color:red}
元素选择器选中所有同种标签,但是不能差异化选择h1 {color:red}
类选择器选中所有特定类名( class 值)的元素,使用频率很高.say {color:red}
ID 选择器选中特定 id 值的那个元素(唯一的)#earthy {color:red}

复合选择器

复合选择器说明用法符号
交集选择器(且)选中同时符合多个条件的元素选择器1选择器2 {}紧挨着
并集选择器(或)选中多个选择器对应的元素选择器1,选择器2 {},
后代选择器符合要求的后代元素选择器1 选择器2 {}空格
子代选择器符合要求的子元素选择器1>选择器2 {}>
相邻兄弟选择器符合条件的相邻兄弟元素选择器1+选择器2 {}+
通用兄弟选择器符合条件的所有兄弟元素选择器1~选择器2 {}~
属性选择器选中属性值符合一定要求的元素见下表

属性选择器

属性选择器语法说明
选择器[属性名]选中具有某个属性的元素。
选择器[属性名="值"]选中包含某个属性,且属性值等于指定值的元素。
选择器[属性名^="值"]选中包含某个属性,且属性值以指定的值开头的元素。
选择器[属性名$="值"]选中包含某个属性,且属性值以指定的值结尾的元素。
选择器[属性名*=“值”]选择包含某个属性,属性值包含指定值的元素。
css
/* 选择器[属性名]: 选中具有title属性的元素 */
div[title] {
  color: red;
}
/* 选择器[属性名="值"]: 选中title属性值为atguigu的元素 */
div[title='atguigu'] {
  color: red;
}
/* 选择器[属性名^="值"]: 选中title属性值以a开头的元素 */
div[title^='a'] {
  color: red;
}
/* 选择器[属性名$="值"]: 选中title属性值以u结尾的元素 */
div[title$='u'] {
  color: red;
}
/* 选择器[属性名*=“值”]: 选中title属性值包含g的元素 */
div[title*='g'] {
  color: red;
}

伪类选择器

伪类: 有类的作用,但不是类

动态伪类

伪类选择器说明示例
:hover当鼠标悬停在元素上button:hover { background: yellow; }
:active元素被激活(如点击时)a:active { color: green; }
:focus元素获得焦点input:focus { border-color: blue; }
:visited用户已访问的链接a:visited { color: purple; }
:link用户未访问的链接a:link { color: red; }

注意点:遵循 LVHA 的顺序,即: linkvisitedhoveractive

结构伪类

常用语法说明
选择器:first-child所有兄弟元素中的第一个。
选择器:last-child所有兄弟元素中的最后一个。
选择器:nth-child(n)所有兄弟元素中的第 n 个。
选择器:first-of-type所有同类型兄弟元素中的第一个。
选择器:last-of-type所有同类型兄弟元素中的最后一个。
选择器:nth-of-type(n)所有同类型兄弟元素中的 第 n 个 。
了解语法说明
选择器:nth-last-child(n)所有兄弟元素中的倒数第 n 个。
选择器:nth-last-of-type(n)所有同类型兄弟元素中的 倒数第 n 个 。
选择器:only-child选择没有兄弟的元素(独生子女)。
选择器:only-of-type选择没有同类型兄弟的元素。
选择器:root根元素,html:root=:root
选择器:empty内容为空元素(空格也算内容)。

关于 n 的值:公式都是 an+b 的格式

  1. 0 或 不写 :什么都选不中 —— 几乎不用。
  2. n :选中所有子元素 —— 几乎不用。
  3. 1 ~ ∞:选中对应序号的子元素。
  4. 2neven :选中序号为偶数的子元素。
  5. 2n+1odd :选中序号为奇数的子元素。
  6. -n+3 :选中的是前 3 个。

UI 伪类

伪类选择器说明示例
:checked选中的复选框或单选框input:checked { border: 2px solid green; }
:disabled被禁用的表单元素input:disabled { background: #ccc; }
:enabled可用的表单元素input:enabled { background: white; }
:indeterminate不确定状态(主要是 radio 组或进度条)input:indeterminate

表单元素

伪类选择器说明示例
:checked选中的复选框或单选框input:checked { border: 2px solid green; }
:disabled被禁用的表单元素input:disabled { background: #ccc; }
:enabled可用的表单元素input:enabled { background: white; }
:required必填的表单字段input:required { border-left: 3px solid red; }
:optional可选的表单字段input:optional { border-left: 3px solid green; }
:valid值合法的表单字段input:valid { background: #e0ffe0; }
:invalid值不合法的表单字段input:invalid { background: #ffe0e0; }
:in-range值在 min-max 范围内的输入框input:in-range
:out-of-range值超出 min-max 范围的输入框input:out-of-range
:read-only有 readonly 属性的或内容不可编辑的元素input:read-only
:read-write没有 readonly 属性、可编辑的元素input:read-write

否定伪类

语法说明
选择器:not(选择器)排除满足括号中条件的元素

CSS4 新增

伪类语法示例功能说明
:where():where(.a, .b)功能同普通选择器,但括号内的特异性(权重)为 0
:is():is(header, main, footer) p匹配列表中任意一个选择器,特异性取最大值
:has()article:has(> img)父元素/祖先元素选择器,选择包含指定子元素的元素
:focus-visiblebutton:focus-visible只有当元素应该显示键盘焦点指示器时才匹配(比:focus 更智能)
:focus-withinform:focus-within表单或其子元素获得焦点时生效

目标伪类(了解)

语法说明
选择器:target选中锚点指向的元素

语言伪类(了解)

语法说明
选择器:lang(语言)根据指定的语言选择元素

伪元素选择器

作用:选中元素中的一些特殊位置

伪元素选择器作用说明常用示例
::before在元素内容前插入内容p::before { content: "★"; }
::after在元素内容后插入内容p::after { content: "★"; }
::first-line选中元素的第一行文本p::first-line { font-weight: bold; }
::first-letter选中元素的首字母p::first-letter { font-size: 2em; }
::selection选中文本时的样式p::selection { background: yellow; }
::marker列表项的标记(如圆点、数字)li::marker { color: red; }
::placeholder输入框的占位符文本input::placeholder { color: gray; }
::backdrop模态元素(如 <dialog>)背景dialog::backdrop { background: rgba(0,0,0,0.5); }
::cue视频字幕或媒体轨道的文本::cue { color: yellow; }
::spelling-error标记拼写错误的文本span::spelling-error { text-decoration: underline red; }
::grammar-error标记语法错误的文本span::grammar-error { text-decoration: underline orange; }

选择器权重

!important > 行内样式(1,0,0,0) > ID 选择器(0,1,0,0) > 类选择器(0,0,1,0) > 元素选择器(0,0,0,1) > 通配选择器(0,0,0,0)

权重计算

  • 每个选择器,都可计算出一组权重(特异性),格式为: (a,b,c,d)
  • !important 不属于选择器权重(specificity)体系,不会算进 (a,b,c,d),!important 会把声明提升到「权重体系之外的最高优先级」
  • a : 行内样式
  • b : ID 选择器的个数
  • c : 类、伪类、属性 选择器的个数
  • d : 元素、伪元素 选择器的个数
选择器权重
ul>li(0,0,0,2)
div ul>li p a span(0,0,0,6)
#atguigu .slogan(0,1,1,0)
#atguigu .slogan a(0,1,1,1)
#atguigu .slogan a:hover(0,1,2,1)

权重比较

按照从左到右的顺序,依次比较大小,当前位胜出后,后面的不再对比

  • (0,1,0,0) > (0,0,2,2)
  • (0,1,1,0) > (0,1,0,3)
  • (0,1,1,3) > (0,1,1,2)