CSS target Selector (:target)

What is CSS :target Selector?

CSS :target selector matches all element whose href hash (#) URL and that URL name exactly element id name.
This is really interesting selector check :target selector example to come exactly idea how it work.

Syntax

General syntax of CSS :target selector,

:target {
  property: value;
  property: value;
  ...
}

element:target {
  property: value;
  property: value;
  ...
}

Example

Match all element whose href hash (#) URL and that URL name exactly element id name.

<!DOCTYPE html>
<html>
<head>
  <title>CSS :target Selector</title>
  <style type="text/css">
    :target {
      color: red;
    }
    h3:target {
      color: blue;
    }
    p:target {
      color: orange;
    }
  </style>
</head>
<body>
  <a href="#one">First</a>
  <a href="#two">Second</a>
  <a href="#three">Third</a>

  <div id="one">Third</div> 
  <h3 id="two">Second</h4> 
  <p id="three">Third</p>
</body>
</html>

Run it...   »

Browser Compatibility

  • Google Chrome 2+
  • Mozilla Firefox 1.3+
  • Internet Explorer 9+
  • Opera 9.5+
  • Safari 1.3+

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.