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;
}
相关推荐
GISer_Jing8 小时前
sqb&ks二面(准备)
前端·javascript·面试
谢尔登8 小时前
【Webpack】模块联邦
前端·webpack·node.js
Bottle4148 小时前
深入探究 React Fiber(译文)
前端
汤姆Tom8 小时前
JavaScript Proxy 对象详解与应用
前端·javascript
xiaopengbc8 小时前
在 React 中如何使用 useMemo 和 useCallback 优化性能?
前端·javascript·react.js
GISer_Jing9 小时前
React 18 过渡更新:并发渲染的艺术
前端·javascript·react.js
全栈技术负责人9 小时前
前端网络性能优化实践:从 HTTP 请求到 HTTPS 与 HTTP/2 升级
前端·网络·http
码上暴富9 小时前
Echarts雷达图根据数值确定颜色
前端·javascript·echarts
Mintopia10 小时前
在混沌宇宙中捕捉错误的光——Next.js 全栈 Sentry / LogRocket
前端·javascript·next.js
Mintopia10 小时前
长文本 AIGC:Web 端大篇幅内容生成的技术优化策略
前端·javascript·aigc