A CSS pseudo-class adds selectors that specify a special state of the element to be selected and used to add special effects to some selectors. A pseudo-class allow you to style Content Dynamically based on its position in the document or state.
Syntax:
selector: pseudo-class { property: value; }
selector: pseudo-element {property: value}
selector. class: pseudo-element {property: value}
The most-commonly used pseudo-classes are:
- :link pseudo-class: The given example shows how to use the :link class to set the link color. Possible values could be any color name in the valid format.
Example
- <html>
- <head>
- <style type="text/css">
- a: link {color: red}
- </style>
- </head>
- <body>
- <a href="">Pseudo class with link property</a>
- </body>
- </html>
Output: The preceding code produces the following Red link.
- :visited pseudo-class: The given example shows how to use the :visited class to set the link color. Possible values could be any color name in the valid format.
Example
- <html>
- <head>
- <title></title>
- <style type="text/css">
- a: visited {color: red}
- </style>
- </head>
- <body>
- <a href="#" class="a1">Click this link</a>
- </body>
- </html>
Output: The preceding code produce the following link. When you click this link it will change its color to Red.
- :hover pseudo-class: The given example shows how to use the :hover class to change the color of links from Blue to Green when we hover a mouse pointer over that link.
Example
- <html>
- <head>
- <title></title>
- <style type="text/css">
- a: hover {color: green}
- </style>
- </head>
- <body>
- <a href="#" class="a1">Bring mouse on me</a>
- </body>
- </html>
Output: The preceding code produce the following link, when you click this link it will change its color to Green.
- :active pseudo-class: The given example shows how to use the :hover class to change the color of links from Blue to Green when we hover the mouse pointer over that link.
Example
- <html>
- <head>
- <title></title>
- <style type="text/css">
- a: active {color: green}
- </style>
- </head>
- <body>
- <a href="#" class="a1">Please activate me</a>
- </body>
- </html>
Output: The preceding code produces the following link. When you click this link it will change its color to Green only for the activation of link:
The most commonly used pseudo-elements are:
- :first-line pseudo-element: The following example shows how to use the :first-line element to add special effects to the first line of elements.
Example
- <html>
- <head>
- <title></title>
- <style type="text/css">
- p: first-line { text-decoration: underline; color: blue }
- </style>
- </head>
- <body>
- <p class="p1">welcome to my program , hi guys my name is mohit and this is example shows how to select first line of a paragraph.
- You can apply this property of peasudo class only on the first line. This line remains same because this is in the second line of the paragraph.</p>
- </body>
- </html>
Output: The preceding code produce the following change only on the first line.
- :first-letter pseudo-element: The following example shows how to use the :first-letter element to add special effects to the first letter of the line.
Example
- <html>
- <head>
- <title></title>
- <style type="text/css">
- p: first-letter { text-decoration: underline; color: red }
- </style>
- </head>
- <body>
- <p >Welcome to my program , hi guys my name is mohit and this is example shows how to select first line of a paragraph.
- You can apply this property of Pseudo class only on the first line. This line remains same because this is in the second line of the paragraph.</p>
- </body>
- </html>
Output: The preceding code produces the following change only on the first letter.
The most commonly used pseudo-elements with a pseudo-class are as follows.
- :first-letter pseudo-element with pseudo class: The following example shows how to use the :first-letter pseudo-element to add special effects to the first letter with selected elements of the line.
Example
- <html>
- <head>
- <title></title>
- <style type="text/css">
- p.p1:first-letter { font-size: 30px; color: blue }
- </style>
- </head>
- <body>
- <p class="p1">Welcome to my program , hi guys my name is mohit and this is example shows how to select first line of a paragraph.
- You can apply this property of peasudo class only on the first line. This line remains same because this is in the second line of the paragraph.</p>
- </body>
- </html>
Output: The preceding code produces the following change only on the first letter with id selector.
Conclusion
I hope this article provides you a better understanding of pseudo-class and pseudo-elements.