导航
×
   ❮   
HTML CSS JavaScript PHP Go Sass W3C Colors ECMS

CSS :target 选择器


实例

突出显示活动的 HTML 锚点

:target {
  border: 2px solid #D4D4D4;
  background-color: #e5eecc;
}
亲自试一试 »

定义和用法

带有 # 及其后跟锚点名称的 URL 会链接到文档中的某个元素。链接到的元素就是目标元素。

:target 选择器可用于样式化当前活动的目标元素。

版本 CSS3

浏览器支持

表格中的数字表示首次完全支持该选择器的浏览器版本。

选择器
:target 4.0 9.0 3.5 3.2 9.6

CSS 语法

:target {
  css 声明;
}

更多实例

实例

创建标签页菜单

.tab div {
  display: none;
}

.tab div:target {
  display: block;
}
亲自试一试 »

实例

创建模态框(对话框)

/* 模态框的背景 */
.modal {
  display: none;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.4);
}

/* 目标元素时显示模态框 */
.modal:target {
  display: table;
  position: absolute;
}

/* 模态框 */
.modal-dialog {
  display: table-cell;
  vertical-align: middle;
}

/* 模态框的内容 */
.modal-dialog .modal-content {
  margin: auto;
  background-color: #f3f3f3;
  position: relative;
 padding: 0;
  outline: 0;
  border: 1px #777 solid;
  text-align: justify;
  width: 80%;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
亲自试一试 »

Copyright ©2020-2026 freew3c.com All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。