×

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

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

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 Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

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.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.

CSS Float

CSS Float: The float property of CSS is the positioning property. This property is applied to elements to push them either to left or right. It permits for wrapping facility...

4 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

Untitled Reusable Block

  ...

1 minute read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Transition

CSS Transition The transitions in CSS are influenced, included to modify the component from one design to another, without the use of the JavaScript or Flash. We should describe two of the...

2 minutes 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.

CSS Ellipsis

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. Here, we will discuss the following: What is CSS Ellipsis? Syntax for CSS ellipsis How to...

3 minutes read.

CSS Inline

What is CSS? CSS is an acronym for Cascading Style Sheets. CSS is the language that we use to style an HTML document. It specifies how HTML components should appear. CSS...

4 minutes read.