CSS伪元素选择器:增强页面交互性与个性化

CSS伪元素选择器是CSS语言中的一种强大工具,它允许我们根据元素的状态或位置来应用特定的样式。本文将介绍一系列CSS伪元素选择器,并对每个选择器进行举例和说明,以帮助开发者更好地利用这些工具来增强网页的交互性和个性化。

:active 选择器

:active 选择器用于选择活动状态的元素,通常用于链接或按钮在被点击时的样式。

css 复制代码
a:active {
  color: red;
}

:checked 选择器

:checked 选择器用于选择被选中的<input>元素,如单选按钮或复选框。

css 复制代码
input:checked {
  background-color: lightblue;
}

:disabled 选择器

:disabled 选择器用于选择被禁用的<input>元素。

css 复制代码
input:disabled {
  opacity: 0.5;
}

:empty 选择器

:empty 选择器用于选择没有子元素的元素,如空的段落。

css 复制代码
p:empty {
  display: none;
}

:enabled 选择器

:enabled 选择器用于选择已启用的<input>元素。

css 复制代码
input:enabled {
  border: 1px solid black;
}

:first-child 选择器

:first-child 选择器用于选择作为其父的首个子元素的元素。

css 复制代码
p:first-child {
  font-weight: bold;
}

:first-of-type 选择器

:first-of-type 选择器用于选择作为其父的首个特定类型的子元素。

css 复制代码
p:first-of-type {
  color: green;
}

:focus 选择器

:focus 选择器用于选择获得焦点的元素,如输入框。

css 复制代码
input:focus {
  outline: 2px solid blue;
}

:hover 选择器

:hover 选择器用于选择鼠标悬停其上的元素。

css 复制代码
a:hover {
  text-decoration: underline;
}

:in-range 选择器

:in-range 选择器用于选择具有指定范围内值的<input>元素。

css 复制代码
input:in-range {
  border-color: green;
}

:invalid 选择器

:invalid 选择器用于选择所有具有无效值的<input>元素。

css 复制代码
input:invalid {
  border-color: red;
}

:lang(language) 选择器

:lang(language) 选择器用于选择每个lang属性值以特定语言代码开头的元素。

css 复制代码
p:lang(it) {
  font-style: italic;
}

:last-child 选择器

:last-child 选择器用于选择作为其父的最后一个子元素的元素。

css 复制代码
p:last-child {
  margin-bottom: 0;
}

:last-of-type 选择器

:last-of-type 选择器用于选择作为其父的最后一个特定类型的子元素。

css 复制代码
p:last-of-type {
  color: purple;
}

:link 选择器用于选择所有未被访问的链接。

css 复制代码
a:link {
  color: blue;
}

:not(selector) 选择器

:not(selector) 选择器用于选择非特定选择器的元素。

css 复制代码
:not(p) {
  display: block;
}

:nth-child(n) 选择器

:nth-child(n) 选择器用于选择作为其父的第n个子元素的元素。

css 复制代码
p:nth-child(2) {
  background-color: lightyellow;
}

:nth-last-child(n) 选择器

:nth-last-child(n) 选择器用于选择作为父的倒数第n个子元素的元素。

css 复制代码
p:nth-last-child(2) {
  border-bottom: 1px solid gray;
}

:nth-last-of-type(n) 选择器

:nth-last-of-type(n) 选择器用于选择作为父的倒数第n个特定类型的子元素。

css 复制代码
p:nth-last-of-type(2) {
  color: darkorange;
}

:nth-of-type(n) 选择器

:nth-of-type(n) 选择器用于选择作为其父的第n个特定类型的子元素。

css 复制代码
p:nth-of-type(2) {
  font-weight: bold;
}

:only-of-type 选择器

:only-of-type 选择器用于选择作为其父的唯一特定类型的子元素。

css 复制代码
p:only-of-type {
  color: teal;
}

:only-child 选择器

:only-child 选择器用于选择作为其父的唯一子元素的元素。

css 复制代码
p:only-child {
  border: 1px solid gold;
}

:optional 选择器

:optional 选择器用于选择不带required属性的<input>元素。

css 复制代码
input:optional {
  border-style: dashed;
}

:out-of-range 选择器

:out-of-range 选择器用于选择值在指定范围之外的<input>元素。

css 复制代码
input:out-of-range {
  background-color: pink;
}

:read-only 选择器

:read-only 选择器用于选择指定了readonly属性的<input>元素。

css 复制代码
input:read-only {
  background-color: lightgray;
}

:read-write 选择器

:read-write 选择器用于选择不带readonly属性的<input>元素。

css 复制代码
input:read-write {
  cursor: text;
}

:required 选择器

:required 选择器用于选择指定了required属性的<input>元素。

css 复制代码
input:required {
  border-color: darkred;
}

:root 选择器

:root 选择器用于选择文档的根元素。

css 复制代码
:root {
  --accent-color: #3498db;
}

:target 选择器

:target 选择器用于选择当前活动的元素,通常与锚点链接一起使用。

css 复制代码
#news:target {
  border: 2px solid purple;
}

:valid 选择器

:valid 选择器用于选择所有具有有效值的<input>元素。

css 复制代码
input:valid {
  border-left: 5px solid green;
}

:visited 选择器

:visited 选择器用于选择所有已访问的链接。

css 复制代码
a:visited {
  color: purple;
}

通过这些伪元素选择器,开发者可以创建更具动态性和响应性的网页,提供丰富的用户体验。掌握这些选择器的使用,将极大地提升你的CSS技能。

相关推荐
袋鼠云数栈前端3 分钟前
AI 赋能编程,Coding新范式
css·sandbox
是忽然之间17 分钟前
vue对接帆软bi
前端
海底火旺19 分钟前
从零实现语音合成:基于火山引擎TTS的前端实践
前端·人工智能·react.js
jackeyfive19 分钟前
最快速搭建博客并部署到 GitHub Pages
前端·github
前端付豪20 分钟前
汇丰实时风控数据流揭秘:Kafka + Flink + CEP 规则引擎全链路实战
前端·后端·架构
Sibyllala25 分钟前
UE5.4——使用UltraDynamicSky实现天气切换和时间变化
前端
溪饱鱼36 分钟前
第七章: SEO与渲染方式 三
前端·seo
TE-茶叶蛋1 小时前
React & Vue 编译/运行流程
前端·vue.js·react.js
kuiini1 小时前
学习 React【Plan - June - Week 1】
前端·react.js
Hilaku1 小时前
低代码是“未来”还是“骗局”?作为前端我被内耗到了
前端·javascript·低代码