Pseudo Selectors in css

Pseudo Selectors in css specify a special state of current selector. Pseudo Selectors are divided as Pseudo Class and Pseudo elements. To use pseudo selectors, colon (:) is used after selector.

Pseudo Class can change properties of an element on particular event, like mouseover, focus etc.

Pseudo Elements are used to call particular child of parent, like first-line, first-letter etc.

Pseudo Class and Pseudo Elements

Pseudo Elements

  1. :first-line
  2. :first-letter
  3. :first-child
  4. :before
  5. :after

:link pseudo class is used to change properties of an unvisited link. once clicked, the link pseudo property will not show as link is visited. Default link color is blue. link pseudo class was introduced in css level 1.


	a:link{ color:red}

<a href="#">Link</a>                                    


:visited pseudo class works only when a hyperlink is visited. Default color of visited hyperlinks is purple. visited pseudo classcan change color and properties of visited links. visited pseudo class was also introduced in css level 1.

a:link{ color:red}                /* Unvisited Link*/
a:visited{ color:orange}          /* Visited Link*/

<a href="#">Link</a>                                    


:active pseudo class works when mouse left key is pressed, as soon as he released mouse key, :active stops. On touch devices, :active will works on touch.

button:active{ background:blue}            



:hover class works when user mouseover on an element. On mouseout, it comes back to its original form. :hover is meant for non touch devices only. On touch devices, :hover will conflict with :active.

    button{ background:white}                
    button:hover{ background:blue; color:white}    
    button:active{ background:red; color:white}                



:focus class change style of an element only when that element is focused. All hyperlinks and form controls support focus. Both mouse and tab key can focus an element.

Default Text Box

Text Box with changed focus

Link 1 Link 2 Default Anchor with focus

   #name{ }
    #name:focus{ outline:none; border:2px solid red} 
    a:focus{ outline: 2px dotted blue;}

<input type="text" > 
<input id="name" type="text" >
<a href="#">Link 1</a>                                    
<a href="#">Link 2</a>                                    


:first-line pseudo element works only for the first line of a paragraph or text.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis nulla, magni reprehenderit fugiat suscipit repellat necessitatibus nisi doloremque ratione dolores assumenda? Illum tempore aspernatur ipsam excepturi distinctio quia tenetur impedit.

p{ color:blue}      	    /* For p tag*/
p:first-line{ color:red}    /* First line of p tag*/

<p>Inteligula congue id elis donec sce sagittis intes id laoreet aenean leo sem massawisi condisse leo sem ac. Tincidunt nibh quis dui fauctor et.</p>                                    


:first-child pseudo element selects first child of the parent only. Means only first child of parent will follow this class.

  • One
  • Two
  • Three
  • Four
  • Five

ul li{ color:blue}                 /* For all li elements*/
ul li:first-child{ color:yellow}   /* Only First li*/



:first-letter pseudo element is used to select the first letter of a text element.

This is a paragragh

	p{ color:blue; font-size:16px}
	p:first-letter{ font-size:24px; color:red}    

<p>This is a paragraph</p>                                    

To see more pseudo class and pseudo elements introduced in css3, click below

CSS3 Selectors