CSS 【详解】样式选择器(含ID、类、标签、通配、属性、伪类、伪元素、Content属性、子代、后代、兄弟、相邻兄弟、交集、并集等选择器)

发布于:2024-07-07 ⋅ 阅读:(45) ⋅ 点赞:(0)

CSS 样式选择器,用于选中页面中的 html 元素,以便添加 CSS 样式。

按渲染性能由高到低 依次是:

ID 选择器 #id

通过元素的 id 属性选中元素,区分大小写

<p id="p1" >第一段</p>
#p1{
  color: red;
}

但不推荐使用,因为:

  • id 选择器的优先级较高,不方便重置样式
  • id 选择器主要给 JS 使用

类选择器 .class

通过元素的 class 属性中的样式类名选中元素,区分大小写

最推荐使用的 CSS 选择器,因为类选择器语义化强,且方便重置样式。

<span class="important" >重点词汇</span>
.important{
  color: red;
  font-weight: bold;
}

同一个元素,可以添加多个样式类,用空格隔开

<span class="important big" >巨大的重点词汇</span>
.important {
  color: red;
  font-weight: bold;
}
.big {
  font-size: 60px;
}

标签选择器 标签名

通过元素的标签名选中元素,不区分大小写

<a href="https://www.baidu.com/" target="_blank" >百度</a>
a {
  text-decoration: none; /* 无文本装饰(消除默认的下划线) */
}

不推荐使用,因为标签选择器性能不佳,维护成本高

通配选择器 *

选中页面中除伪元素外的所有 html 元素,常用于清除浏览器的默认样式,但不推荐使用,因为消耗性能。

/* 清除所有html标签默认的外边距和内边距 */
* {
  margin: 0;
  padding: 0;
}

属性选择器 [属性]

根据元素的属性和属性值来选中元素,属性不区分大小写,属性值区分大小写

属性选择器 描述
[attribute] 用于选取带有指定属性的元素。
[attribute=value] 用于选取带有指定属性和值的元素。
[attribute~=value] 用于选取属性值中包含指定词汇的元素,非常适合包含多种组合属性值的场景
[attribute|=value] 属性值起始片段选择器,该值必须是整个单词。
[attribute^=value] 匹配属性值以指定值开头的每个元素。
[attribute$=value] 匹配属性值以指定值结尾的每个元素。
[attribute*=value] 匹配属性值中包含指定值的每个元素。

伪类选择器 :状态名

根据元素的不同状态来选中元素

同一个标签,不同的状态,有不同的样式,就叫做“伪类”
伪类选择器 例子 例子描述
:active a:active 选择活动的链接。(鼠标点击标签,但是不松手时)
:checked input:checked 选择每个被选中的<input> 元素。
:disabled input:disabled 选择每个被禁用的 <input> 元素。
:empty p:empty 选择没有子元素的每个 <p> 元素。
:enabled input:enabled 选择每个已启用的 <input> 元素。
:first-child p:first-child 选择作为其父的首个子元素的每个 <p> 元素。
:first-of-type p:first-of-type 选择作为其父的首个 <p> 元素的每个 <p> 元素。
:focus input:focus 选择获得焦点的 <input> 元素。
:hover a:hover 选择鼠标悬停其上的链接。
:in-range input:in-range 选择具有指定范围内的值的 <input> 元素。
:invalid input:invalid 选择所有具有无效值的 <input> 元素。
:lang(language) p:lang(it) 选择每个 lang 属性值以 “it” 开头的 <p> 元素。
:last-child p:last-child 选择作为其父的最后一个子元素的每个 <p> 元素。
:last-of-type p:last-of-type 选择作为其父的最后一个 <p> 元素的每个 <p> 元素。
:link a:link 选择所有未被访问的链接。
:not(selector) :not§ 选择每个非 <p> 元素的元素。
:nth-child(n) p:nth-child(2) 选择作为其父的第二个子元素的每个 <p> 元素。
:nth-last-child(n) p:nth-last-child(2) 选择作为父的第二个子元素的每个<p>元素,从最后一个子元素计数。
:nth-last-of-type(n) p:nth-last-of-type(2) 选择作为父的第二个<p>元素的每个<p>元素,从最后一个子元素计数
:nth-of-type(n) p:nth-of-type(2) 选择作为其父的第二个 <p> 元素的每个 <p> 元素。
:only-of-type p:only-of-type 选择作为其父的唯一 <p> 元素的每个 <p> 元素。
:only-child p:only-child 选择作为其父的唯一子元素的 <p> 元素。
:optional input:optional 选择不带 “required” 属性的 <input> 元素。
:out-of-range input:out-of-range 选择值在指定范围之外的 <input> 元素。
:read-only input:read-only 选择指定了 “readonly” 属性的 <input> 元素。
:read-write input:read-write 选择不带 “readonly” 属性的 <input> 元素。
:required input:required 选择指定了 “required” 属性的 <input> 元素。
:root root 选择元素的根元素。
:target #news:target 选择当前活动的 #news 元素(单击包含该锚名称的 URL)。
:valid input:valid 选择所有具有有效值的 <input> 元素。
:visited a:visited 选择所有已访问的链接。

列表中使用伪类选择器

伪类选择器 含义
li:nth-child(2) 第2个 li
li:nth-child(n) 所有的li
li:nth-child(2n) 所有的第偶数个 li
li:nth-child(2n+1) 所有的第奇数个 li
li:nth-child(-n+5) 前5个 li
li:nth-last-child(-n+5) 最后5个 li
li:nth-child(7n) 选中7的倍数

n 表示 0,1,2,3,4,5,6,7,8…(当n小于1时无效,因为n = 0 也是不会选中的)

表格中使用伪类选择器


网站公告

今日签到

点亮在社区的每一天
去签到