Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
A CSS pseudo-class is a keyword added to selectors that specifies a special state of the element. For example :hover will apply a style when the user hovers over the element specified by the selector.
Pseudo-classes, together with pseudo-elements, let you apply a style to an element not only in relation to the content of the document tree, but also in relation to external factors like the history of the navigator (:visited, for example), the status of its content (like :checked on some form elements), or the position of the mouse (like :hover which lets you know if the mouse is over an element or not).
Pseudo link classes alow us to manipulate the way that links look like in their various states. There are four main link pseudo classes.
Note that some of the link states are mutually exclusive (e.g. a given link cannot be simultaneously visited and unvisited) and others are not (while a link is active, it will also be either visited or unvisited). Where mutual exclusion does not occur, the selector which occurs further from the top of the style sheet prevails. To ensure that they work as planned, link classes should always be declared in the order shown above.
Write code to change a webpage's active hyperlinks to red and visited hyperlinks to green, as shown here.
The use of pseudo classes is not restricted to hyperlinks. Pseudo classes can be used on any element.
Write code to change the cursor to a crosshair when it hovers over an image, as shown here.
Write code to allow the user to see change the cursor to a bullseye image when the mouse hovers over an image, as shown here.
<!DOCTYPE html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pseudo class :target Example</title> <style> :target { background-color:#CCC; border: thin solid black; } </style> </head> <body> <a href = '#dkit'>Dkit</a><br> <a href = '#dcu'>DCU</a><br> <a href = '#maynooth'>Maynooth</a><br> <div id='dkit'> <h1>Dundalk Institute of Technology</h1> <p>Some text about DkIT.</p> </div> <div id='dcu'> <h1>Dublin City University</h1> <p>Some text about DCU.</p> </div> <div id='maynooth'> <h1>Maynooth University</h1> <p>Some text about Maynooth University.</p> </div> </body> </html>
Modify the code above, so that the <h1> of the selected <div> has a black background with white text, as shown here.
Position pseudo classes are dependent on the position of elements. We can treat the first, last, odd, even, or any other position of a particular element differently to other elements. For example, we could get the odd rows of a table to have a different background colour to the even rows.
The example below uses the :nth-child(odd) and :nth-child(even) to colour the rows of a list (Run Example):
<!DOCTYPE html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pseudo class :nth-child Example</title> <style> ul { list-style-type:none; margin:0px; padding:0px; } li:nth-child(odd) { background-color:#FFC; } li:nth-child(even) { background-color:#CFF; } </style> </head> <body> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul> </body> </html>
Improve the code above, so that the currently highlighted row is displayed in a different colour, as shown here.
Adjust the code from the previous example, so as to include a border with rounded corners, as shown here.
:root { background-colour:red; }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.