CSS伪类是前端开发中不可或缺的强大工具,它们允许我们根据文档树之外的信息或简单选择器无法表达的状态来样式化元素。本文将全面探讨CSS伪类的各种类型、使用场景和最佳实践。
伪类(Pseudo-class)是CSS中的一种特殊关键字,用于选择元素的特定状态或特征。它们以冒号(:
)开头,可以附加到选择器末尾来定义元素在特定状态下的样式。
/* 语法 */
selector:pseudo-class {
property: value;
}
/* 示例 - 链接悬停状态 */
a:hover {
color: #ff4500;
}
button:hover {
background-color: #4CAF50;
transform: translateY(-2px);
}
button:active {
transform: translateY(1px);
}
input:focus {
outline: 2px solid #2196F3;
box-shadow: 0 0 5px rgba(33, 150, 243, 0.5);
}
button:focus-visible {
outline: 2px dashed #000;
}
/* 列表首项样式 */
li:first-child {
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
/* 列表末项样式 */
li:last-child {
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
/* 隔行变色 */
tr:nth-child(even) {
background-color: #f2f2f2;
}
/* 选择前3项 */
li:nth-child(-n+3) {
font-weight: bold;
}
/* 复杂模式:3n+1 (1,4,7...) */
div:nth-child(3n+1) {
margin-right: 0;
}
/* 每第三个段落 */
p:nth-of-type(3n) {
color: #e91e63;
}
/* 选择所有不是.disabled的按钮 */
button:not(.disabled) {
cursor: pointer;
}
/* 复合使用 */
li:not(:first-child):not(:last-child) {
padding: 8px 0;
}
input[type="checkbox"]:checked + label {
color: #4CAF50;
font-weight: bold;
}
input:disabled {
background-color: #ebebe4;
cursor: not-allowed;
}
input:enabled {
border-color: #66afe9;
}
input:valid {
border-color: #4CAF50;
}
input:invalid {
border-color: #f44336;
}
input:placeholder-shown {
font-style: italic;
color: #999;
}
section:target {
background-color: #fffde7;
animation: highlight 1s ease;
}
div.empty:empty {
display: none;
}
div.empty:not(:empty) {
padding: 15px;
}
:root {
--primary-color: #4285f4;
--base-font-size: 16px;
}
/* 传统写法 */
header h1, header h2, header h3 {
font-family: 'Roboto', sans-serif;
}
/* 使用:is() */
header :is(h1, h2, h3) {
font-family: 'Roboto', sans-serif;
}
/* 特异性为0,0,1 */
article :where(h1, h2, h3) {
margin-top: 1em;
}
/* 选择包含img的article */
article:has(img) {
background: #f5f5f5;
}
/* 选择后面跟着p的h2 */
h2:has(+ p) {
margin-bottom: 0;
}
/* 悬停且获得焦点的按钮 */
button:hover:focus {
box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.5);
}
/* 非第一个且非最后一个列表项 */
li:not(:first-child):not(:last-child) {
padding: 8px 0;
}
.menu-item {
transition: transform 0.3s ease;
}
.menu-item:hover {
transform: scale(1.05);
}
.menu-item:active {
transform: scale(0.98);
}
/* 自定义复选框 */
input[type="checkbox"] {
opacity: 0;
position: absolute;
}
input[type="checkbox"] + label::before {
content: "";
display: inline-block;
width: 18px;
height: 18px;
border: 2px solid #ccc;
margin-right: 8px;
vertical-align: middle;
}
input[type="checkbox"]:checked + label::before {
background-color: #4CAF50;
border-color: #4CAF50;
}
input[type="checkbox"]:disabled + label {
color: #aaa;
}
/* 鼠标设备与触摸设备区分 */
@media (hover: hover) {
/* 只在支持悬停的设备上应用 */
.card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
}
@media (hover: none) {
/* 触摸设备专用样式 */
.card:active {
transform: scale(0.98);
}
}
:hover
在移动设备上的表现:focus
状态明显可见CSS选择器Level 4规范正在引入更多强大的伪类:
/* 匹配包含特定数量子元素的父元素 */
.container:has(> .item:nth-child(5)) {
grid-template-columns: repeat(5, 1fr);
}
/* 方向性伪类 */
:dir(rtl) {
text-align: right;
}
/* 范围伪类 */
p:read-only {
background-color: #f5f5f5;
}
CSS伪类为我们提供了强大的工具来创建动态、交互式和响应式的用户界面。从简单的悬停效果到复杂的结构选择,伪类能够帮助我们以更少的代码实现更多的功能。随着CSS标准的不断发展,伪类的功能也在不断增强,如:has()
选择器将彻底改变我们选择元素的方式。
记住,良好的伪类使用应该:
通过掌握这些伪类技术,你将能够创建更加精致、响应迅速且易于访问的网页界面。