×

CSS Pseudo-elements

Pseudo-elements

The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be used for styling the particular part of the element, although a pseudo-class is used for styling any element.

For example, the pseudo-elements are used to design any first line or any first letter of the element. Also, these elements can include the text before or after an element.

Syntax:

selector::pseudo-element 
{
property: value;  
}  

We have described the double colon in the above syntax. It is a substitute for the pseudo-elements.

 It can be represented as a distinction between the pseudo-classes and pseudo-elements. Rather than the use of one color notation, it is suggested to apply the double color notation (like ::pseudo-element).

Some of the essential pseudo-elements in CSS are described in the table as below:

Pseudo-elementDescription
::first-letter (:first-letter)This element is used to choose the first character of the content.
::first-line (:first-line)This element is used to choose the content’s first line.
::before (:before)This pseudo-element includes something before any content of an element.
::after (:after)This pseudo-element includes something after any content of an element.
::selectionIt chooses an element’s area, i.e., chosen by a user.

Let’s explain the above elements with a demonstration.

Pseudo-element- ::first-letter

As its name suggested, it will affect the text’s first letter. It may be used to the block-level elements only. Rather than supporting every property of CSS, it can support only few properties of CSS. These properties are listed and discussed below:

  • Color-properties (like color)
  • Font-properties (like font-color, font-size, font-family, font-style, and many others)
  • Margin-properties (like margin left, margin-right, margin-top, and margin-bottom)
  • Border properties (like border-width, border-color, border left, border-bottom, border-right, border-top, and many others)
  • Padding properties (like padding-left, padding-right, padding-top, and padding-bottom)
  • Background properties (like background-position, background-image, background-repeat, and background-color)
  • Text related properties (like text-decoration, text-transform, and text-shadow)
  • Another property is vertical-align (if float value is ‘none’ only) line-spacing, line-height, and word-spacing

Let’s consider an example.

Example:

<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
h1::first-letter
{ 
font-family: Lucida Calligraphy; 
font-size: 3cm; 
color: green; 
text-shadow: 5px 8px 9px red; 
}  
h1
{ 
color: blue; 
} 
</style>  
</head>  
<body>  
<h1> Welcome to this Page </h1>  
<h2> It is an illustration of pseudo-element ::first-letter. </h2> 
</body>  
</html> 

Output:

Pseudo-elements

Pseudo-element- ::first-line

This element is the same as the pseudo-element ::first-letter, although it can affect the whole line. It can insert a unique effect to any text’s first line. The following properties of CSS will be supported by this pseudo-element:

  • Color properties (like color)
  • Font-properties (like font-color, font-size, font-family, font-style, and many others)
  • Background properties (like background-position, background-image, background-repeat, and background-color)
  • Another CSS property: text-decoration, text-transform, vertical-align, line-height, letter-spacing, and word-spacing.

Consider the below example:

Example:

<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
h1::first-line
{ 
font-family: Lucida Calligraphy; 
font-size: 1cm; 
color: green; 
text-shadow: 5px 8px 9px red; 
}  
</style>  
</head>  
<body>  
<h1> Welcome to the tutorial and example. This site is designed so that the students may study computer science technologies easily. It is developed to provide in-depth and easy tutorials on many technologies.</h1>  
<h2> It is an illustration of pseudo-element ::first-line. </h2> 
</body>  
</html> 

Output:

Pseudo-elements

Pseudo-element- ::before

It permits us to include anything before the content of an element. It is applied to insert anything before any element’s specific part. It is applied with the CSS content property, generally.

Also, we can include the images or regular strings before any content with the use of pseudo-element ::before.

Consider the below example:

Example:

<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
h1::before
{  
content: "'Hello World.'"; 
color: blue; 
}  
</style>  
</head>  
<body>  
<h1>Welcome to the Tutorial and Example. </h1>  
<h2> It is an illustration of pseudo-element ::before. </h2> 
<h3> The "Hello World" has inserted with the use of the ::before pseudo-element in the first line. </h3> 
</body>  
</html> 

Output:

Pseudo-elements

Pseudo-element- ::after

It executes similarly as the pseudo-element ::before, although it can include any text after the element’s text. It is applied to insert anything after any element’s specific part. It is applied with the CSS content property, generally.

Also, we can include the images or regular strings after any content with the use of pseudo-element ::after.

Consider the below example:

Example:

<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
h1::before
{  
content: "'Welcome to the Tutorial and Example.'"; 
color: blue; 
}  
</style>  
</head>  
<body>  
<h1> Hello World!! </h1>  
<h2> It is an illustration of pseudo-element ::after. </h2> 
<h3> The "Welcome to the Tutorial and Example." has inserted with the use of the ::before pseudo-element in the first line. </h3> 
</body>  
</html> 

Output:

Pseudo-elements

Pseudo-element- ::selection

It is applied to design the element’s part that is chosen by a user. We may apply the CSS properties listed below:

  • color
  • background-color
  • Another property includes outline, cursor, etc.

Consider the below example:

Example:

<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
h1::selection
{  
color: blue; 
}  
</style>  
</head>  
<body>  
<h1> Hello World!! </h1>  
<h2> Choose the content in the first line to examine the effect. </h2> 
<h3> It is an illustration of pseudo-element ::selection. </h3> 
</body>  
</html> 

Output:

Pseudo-elements

CSS pseudo-element and Classes

The pseudo-elements in CSS are combined with the classes of CSS to design any specific element containing that class. To combine the CSS classes with CSS pseudo-elements, we can apply the following syntax:

Syntax:

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

Consider the below example:

Example:

The below example will influence the <h1> element’s first letter that contain class= “example” instead of influencing every <h1> element.

<html>  
<head>  
<style>  
body
{ 
text-align: center; 
} 
h1.example::first-letter
{  
color: blue;
font-size: 2cm; 
font-family: Lucida Calligraphy; 
}  
</style>  
</head>  
<body>  
<h1 class= "example"> Hello World!! </h1>  
<h1> Welcome to the Tutorial and Example. </h1>  
<h3> It is an illustration of CSS classes with pseudo-element. </h3> 
</body>  
</html> 

Output:

Pseudo-elements

Related Topics

CSS Filter

CSS Filter CSS filter is used to apply visual effects on the images, text, and other factors of the webpage. This filter property in CSS allows us to admit the effects like...

7 minutes read.

How to Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

CSS Table

CSS table provides better look and feel. There are various properties of CSS table that are given below. border border-collapse padding width height text-align color background-color CSS Table Border You can set...

4 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

CSS Opacity

The CSS Opacity property is used to set the clarity of the image. It is defined as degree and specify the transparency of an element. Note: Opacity value will be less...

1 minute read.

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 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 Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 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: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

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 Background

CSS Background The properties of CSS background are applied to describe the effects of background on the element. Here are five properties of background that affects various elements of HTML: Background-colorBackground-imageBackground-repeatBackground-attachment Background-position Background-color The color of the background...

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

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

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

Combine Background Image with Gradient Overlay

With the help of CSS gradient, we can display smooth and straightforward transitions between two or more colours. We can add the background images by combining the gradient property with...

2 minutes read.

Is CSS a programming language?

 A programming language is a set of grammatical rules and a vocabulary for commanding a machine to accomplish specified tasks. High-level languages like BASIC, C, C++, COBOL, Java, FORTRAN, Ada...

4 minutes read.