×

CSS Pseudo-class

Pseudo-class

The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to insert the effect to an existing element regarding its state. Example, The :hover can be applied to add the unique effects to the element if the user points the cursor on the element.

The pseudo-class name isn’t case-sensitive.

Syntax:

The pseudo-class, along with the colon (:) is used. The syntax is as follows:

selector: pseudo-class 
{  
property: value;  
}  

Here, we are defining several pseudo-classes of CSS. We will explain a few most commonly applied and essential pseudo-classes. CSS pseudo-classes are explained in tabular form as below:

Pseudo-classDescription
:activeIt can be applied to include style to the active element.
:hoverIt includes the unique effect to the element if a user points the cursor on the element.
:linkIt inserts the style to an unvisited link.
:visitedIt inserts the style to a visited link.
:langIt can be applied to represent a language for use inside any specified element.
:focusThis class chooses an element that is focused via a user currently.
:first-childThis class includes a unique effect to the element, which are the first children of other elements.

Let’s consider the pseudo-classes, which are discussed above, with some illustrations.

Pseudo-class- :hover

The :hover pseudo-class includes the style to the element if any user points the cursor on it. If we wish to create it useful, then it should be used after two pseudo-classes, i.e., “:visited” and “:link”.

Example:

<!DOCTYPE html>
<html> 
<head> 
<style> 
body
{ 
text-align:center; 
} 
h1:hover
{ 
color: blue; 
} 
h2:hover
{ 
color: tomato; 
} 
</style> 
</head> 
<body> 
<h1> Hello world </h1> 
<h2> It is an illustration of pseudo class :hover </h2> 
</body> 
</html> 

Output:

CSS Pseudo-class

Pseudo-class- :active

This pseudo-class uses at the time of the element is activated or clicked. It chooses an activated element.

Let’s consider it along with a demonstration as follows:

Example:

<!DOCTYPE html>  
<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
a:active
{  
color: red;  
}  
h1, h2, h3
{  
color: green;
}  
</style>  
</head>  
<body>  
<h1> Hello World! </h1>  
<h2> pseudo-class :active </h2>  
<h3> Click on the following link to examine the effect </h3> 
<a href="#"> Click over the link</a> 
</body>  
</html> 

Output:

CSS Pseudo-class

Pseudo-class- :visited

It chooses a visited link and includes a unique style to them. It has possible values that can be the name of the color in any valid format.

Example:

<!DOCTYPE html>  
<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
a:visited
{  
color: lime;  
}   
</style>  
</head>  
<body>  
<h1> Hello World! </h1>  
<h2> pseudo-class :visited </h2>  
<h3> Click on the following link to examine the effect </h3> 
<a href="#"> Click over the link</a> 
</body>  
</html> 

Output:

CSS Pseudo-class

Pseudo-class- :lang

This class provides support in documents that need several languages. It permits us to portray the unique rules to distinct languages.

Example:

In the following example, we will describe p:lang(fr) that will choose the components having property lang=”fr”.

<!DOCTYPE html>  
<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
p:lang(fr)
{
font-family: Verdana;  
color: tomato;  
}   
</style>  
</head>  
<body>   
<p> Without pseudo-class :lang </p>  
<p lang="fr"> With pseudo-class :lang using the fr value </p> 
</body>  
</html> 

Output:

CSS Pseudo-class

Pseudo-class- :focus

It chooses the components that are focused over via a user currently. Generally, it is applied inside the input components of the triggers and forms if a user clicks over it.

Example:

<!DOCTYPE HTML>
<html>
<style>
form
{
text-align: center;
}
input:focus
{
border:5px solid lightgreen;
box-shadow:10px 10px 10px black;
color: blue;
width:300px;
}
</style>
<body>
<form>
<h1>Name: <input type="text" value="Enter your name"></h1>
</form>
</body>
</html>

Output:

CSS Pseudo-class

Pseudo-class- :first-child

This pseudo-class is used to match the particular component, i.e., the first children of other components. It includes the unique effects to any corresponding component.

Note: We must declare the <!DOCTYPE> on the document’s top to create the pseudo-class, i.e., “:first-child” to implement within the IE8 and its other earlier version.

Let’s consider the following demonstration to understand it.

Example:

<!DOCTYPE HTML> 
<html> 
<head> 
<style> 
h1:first-child
{ 
text-indent: 200px; 
color: red; 
} 
</style> 
</head> 
<body> 
<div> 
<h1> First heading inside the div. It will be sectioned, and its color will be red. </h1> 
<h1> Second heading inside the div, and it will not be influenced.</h1> 
</div> 
</body> 
</html> 

Output:

CSS Pseudo-class

The general tooltip hover

The tooltip describes extra information regarding something if the user points the cursor on the component. Let’s make the tooltip with the use of the pseudo-class “:hover”.

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
body
{ 
text-align: center; 
} 
h2
{ 
display: none; 
background-color: purple; 
color: white; 
padding: 20px; 
} 
div
{ 
font-size: 40px; 
} 
div:hover h2
{ 
display: block; 
} 
</style> 
</head> 
<body> 
<h1> Point your mouse on the below text to examine the effect </h1>  
<div>Hello World!! 
<h2>Welcome to this Page </h2> 
</div> 
</body> 
</html> 

Output:

CSS Pseudo-class

CSS pseudo-classes and classes

The CSS classes may be connected with the pseudo-classes. The syntax is as follows:

Syntax:

selector.class: pseudo-class {  
property: value;  }  

We can consider it with an example as follows:

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
body
{ 
text-align: center; 
} 
div.hello:hover
{ 
color: blue; 
font-size: 40px; 
}  
</style> 
</head> 
<body> 
<h1> CSS pseudo-classes and classes </h1> 
<h2> Point your cursor on the below text </h2> 
<div class="hello"> Hello World!! </p> 
</body> 
</html> 

Output:

CSS Pseudo-class

Related Topics

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time,...

3 minutes read.

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.

CSS Font

CSS Font: CSS has font property, which makes the text more attractive. We can alter the entire look by changing font color, font size, font style, and much more. In...

5 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

CSS Links

CSS Link is used to create styled link. There are various ways to implement it. Styling Links Link can be styled with CSS properties like color, font-family, background etc. Example: <!DOCTYPE html>   <html>   <head>   <style>   a {       color: red;   }   </style>   </head>   <body>   <p><b><a href="https://www.tutorialandexample.com"    target="_blank">This is a link</a></b></p>   </body>   </html> Try Now There are four...

2 minutes read.

CSS Text-decoration

CSS Text-decoration: It is an essential property of CSS that is used to decorate the content of the text. It can add-on the lines above, under, and over the text....

3 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Text-align

Text-align The text-align property of CSS sets the table-cell box’s horizontal alignment or any block element. The text-align property is the same as the CSS property vertical-align but towards any horizontal...

1 minute read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.