css基础-选择器

选择器进阶


子串选择器

css 复制代码
/* 匹配 href 以 "https" 开头的链接 */
a[href^="https"] {
  color: green;
}

/* 匹配 href 包含 "example" 的链接 */
a[href*="example"] {
  text-decoration: underline;
}

/* 匹配 href 以 ".pdf" 结尾的链接 */
a[href$=".pdf"]::after {
  content: "📄";
}

伪类

  1. 状态伪类
css 复制代码
/* 未访问链接 */
a:link { color: blue; }

/* 已访问链接 */
a:visited { color: purple; }

/* 鼠标悬停 */
button:hover { background: #eee; }

/* 输入框获取焦点时 */
input:focus { outline: 2px solid orange; }
  1. 结构伪类
css 复制代码
/* 第一个子元素 */
ul li:first-child { font-weight: bold; }

/* 最后一个子元素 */
ul li:last-child { border-bottom: none; }

/* 第3个元素 */
ul li:nth-child(3) { color: red; }

/* 奇数行 */
tr:nth-child(odd) { background: #f5f5f5; }

伪元素

  1. ::before / ::after
css 复制代码
/* 在元素前插入内容 */
h1::before {
  content: "🌟";
  margin-right: 10px;
}

/* 在元素后插入内容 */
.price::after {
  content: "元";
  color: #999;
}
  1. ::selection
css 复制代码
/* 文本选中样式 */
::selection {
  background: yellow;
  color: black;
}

其他选择器

  1. 群组选择器
css 复制代码
/* 同时选择 h1-h3 */
h1, h2, h3 {
  font-family: Arial;
}
  1. 否定伪类
css 复制代码
/* 排除 .disabled 的按钮 */
button:not(.disabled) {
  cursor: pointer;
}
相关推荐
IT_陈寒23 分钟前
React的useEffect依赖数组把我坑惨了,真相其实很简单
前端·人工智能·后端
徐小夕26 分钟前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器
前端·vue.js·算法
恋猫de小郭30 分钟前
KMP / CMP 鸿蒙版本 Beta 发布,他有什么特别之处?
android·前端·flutter
乘风gg1 小时前
OpenClaw 爆火,但”飞书"赢麻了!!!
前端·ai编程·claude
Oneslide1 小时前
React 纯前端技术栈报告(2026年)
前端
前端一小卒2 小时前
AI 时代,前端工程化要重做一遍
前端
橙子家10 小时前
浏览器缓存之【基础键值存储】:Local storage 和 Session storage
前端
星星在线13 小时前
MusicFree:一个「All in One」的个人音乐服务器,让听歌回归简单
前端·后端
IT_陈寒14 小时前
Redis的SETNX并发问题让我加了三天班
前端·人工智能·后端
demo007x14 小时前
Docling 文档转换以及技术架构分析
前端·后端·程序员