Pseudo-classes allow adding CSS rules to certain parts of the same selector.
For example, when you define a style for a HTML tag or a particular class, the content of all those tags or class will have the same style. If you want to define a different style for the first (or last) element of those tags or for the first content of a group of elements with the same class, you can accomplish this by using pseudo-classes.
Also, they can change the graphic style of items when the mouse is acting on them.

- Syntax:
selector:pseudo-class { property: value; }
Or, used with class:
selector.class:pseudo-class { property: value; }

List with Pseudo-classes

To understand CSS pseudo-classes, how they work and what is their effect, study the following examples.

Pseudo-classes with type selector

Type selectors refer to HTML tags which they define (eg.: p for <p>, li for <li>, div for <div>, etc.).
In the following example is used first-child with paragraphs.

p:first-child {

<h4>Example first-child</h4>
<blockquote>Apply css style to first P tag.</blockquote>

<p>A text within the first paragraph</p>
<p>Content in the second paragraph</p>
<p>Text content in the third paragraph</p>
• Other example, with the hover value applied to <li> tags.
li:hover {

<h4>Example hover</h4>
<p>When you place the mouse over each LI, it will have a green background.</p>

 <li>Free courses and tutorials</li>
 <li>Web site:</li>
 <li>Web site:</li>

Pseudo-classes with class

Pseudo-classes are not the same with classes. The classes refer to the value of a class attribute, in stylesheets are added after a (.) character.
- In the next example are used lang and last-child applied to a class '.test':
.test:last-child {
.test:lang(en) {

<h4>Example lang and last-child</h4>
<p>The CSS style defined for <b>test: last-child</b> will be applied to the last LI that has class 'test'.<br>
The CSS rule added to <b>.test:lang(en)</b> is applied only to the element that has both: <em>class='test'</em> and <em>lang='en'</em> attributes.</p>

 <li class='test'>HTML:</li>
 <li class='test' lang='en'>CSS:</li>
 <li class='test'>JavaScript:</li>
• Here is other example, where are combined a class (test), a type selector <em> tag and 'first-child'.
/* Applies to the first EM tag of every element with class='test' */
.test em:first-child {
font-weight: bold;
color: blue;

<p>A CSS style will be applied to first EM tag of every 'test' class.</p>

<p class='test'>Paragraph with three EM tags: <em>blue text</em>, a <em>second EM tag</em>, and another <em>em text</em>.</p>
<p class='test'>Another paragraph: <em>a bold-italic blue string</em>, and a second <em>em sub-string</em>.</p>

Pseudo-classes with ID and form elements

In the fallowing example is used the hover pseudo-class applied to an ID, and the focus pseudo-class to a class (test).
#idb:hover {
.test:focus {

<h4>Example hover and focus</h4>
<p>When the mouse is over the button it changes its background color.<br>
When you click on the text areas of the form it changes their background color.</p>

<form action='' method='post'>
 <input type='text' class='test' /><br />
 <textarea cols='20' rows='5' class='test'></textarea><br />
 <input type='button' value='Button' id='idb' />

Daily Test with Code Example

Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
Indicate the PHP variable that contains data added in URL address after the "?" character.
if(isset($_GET["id"])) {
  echo $_GET["id"];

Last accessed pages

  1. Contact page - CoursesWeb (7906)
  2. Tabs effect with CSS (8620)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (57370)
  4. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (14440)
  5. Insert, Select and Update NULL value in MySQL (36592)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1826)
  2. Contact page - CoursesWeb (1755)
  3. Tabs effect with CSS (1748)
  4. Insert, Select and Update NULL value in MySQL (1030)
  5. PHP getElementById and getElementsByTagName (707)