:target
The :target CSS pseudo-class selects the target element of the document. When the document is loaded, the target element is derived using the document's URL fragment identifier.
css
/* Selects document's target element */ :target { border: 2px solid black; }
For example, the following URL has a fragment identifier (denoted by the # sign) that marks the element with the id of setup as the document's target element:
url
http://www.example.com/help/#setup
The following element would be selected by a :target selector when the current URL is equal to the above:
html
<section id="setup">Installation instructions</section>
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 1.3 | 18 | 2 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
CSS
:target {
background-color: #ffffcc;
border-left: 4px solid #f39c12;
padding-left: 8px;
} Live demo
Use cases
-
Using :target
The :target CSS pseudo-class selects the target element of the document.
Cautions
- May not be supported in older browsers.
Accessibility
- Make sure visual changes are conveyed appropriately to assistive technology.