CSS Pseudo-Class

  • Avinash
  • 1
  • Aug 18, 2011
  • Web Design

A pseudo-class is similar to class in HTML. But it’s not declared in Markup explicitly.

A pseudo-class starts with the colon(:). There is no white space appear between the selector and colon. Also there should be no white space after the colon.

If we start from the beginning, then CSS1 introduced the :link, :visited and :active pseudo classes but it were limited to HTML link (a) elements only. These pseudo-classes defines the state of the link like already visited, normal behaviour or active which is currently being selected.

Then CSS2 explored the range of the pseudo-classes and made available to all the elements. In this version :active joins into two other dynamic classes like :hover and :focus. :hover defines the element state by moving the mouse moves over it and :focus defines the state when cursor is focused on that element.

A simple selector can contain more than one pseudo-class if the pseudo-classes are not mutually exclusive. Like, the selectors a:link:hover and a:visited:hover are valid, but a:link:visited is not because :link and :visited are mutually exclusive.

The order of declaring the pseudo-classes is more important. The :link and :visited are comes first generally. Next comes the :focus and :hover and :active usually comes last.

Please find the below code for declaring the pseudo-class for element.

a:link { CSS Style Here } a:visited { CSS Style Here } a:focus { CSS Style Here } a:hover { CSS Style Here } a:active { CSS Style Here }
a:link
{
 CSS Style Here
}
a:visited
{
 CSS Style Here
}
a:focus
{
 CSS Style Here
}
a:hover
{
 CSS Style Here
}
a:active
{
 CSS Style Here
}

Note: If you want to apply special styling to the hover state of a link that also has keyboard input focus, use a:focus:hover.

Related Posts

Written by Avinash

Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.

View all posts by: