CSS3 目标伪类选择器

CSS3 的 :target 伪类选择器是一个独特而强大的选择器,它允许我们选择当前活动的目标元素,即 URL 中 # 后面标识符所对应的元素。这个选择器为创建页面内导航、模态框、标签页等交互效果提供了简洁的解决方案。

:target 伪类概述

:target 伪类选择器用于选择当前活动的目标元素。当用户点击一个带有锚点链接(如 #section1)时,浏览器会滚动到对应的元素,此时该元素就会匹配 :target 选择器。

基本语法

:target {
  /* 样式规则 */
}

element:target {
  /* 样式规则 */
}

工作原理

  1. 用户点击带有锚点的链接
  2. 浏览器滚动到目标元素
  3. 目标元素的 ID 匹配 URL 中的锚点
  4. :target 选择器匹配该元素
  5. 应用相应的样式

基本用法

简单的目标高亮

/* 目标元素高亮 */
:target {
  background: #fff3cd;
  border: 2px solid #ffc107;
  padding: 20px;
  animation: highlight 2s ease;
}

@keyframes highlight {
  0%, 100% {
    background: #fff3cd;
  }
  50% {
    background: #ffe69c;
  }
}

HTML 示例:

<a href="#section1">跳转到第一部分</a>
<a href="#section2">跳转到第二部分</a>

<div id="section1">
  <h2>第一部分</h2>
  <p>这是第一部分的内容...</p>
</div>

<div id="section2">
  <h2>第二部分</h2>
  <p>这是第二部分的内容...</p>
</div>

特定元素的目标样式

/* 特定元素的目标样式 */
#section1:target {
  background: #e3f2fd;
  border-left: 5px solid #2196f3;
}

#section2:target {
  background: #e8f5e9;
  border-left: 5px solid #4caf50;
}

#section3:target {
  background: #fff3e0;
  border-left: 5px solid #ff9800;
}

实际应用场景

1. 页面内导航高亮

/* 章节目标样式 */
.chapter:target {
  background: #f8f9fa;
  border: 2px solid #007bff;
  border-radius: 8px;
  padding: 20px;
  margin: 20px 0;
}

.chapter:target::before {
  content: "📍 当前位置";
  display: block;
  font-weight: bold;
  color: #007bff;
  margin-bottom: 10px;
}

/* 平滑滚动 */
html {
  scroll-behavior: smooth;
}

2. 纯 CSS 模态框

/* 模态框容器 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: none;
  z-index: 1000;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  padding: 20px;
  border-radius: 8px;
  max-width: 500px;
  width: 90%;
  display: none;
  z-index: 1001;
}

/* 目标激活模态框 */
.modal-overlay:target {
  display: block;
}

.modal:target {
  display: block;
}

/* 关闭按钮 */
.close-modal {
  position: absolute;
  top: 10px;
  right: 10px;
  text-decoration: none;
  color: #333;
  font-size: 24px;
}

HTML 示例:

<a href="#modal1">打开模态框</a>

<div id="modal1" class="modal-overlay">
  <div class="modal">
    <a href="#" class="close-modal">&times;</a>
    <h2>模态框标题</h2>
    <p>这是模态框的内容...</p>
  </div>
</div>

3. 标签页切换

/* 标签内容 */
.tab-content {
  display: none;
  padding: 20px;
  background: white;
  border: 1px solid #ddd;
  border-top: none;
}

/* 目标激活标签 */
.tab-content:target {
  display: block;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

HTML 示例:

<div class="tabs">
  <a href="#tab1" class="tab-link">标签 1</a>
  <a href="#tab2" class="tab-link">标签 2</a>
  <a href="#tab3" class="tab-link">标签 3</a>
</div>

<div id="tab1" class="tab-content">
  <h3>标签 1 内容</h3>
  <p>这是标签 1 的内容...</p>
</div>

<div id="tab2" class="tab-content">
  <h3>标签 2 内容</h3>
  <p>这是标签 2 的内容...</p>
</div>

<div id="tab3" class="tab-content">
  <h3>标签 3 内容</h3>
  <p>这是标签 3 的内容...</p>
</div>

4. 手风琴效果

/* 手风琴内容 */
.accordion-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

/* 目标展开内容 */
.accordion-content:target {
  max-height: 500px;
}

.accordion-item {
  margin-bottom: 5px;
}

.accordion-title {
  display: block;
  padding: 15px;
  background: #f8f9fa;
  border: 1px solid #ddd;
  text-decoration: none;
  color: #333;
}

.accordion-title:hover {
  background: #e9ecef;
}

HTML 示例:

<div class="accordion">
  <div class="accordion-item">
    <a href="#section1" class="accordion-title">第一部分</a>
    <div id="section1" class="accordion-content">
      <p>这是第一部分的内容...</p>
    </div>
  </div>

  <div class="accordion-item">
    <a href="#section2" class="accordion-title">第二部分</a>
    <div id="section2" class="accordion-content">
      <p>这是第二部分的内容...</p>
    </div>
  </div>
</div>

5. 图片画廊

/* 画廊容器 */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

/* 缩略图 */
.thumbnail {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
}

/* 大图容器 */
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.9);
  display: none;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

