loading

CSS Pseudo-element


What are Pseudo-Elements?

To style specific portions of an element, utilize a CSS pseudo-element.

It can be utilized, for instance, to:

  • Style an element’s initial letter or line
  • Add content either before or after an element’s content.

Syntax

The way pseudo-elements are written:

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

The ::first-line Pseudo-element

A text’s opening line can have a unique style applied to it by using the ::first-line pseudo-element.

The first line of the text in each of the <p> elements is formatted like the example below:

Example

				
					p::first-line {
  color: #ff0000;
  font-variant: small-caps;
}
				
			

Note: It should be noted that only block-level components may use the ::first-line pseudo-element.

The ::first-line pseudo-element has the following characteristics:

  • font properties
  • color properties
  • background properties
  • word-spacing
  • letter-spacing
  • text-decoration
  • vertical-align
  • text-transform
  • line-height
  • clear

Notice the double colon notation ::first-line versus :first-line

In CSS3, the single colon was substituted with a double colon for pseudo-element notation. W3C made an attempt to differentiate between pseudo-elements and pseudo-classes with this approach.

In CSS2 and CSS1, the pseudo-class and pseudo-element syntaxes were both single-colon based.

For CSS2 and CSS1 pseudo-elements, the single-colon syntax is appropriate for backward compatibility.

The ::first-letter Pseudo-element

A text’s initial letter can have a unique style applied to it by using the ::first-letter pseudo-element.

The first letter of the text in each of the <p> elements is formatted in the example below:

Example

				
					p::first-letter {
  color: #ff0000;
  font-size: xx-large;
}
				
			

Note: Only block-level elements are eligible to use the::first-letter pseudo-element.

The following properties apply to the ::first-letter pseudo- element: 

  • font properties
  • color properties 
  • background properties
  • margin properties
  • padding properties
  • border properties
  • text-decoration
  • vertical-align (only if “float” is “none”)
  • text-transform
  • line-height
  • float
  • clear

Pseudo-elements and HTML Classes

HTML classes can be paired with pseudo-elements:

Example

				
					p.intro::first-letter {
  color: #ff0000;
  font-size: 200%;
}
				
			

The initial letter of paragraphs with class=”intro” will be shown in the example above, larger and in red.

Multiple Pseudo-elements

It’s also possible to mix multiple pseudo-elements.

The first letter of a paragraph in the example below will be red and written in a xx-large font size. The initial line will be all capitalized and blue. The default font size and color will be used for the remainder of the paragraph:

Example

				
					p::first-letter {
  color: #ff0000;
  font-size: xx-large;
}

p::first-line {
  color: #0000ff;
  font-variant: small-caps;
}
				
			

CSS - The ::before Pseudo-element

You can add content before an element’s content by using the ::before pseudo-element.

An picture is inserted before each <h1> element’s content in the example below:

Example

				
					h1::before {
  content: url(smiley.gif);
}
				
			

CSS - The ::after Pseudo-element

You can add content after an element’s content by using the ::after pseudo-element.

An picture is inserted after each <h1> element’s content in the example below:

Example

				
					h1::after {
  content: url(smiley.gif);
}
				
			

CSS - The ::marker Pseudo-element

The markers of list items are chosen via the ::marker pseudo-element.

The list item markers are styled in the example that follows:

Example

				
					::marker {
  color: red;
  font-size: 23px;
}
				
			

CSS - The ::selection Pseudo-element

The ::selection The user-selected piece of an element is matched by a pseudo-element.

::selection can have the following CSS properties applied to it: outline, color, background, and cursor.


The selected text is shown red on a yellow backdrop in the example below:

Example

				
					::selection {
  color: red;
  background: yellow;
}
				
			

All CSS Pseudo Elements

Selector Example Example description
::after p::after Insert something after the content of each <p> element
::before p::before Insert something before the content of each <p> element
::first-letter p::first-letter Selects the first letter of each <p> element
::first-line p::first-line Selects the first line of each <p> element
::marker ::marker Selects the markers of list items
::selection p::selection Selects the portion of an element that is selected by a user

CSS Display/Visibility Properties

Selector Example Example description
:active a:active Selects the active link
:checked input:checked Selects every checked <input> element
:disabled input:disabled Selects every disabled <input> element
:empty p:empty Selects every <p> element that has no children
:enabled input:enabled Selects every enabled <input> element
:first-child p:first-child Selects every <p> elements that is the first child of its parent
:first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent
:focus input:focus Selects the <input> element that has focus
:hover a:hover Selects links on mouse over
:in-range input:in-range Selects <input> elements with a value within a specified range
:invalid input:invalid Selects all <input> elements with an invalid value
:lang(language) p:lang(it) Selects every <p> element with a lang attribute value starting with "it"
:last-child p:last-child Selects every <p> elements that is the last child of its parent
:last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent
:link a:link Selects all unvisited links
:not(selector) :not(p) Selects every element that is not a <p> element
:nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent
:nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child
:nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent
:only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent
:only-child p:only-child Selects every <p> element that is the only child of its parent
:optional input:optional Selects <input> elements with no "required" attribute
:out-of-range input:out-of-range Selects <input> elements with a value outside a specified range
:read-only input:read-only Selects <input> elements with a "readonly" attribute specified
:read-write input:read-write Selects <input> elements with no "readonly" attribute
:required input:required Selects <input> elements with a "required" attribute specified
:root root Selects the document's root element
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor name)
:valid input:valid Selects all <input> elements with a valid value
:visited a:visited Selects all visited links
Share this Doc

CSS Pseudo-element

Or copy link

Explore Topic