Adjacent Sibling Selectors
These selectors allow the selection of specific elements that come directly after another specific element. However, the elements are of the same level in the element hierarchy. In the following rule, you will be able to select all p elements that comes immediately after h2 elements but not other p elements.
h2 + p {
...
}
(Note: Adjacent Sibling selectors are not supported in IE 6 and below)
Pseudo-Classes
These classes are used to provide styles to various states of elements. Its most common usage is in styling links states. Examples are given below:
:link—the default state of links.
:visited—links already visited in the browser.
:focus—links that currently have the keyboard cursor within them.
:hover—links that are currently being hovered over by the mouse pointer.
:active—a link that is currently being clicked on.
According to the following rule the default links will be blue. The underlining under the link will disappear when hovered over. In order to make the same effect take place when links are focused via the keyboard, :hover rule is duplicated with :focus. If a link is already visited, the color of the link will turn gray and finally, when a link is active, it is emboldened.
a:link {
color: blue;
}
a:visited {
color: gray;
}
a:hover, a:focus {
text-decoration: none;
}
a:active {
font-weight: bold;
}
Note that the above specified rules should be in the same order. It may not work otherwise. This is because specificity of a rule in a style sheet may override the earlier rules. You will come to know more about it later.
The :focus pseudo-class also comes in handy as a usability aid in forms. For instance, we can highlight the input field that has the active blinking cursor with a rule like given below:
input:focus {
border: 2px solid black;
background color: lightgray;
}
Now, lets have a look at :first-child. This pseudo-class selects any instance of the element that is first child element of its parent. As given in the example below, the rule selects the first list item in any list and makes its text bold.
li:first-child {
font-weight: bold;
}
Last but not the least, lets look at :lang pseudo-class. This class selects elements whose languages have been set to a specified language using the lang attribute. An example is given below.
<p lang="en-US">A paragraph of American text, gee whiz!<p>
Could be selected using the following:
p:lang((en-US) {
...
}