target property

Image Tutorials

Definition and Usage

The :target pseudo-class represents the unique element, if any, with an id matching the fragment identifier of the URI of the document..

URIs with fragment identifiers link to a certain element within the document, known as the target element. For instance, here is a URI pointing to an anchor named section2:http://example.com/folder/document.html#section2

The anchor can be any element with an id attribute, e.g. <h1 id=”section2″> in our example. The target element h1 can be represented by the :target pseudo-class.

Note: The id attribute was new in HTML 4 (December 1997).  In old-style HTML <a name=”foo”> is a target element.  The :target pseudo-class applies to those targets as well.


Examples

1 :target { outlinesolid red }  /* draw a red, solid line around the target element */
01 /* example code for userContent.css or any web pages;
02    a red/yellow arrow indicates the target element */
03 :target {
04  -webkit-box-shadow: 0.2em 0.2em 0.3em #888;
05     -moz-box-shadow: 0.2em 0.2em 0.3em #888;
06          box-shadow: 0.2em 0.2em 0.3em #888;
07 }
08 :target:before {
09   font:           70% Arial,"Nimbus Sans L",sans-serif !important;
10   content:        "\25ba";  /* ► */
11   color:          red;
12   background:     gold;
13   border:         solid thin;
14   padding-left:   1px;
15   display:        inline-block;
16   margin-right:   0.13em;
17   vertical-align20%;
18 }

Working with display: none elements…

The :target pseudo-class also works fine with undisplayed elements:

01 <!DOCTYPE html>
02 <html>
03 <head>
04 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
05 <title>:target pseudoclass example</title>
06 <style>
07 #newcomment {
08   display: none;
09 }
10 #newcomment:target {
11   display: block;
12 }
13 </style>
14 </head>
15 <body>
16  <p><a href="#newcomment">Add a comment</a></p>
17  <div id="newcomment">
18   <form name="myForm">
19   <p>Write your comment:<br />
20   <textarea name="myTextarea"></textarea></p>
21   </form>
22  </div>
23 </body>
24 </html>

Creating a pure CSS “lightbox”

The :target pseudo-class is useful to switch on/off some invisible elements. In this way you can create a pure-CSS lightbox (live demo).

001 <!DOCTYPE html>
002 <html>
003 <head>
004 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
005 <title>MDN Example &ndash; CSS Lightbox</title>
006 <style type="text/css">
007 div.lightbox {
008   display: none;
009   position: fixed;
010   left: 0;
011   top: 0;
012   width: 100%;
013   height: 100%;
014 }
015 div.lightbox:target {
016   display: table;
017 }
018 div.lightbox figure {
019   display: table-cell;
020   margin: 0;
021   padding: 0;
022   width: 100%;
023   height: 100%;
024   vertical-align: middle;
025 }
026 div.lightbox figure figcaption {
027   display: block;
028   margin: auto;
029   padding: 8px;
030   background-color: #ddbbff;
031   height: 250px;
032   position: relative;
033   overflow: auto;
034   border: 1px #000000 solid;
035   border-radius: 10px;
036   text-align: justify;
037   font-size: 14px;
038 }
039 div.lightbox figure .closemsg {
040   display: block;
041   margin: auto;
042   height: 0;
043   overflow: visible;
044   text-align: right;
045   z-index: 5001;
046   cursor: default;
047 }
048 div.lightbox figure .closemsg, div.lightbox figure figcaption {
049   width: 300px;
050 }
051 .closemsg::after {
052   content: "\00D7";
053   display: inline-block;
054   position: relative;
055   right: -20px;
056   top: -10px;
057   z-index: 5002;
058   color: #ffffff;
059   border: 1px #ffffff solid;
060   border-radius: 10px;
061   width: 20px;
062   height: 20px;
063   line-height: 18px;
064   text-align: center;
065   margin: 0;
066   background-color: #000000;
067   font-weight: bold;
068   cursor: pointer;
069 }
070 .closemsg::before {
071   content: "";
072   display: block;
073   position: fixed;
074   left: 0;
075   top: 0;
076   width: 100%;
077   height: 100%;
078   background-color: #000000;
079   opacity: 0.85;
080 }
081 </style>
082 </head>
083 <body>
084 <h1>Pure CSS Lightbox</h1>
085 <p>Some sample text&hellip;</p>
086 <p>[ <a href="#example1">Open example #1</a> | <a href="#example2">Open example #2</a> ]</p>
087 <p>Another sample text&hellip;</p>
088 <div class="lightbox" id="example1">
089   <figure>
090     <a href="#" class="closemsg"></a>
091     <figcaption>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec felis enim, placerat id eleifend eu, semper vel sem. Sed interdum commodo enim venenatis pulvinar. Proin mattis lorem vitae diam scelerisque hendrerit. Fusce cursus imperdiet mauris, vitae hendrerit velit dignissim a. Suspendisse potenti. Aenean feugiat facilisis diam, in posuere sapien mattis vel. Proin molestie rutrum diam, pharetra feugiat ligula sollicitudin sed. Etiam cursus diam quis tellus aliquam gravida. Aliquam erat volutpat.<br />
092     Etiam varius adipiscing mi eget imperdiet. Nulla quis vestibulum leo. Integer molestie massa ut massa commodo in blandit purus aliquam. Mauris sit amet posuere massa. Ut a eleifend augue. Proin sodales mauris nec tellus pharetra dictum.</figcaption>
093   </figure>
094 </div>
095 <div class="lightbox" id="example2">
096   <figure>
097     <a href="#" class="closemsg"></a>
098     <figcaption>Cras risus odio, pharetra nec ultricies et, mollis ac augue. Nunc et diam quis sapien dignissim auctor. Quisque quis neque arcu, nec gravida magna. Etiam ullamcorper augue quis orci posuere et tincidunt augue semper. Maecenas varius augue eu orci auctor bibendum tristique ligula egestas. Morbi pharetra tortor iaculis erat porta id aliquam leo cursus. Ut nec elit vel mauris dapibus lacinia eget sed odio.</figcaption>
099   </figure>
100 </div>
101 </body>
102 </html>

Compatibility

Desktop browsers

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 (1.3) 9 9.5 1.3

Mobile browsers

Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 1.0 (1.3) 9.0 9.5 2.0
Rate article