EN
CSS - :target peseudo-class example
12 points
:target
pseudo-class is used to describe styles for element which has id
attribute and url hash has been set to id value.
Note: to see effect copy the code to server and type in browser addreses with hashes:
#bokmark-1
e.g.http://localhost#bookmark-1
#bokmark-2
e.g.http://localhost/#bookmark-2
#bokmark-3
e.g.http://localhost/index.htm#bookmark-3
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
:target {
7
background: yellow;
8
}
9
10
</style>
11
</head>
12
<body>
13
<div id="bookmark-1">Article 1</div>
14
<div id="bookmark-2">Article 2</div>
15
<div id="bookmark-3">Article 3</div>
16
<script>
17
18
// this line is only to show how it works inside embedded online example
19
location.href = '#bookmark-2';
20
21
</script>
22
</body>
23
</html>

xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div {
7
margin-top: 10px;
8
padding: 10px;
9
border: 1px solid gray;
10
}
11
12
div:target {
13
background: yellow;
14
}
15
16
</style>
17
</head>
18
<body>
19
<h3>Menu:</h3>
20
<ol>
21
<li><a href="#bookmark-1">Article 1</a></li>
22
<li><a href="#bookmark-2">Article 2</a></li>
23
<li><a href="#bookmark-3">Article 3</a></li>
24
</ol>
25
<h3>Articles:</h3>
26
<div id="bookmark-1">Article 1</div>
27
<div id="bookmark-2">Article 2</div>
28
<div id="bookmark-3">Article 3</div>
29
</body>
30
</html>

xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
.article {
7
margin-top: 10px;
8
border: 1px solid #f2f2f2;
9
font-size: Arial;
10
}
11
12
.article h1 {
13
margin: 0;
14
padding: 5px;
15
background: #f2f2f2;
16
font-size: 16px;
17
}
18
19
.article p {
20
padding: 5px;
21
font-size: 14px;
22
}
23
24
:target {
25
border: 1px solid yellow;
26
}
27
28
:target h1 {
29
background: yellow;
30
}
31
32
</style>
33
</head>
34
<body>
35
<div id="bookmark-1" class="article">
36
<h1>Article 1</h1>
37
<p>Article 1</p>
38
</div>
39
<div id="bookmark-2" class="article">
40
<h1>Article 2</h1>
41
<p>Article 2</p>
42
</div>
43
<div id="bookmark-3" class="article">
44
<h1>Article 3</h1>
45
<p>Article 3</p>
46
</div>
47
<script>
48
49
// this line is only to show how it works inside embedded online example
50
location.href = '#bookmark-2';
51
52
</script>
53
</body>
54
</html>
