Context selectors

When creating a web page it is often required to put one tags into another. In order to use these tags correctly, use selectors, which work only in the certain context. For example, defining the style for tag <B> only when it is located in the package <P>. Contextual selector consists of simple selectors which are separated with space.

Syntax:
Tag1 Tag2 { ... }
In this case style will be applied to the Tag2, when it is located in the Tag1, as it is shown below.
<Tag1>
<Tag2> ... </Tag>
</Tag1>

The usage of contextual selectors is shown in the example 9.1.

Example 9.1. Contextual selectors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
  <title>Contextual selectors</title>
  <style type="text/css">
   P B { 
    font-family: Times, serif; /* Font family */
    font-weight: bold; /* heavy faced type*/
    color: navy; /* blue colour of the text */
   }
  </style>
 </head> 
 <body>
   <div><b>heavy faced type</b></div>
   <p><b>The heavy faced type of the text and text highlighting takes part simultaneously</b></p>
 </body>
</html>

In this example we see the common use of the tag <B> and its use, when it is put into paragraph <P>. The colour of the text and font are changed, as it is shown in the figure 9.1.


Figure 9.1. Design of the text, depends on the nesting of tags.

Note
It is not obligatory that context selectors consist of only one embedded tag. Depending on the situation it is supposed to apply one and more than one tags, logically embedded into each other.
When using classes and identifiers, contextual selectors give more capacities. It allows defining the style only for that element, which is located in the certain class, as it is shown in the example.

Example 9.2. The use of classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
  <title>Contextual selectors</title>
  <style type="text/css">
   A {
    color: green; /* Green text for all links */
   }
   .menu { 
    padding: 7px; /* Fields around the text */
    border: 1px solid #333; /* Border parameters */
    background: #fc0; /* Background colour */
   }
   .menu A {
    color: navy; /* Dark blue colour of the link, when pointing on it */
   }
   .menu A:hover {
    color: red; /* Red colour of the link, when pointing on it */
   }
  </style>
 </head> 
 <body>
  <div class="menu">
   <a href="link1.html">Russian cuisine </a> |
   <a href="link2.html">Ukrainian cuisine </a> |
   <a href="link3.html"> Caucasian cuisine </a>
  </div>
  <p><a href="link4.html">Other materials on the theme.</a></p>
 </body>
</html>

The result of this example is shown below in the figure 9.2.

Figure 9.2. Links of different colours.

Two types of links are used in this example. The style of the first link is determined by selector A, and it will be applied to the whole page, and the style of the second link (.menu A) is applied only to the links in the element with the class menu.
Using this approach it is easy to operate the style of the similar elements in a kind of images and references, the formatting of which should differ in different web-page areas.