Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Pseudo Class: Pseudo-class in CSS

Understand Pseudo Class: Pseudo-class in CSS with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts...

Table of Contents

Pseudo Class: Pseudo-class in CSS is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Pseudo-Class in CSS helps add special effects to some Selector without using JavaScript or any Script language. For instance,, write CSS to change the color of links when you hover over, change an element's attribute when clicked, format the path when it has not been clicked.

Pseudo Class: Pseudo-class in CSS example image 1

Syntax

The simple syntax for Pseudo-Class in CSS is:

 selector:pseudo-class { 
  property:value; 
 } 

The Pseudo-Class is most often used with links (tags) to create an effect that changes the text state every time the user moves the mouse over it.

There are 4 states equivalent to the 4 Pseudo-Class of the link:

  • a: link {color: blue;}: Show different effects so readers know this is a link.
  • a: visited {color: green;}: The link has been clicked on.
  • a: hover {color: red;}: Hover over the link.
  • a: active {color: navy;}: The link is being clicked on.

Note :

  • a: hover must appear after a: link and a: visited in CSS to create the effect.
  • Similarly, a: active must appear after a: hover.
  • The names used for the Pseudo Class are not case sensitive.
  • The Pseudo Class is different from other Classes in CSS, but they can be combined.

For instance, :

Website homepage Network administrator

The code above defines the link at:

  • Normally it will be green (blue) and underlined is the default value.
  • The link has been clicked by the user, it will be green.
  • The user hovers over (not yet clicked) it will be pink.
  • Users only click and hold the mouse will be red (rarely happens).

Pseudo-class and CSS Class

Pseudo-classes can be combined with CSS classes.

For instance,, when you hover over the link, it changes color:

 a.highlight:hover { 
  color: #ff0000; 
 } 

Pseudo Class: Pseudo-class in CSS example image 2

Website homepage Network Administration (Use Pseudo-class to combine CSS Class)

Technology Village - Administration network (Do not use Pseudo-class to combine CSS Class)

Mouse over

An example of using pseudo-class : hover above

changes the background color of the element.

 div:hover { 
  background-color: purple; 
 } 

Pseudo Class: Pseudo-class in CSS example image 3

Hovering over the div element below will change its background color:

Try hovering here

Create tooltip: simple hover

Create a tooltip : simple hover type of mouse movement on the element

to display an element

 p { 
  display: none; 
  background-color: yellow; 
  padding: 20px; 
 } 
 
 div:hover p { 
  display: block; 
 } 

Pseudo Class: Pseudo-class in CSS example image 4

Hover over here to see what's interesting!

Pseudo Class: Pseudo-class in CSS example image 5

Pseudo-class: first-child in CSS

Pseudo-class : first-child defines a specific element as the first child of another element and adds a special style to that element.

Select all tags

first and style it

 p:first-child { 
 color: purple; 
 } 

To : first-child can work in IE, need to declare the element at the beginning of the document.

For instance,, to

first with purple text, can do the following:

Website Administration Network.

Website Administration Network.

Note: To: first-child can work in IE, need to declare element! DOCTYPE at the beginning of the document.

Pseudo Class: Pseudo-class in CSS example image 6

Choose first in all

and style it

 pi:first-child { 
  color: blue; 
 } 

the example below uses : first-child to define the element as the first child in all

and add special styles for .

Website TipsMake.com . Website TipsMake.com .

Website TipsMake.com . Website TipsMake.com .

Note: To : first-child can work in IE, need to declare the element! DOCTYPE at the beginning of the document.

Pseudo Class: Pseudo-class in CSS example image 7

Select all in

first and style it

 p:first-child i { 
  color: blue; 
 } 

the example below uses : first-child to identify all elements as the first child element in

first and add special style for .

Website TipsMake.com . Website TipsMake.com .

Website TipsMake.com . Website TipsMake.com .

Note: To : first-child can work in IE, need to declare the element! DOCTYPE at the beginning of the document.

Pseudo Class: Pseudo-class in CSS example image 8

Pseudo-class: lang in CSS

Use Pseudo-class : lang in CSS to define a special rule for a particular language in a specific element.

Vietnamese A Vietnamese text is quoted.

This class is very useful for documents presented by many languages and has different conventions.

For instance,:

Vietnamese A Vietnamese text is quoted.

English A quoted English text.

French A French text is quoted.

Note: To : lang can work in IE, need to declare element! DOCTYPE at the beginning of the document.

Pseudo Class: Pseudo-class in CSS example image 9

Previous post: Combinator in CSS

Next lesson: Pseudo-element in CSS

FAQ

What should you know about syntax?

The simple syntax for Pseudo-Class in CSS is:

The Pseudo-Class is most often used with links (tags) to create an effect that changes the text state every time the user moves the mouse over it.

What should you know about pseudo-class and CSS Class?

Pseudo-classes can be combined with CSS classes.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.