×

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 Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

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 use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

CSS Page-break-inside Attribute

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

3 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

Css Box Model

Introduction The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to...

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

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

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

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.

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.

CSS Align

CSS Center Align Elements CSS center align is used to create align by using block element. You should specify margin: auto;properties. The element will take up the specified width and the remaining space....

2 minutes read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 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 Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

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

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.