/* 目标显示大图 */
.lightbox:target {
  display: flex;
  animation: zoomIn 0.3s ease;
}

.lightbox img {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
}

.close-lightbox {
  position: absolute;
  top: 20px;
  right: 20px;
  color: white;
  text-decoration: none;
  font-size: 30px;
}

@keyframes zoomIn {
  from {
    transform: scale(0.8);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

高级技巧

1. 多目标选择

/* 多个目标元素 */
#section1:target,
#section2:target,
#section3:target {
  background: #f8f9fa;
  border: 2px solid #007bff;
}

2. 目标链

/* 目标链效果 */
.target-chain {
  opacity: 0;
  transition: opacity 0.3s ease;
}

.target-chain:target {
  opacity: 1;
}

/* 级联目标 */
#section1:target ~ #section2:target {
  background: #e3f2fd;
}

3. 目标动画

/* 目标动画 */
.animate-target {
  transition: all 0.3s ease;
}

.animate-target:target {
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

/* 脉冲动画 */
.pulse-target:target {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(0, 123, 255, 0.7);
  }
  50% {
    box-shadow: 0 0 0 10px rgba(0, 123, 255, 0);
  }
}

4. 目标状态指示器

/* 目标状态指示器 */
.target-indicator {
  position: fixed;
  top: 20px;
  right: 20px;
  background: #007bff;
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  display: none;
}

.target-indicator:target {
  display: block;
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0);
  }
}

浏览器兼容性

:target 伪类选择器在所有现代浏览器中都有良好的支持:

  • Chrome 4+
  • Firefox 3.5+
  • Safari 3.2+
  • Edge 所有版本
  • IE 9+

最佳实践

1. 提供视觉反馈

/* 推荐 - 提供明显的视觉反馈 */
:target {
  background: #fff3cd;
  border: 2px solid #ffc107;
  animation: highlight 2s ease;
}

/* 不推荐 - 没有明显的视觉反馈 */
:target {
  margin-top: 10px;
}

2. 使用平滑滚动

/* 推荐 - 使用平滑滚动 */
html {
  scroll-behavior: smooth;
}

/* 推荐 - 添加滚动偏移 */
:target {
  scroll-margin-top: 80px;
}

3. 提供关闭选项

/* 推荐 - 提供关闭选项 */
.modal:target .close-button {
  display: block;
}

/* 不推荐 - 没有关闭选项 */
.modal:target {
  display: block;
}

4. 考虑移动端体验

/* 推荐 - 移动端优化 */
@media (max-width: 768px) {
  .modal {
    width: 95%;
    padding: 15px;
  }

  .lightbox img {
    max-width: 95%;
    max-height: 95%;
  }
}

常见问题解决

1. 目标元素不在视口内

/* 解决方案:添加滚动偏移 */
:target {
  scroll-margin-top: 80px;
}

2. 多个目标同时激活

/* 解决方案:使用唯一 ID */
#unique-target:target {
  /* 样式 */
}

3. 目标样式不生效

/* 解决方案:确保 ID 匹配 */
/* 检查 HTML 中的 ID 和 CSS 中的选择器是否一致 */
#section1:target {
  /* 样式 */
}

总结

:target 伪类选择器是 CSS3 中一个独特而强大的选择器:

主要特点:

  • 选择当前活动的目标元素
  • 基于锚点 URL 匹配
  • 支持动画和过渡效果
  • 无需 JavaScript 实现

实际应用:

  • 页面内导航高亮
  • 纯 CSS 模态框
  • 标签页切换
  • 手风琴效果
  • 图片画廊
  • 交互式组件

最佳实践:

  • 提供明显的视觉反馈
  • 使用平滑滚动
  • 提供关闭选项
  • 考虑移动端体验
  • 确保浏览器兼容性

优势:

  • 简化代码实现
  • 提高性能
  • 增强用户体验
  • 减少 JavaScript 依赖

掌握 :target 伪类选择器,能够让我们创建各种交互效果,而无需依赖 JavaScript,大大简化了代码实现,提高了页面性能。