CSS :target 选择器

CSS :target 伪类 代表一个唯一的页面元素(目标元素),其id 与当前URL片段匹配 .


实例

突出显示活动的 HTML 锚:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. :target {
  6. border: 2px solid #D4D4D4;
  7. background-color: #e5eecc;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h2>这是一个标题</h2>
  13. <p><a href="#news1">跳转到新内容 1</a></p>
  14. <p><a href="#news2">跳转到新内容 2</a></p>
  15. <p>单击上面的链接,:target 选择器高亮显示当前活动的 HTML 锚文本。</p>
  16. <p id="news1"><b>新内容 1...</b></p>
  17. <p id="news2"><b>新内容 2...</b></p>
  18. </body>
  19. </html>

浏览器支持

表格中的数字注明了完全支持该选择器的首个浏览器版本。

选择器
:target4.09.03.53.29.6

定义和用法

URL 带有后面跟有锚名称 #,指向文档内某个具体的元素。这个被链接的元素就是目标元素(target element)。

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


CSS 语法

:target { css declarations;}


更多实例

创建选项卡式菜单:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .tab div {
  6. display: none;
  7. }
  8. .tab div:target {
  9. display: block;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div class="tab">
  15. <a href="#link1">Link 1</a>
  16. <a href="#link2">Link 2</a>
  17. <a href="#link3">Link 3</a>
  18. <div id="link1">
  19. <h3>Content to Link 1</h3>
  20. <p>Hello World!</p>
  21. </div>
  22. <div id="link2">
  23. <h3>Content to Link 2</h3>
  24. <h4>Great success!</h4>
  25. </div>
  26. <div id="link3">
  27. <h3>Content to Link 3</h3>
  28. <p>Yeah!</p>
  29. </div>
  30. </div>
  31. </body>
  32. </html>

创建模态(对话框):

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. /* Add animation (Chrome, Safari, Opera) */
  6. @-webkit-keyframes example {
  7. from {top:-100px;opacity: 0;}
  8. to {top:0px;opacity:1;}
  9. }
  10. /* Add animation (Standard syntax) */
  11. @keyframes example {
  12. from {top:-100px;opacity: 0;}
  13. to {top:0px;opacity:1;}
  14. }
  15. /* The modal's background */
  16. .modal {
  17. display: none;
  18. position: fixed;
  19. left: 0;
  20. top: 0;
  21. width: 100%;
  22. height: 100%;
  23. overflow: auto;
  24. background-color: rgb(0, 0, 0);
  25. background-color: rgba(0, 0, 0, 0.4);
  26. }
  27. /* Display the modal when targeted */
  28. .modal:target {
  29. display: table;
  30. position: absolute;
  31. }
  32. /* The modal box */
  33. .modal-dialog {
  34. display: table-cell;
  35. vertical-align: middle;
  36. }
  37. /* The modal's content */
  38. .modal-dialog .modal-content {
  39. margin: auto;
  40. background-color: #f3f3f3;
  41. position: relative;
  42. padding: 0;
  43. outline: 0;
  44. border: 1px #777 solid;
  45. text-align: justify;
  46. width: 80%;
  47. box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  48. /* Add animation */
  49. -webkit-animation-name: example; /* Chrome, Safari, Opera */
  50. -webkit-animation-duration: 0.5s; /* Chrome, Safari, Opera */
  51. animation-name: example;
  52. animation-duration: 0.5s;
  53. }
  54. /* The button used to close the modal */
  55. .closebtn {
  56. text-decoration: none;
  57. float: right;
  58. font-size: 35px;
  59. font-weight: bold;
  60. color: #fff;
  61. }
  62. .closebtn:hover,
  63. .closebtn:focus {
  64. color: #000;
  65. text-decoration: none;
  66. cursor: pointer;
  67. }
  68. .container {
  69. padding: 2px 16px;
  70. }
  71. header {
  72. background-color: #5cb85c;
  73. font-size: 25px;
  74. color: white;
  75. }
  76. footer {
  77. background-color: #5cb85c;
  78. font-size: 20px;
  79. color: white;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <a href="#id01">Open Modal</a>
  85. <div id="id01" class="modal">
  86. <div class="modal-dialog">
  87. <div class="modal-content">
  88. <header class="container">
  89. <a href="#" class="closebtn">×</a>
  90. <h2>Modal Header</h2>
  91. </header>
  92. <div class="container">
  93. <p>Some text in the modal.</p>
  94. <p>Some text in the modal.</p>
  95. </div>
  96. <footer class="container">
  97. <p>Modal footer</p>
  98. </footer>
  99. </div>
  100. </div>
  101. </div>
  102. </body>
  103. </html>

分类导航