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;
}
相关推荐
x***r1511 分钟前
Notepad++ 8.6 安装教程:详细步骤+自定义安装路径(附注意事项)
linux·前端·javascript
Hilaku4 分钟前
为什么很多工作 5 年的前端,身价反而卡住了?🤷‍♂️
前端·javascript·面试
ai超级个体4 分钟前
前端下午茶:这 3 个网页特效建议收藏(送源码)
前端·three.js·threejs·网页设计·vibe coding·网页灵感·网页分享
维斯184 分钟前
VueFlow 图自适应容器尺寸教程
前端
helloweilei5 分钟前
next/dynamic和React.lazy的区别
前端·next.js
Aaron_Feng8 分钟前
一个小工具解决Swift Actor重入问题
前端
笨笨狗吞噬者9 分钟前
维护 uniapp 小程序端近一年,我想拉一个开发者交流群
前端·程序员·uni-app
前端炒粉15 分钟前
React 面试高频题
前端·react.js·面试
程序员陆业聪16 分钟前
让 Android 里的 AI 真正「干活」:Function Calling 工程实现全解
前端
mumuWorld18 分钟前
解决openclaw以及插件安装的报错
前端·ai编程