CSS - Pseudo Elements

-

Syntax and Notation

Pseudo-elements in CSS have a syntax and notation that distinguishes them from regular elements and pseudo-classes. Understanding the syntax and notation is important for correctly applying pseudo-elements to your CSS styles.

Example: General Syntax for Pseudo-elements

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

In this syntax, selector represents the element you want to apply the pseudo-element to, and ::pseudo-element is the name of the pseudo-element. The double colon :: is used to differentiate pseudo-elements from pseudo-classes, which use a single colon :.

However, in older versions of CSS (before CSS3), pseudo-elements were written with a single colon:

Example: Older Versions of CSS Syntax

selector:pseudo-element {
  property: value;
}

While the single colon notation is still supported for backward compatibility, it is recommended to use the double colon notation for pseudo-elements in modern CSS to avoid confusion with pseudo-classes.

Pseudo-element Browser Support
::before, ::after, ::first-letter, ::first-line Most modern browsers
::placeholder, ::marker Limited support in older browser versions

You may need to use vendor prefixes or provide fallback styles for browsers that don't support newer pseudo-elements.

Pseudo-elements are not part of the DOM tree and cannot be selected or changed using JavaScript. They are presentational and handled by the browser's rendering engine.

Common Pseudo-Elements

::before and ::after

The ::before and ::after pseudo-elements let you add content before or after an element without changing the HTML. They are often used to add icons, decorations, or extra text to improve the look of an element.

Example: Adding an Icon Before a Link

a::before {
  content: "\f0c1"; /* Unicode character for a link icon */
  font-family: FontAwesome;
  margin-right: 5px;
}

In this example, the ::before pseudo-element adds a link icon before each <a> element. The content property sets the content to add, which can be text, an image, or even an empty string. The font-family property sets the icon font (e.g., FontAwesome), and you can add more styling as needed.

Example: Adding a Decorative Element After a Heading

h2::after {
  content: "";
  display: inline-block;
  width: 20px;
  height: 20px;
  background-color: #ff0000;
  margin-left: 10px;
}

Here, the ::after pseudo-element creates a decoration (a red square) after each <h2> element. The content property is an empty string since we don't want to add any text. The display, width, height, and background-color properties style the decorative element.

::first-letter and ::first-line

The ::first-letter and ::first-line pseudo-elements let you style the first letter or the first line of a text block differently from the rest of the text.

Example: Creating a Drop Cap

p::first-letter {
  font-size: 3em;
  font-weight: bold;
  float: left;
  margin-right: 5px;
}

In this example, the ::first-letter pseudo-element creates a drop cap effect for the first letter of each <p> element. The font-size property increases the size of the first letter, font-weight makes it bold, float puts it on the left, and margin-right adds space between the drop cap and the rest of the text.

Example: Styling the First Line

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

Here, the ::first-line pseudo-element styles the first line of each <p> element. The font-variant property sets the text to small capitals, and the color property changes the text color of the first line.

Note that the ::first-letter and ::first-line pseudo-elements can only be used on block-level elements, and they have limits on the properties that can be used with them.

::selection

The ::selection pseudo-element lets you style the text that the user selects. You can change the background color and text color of the selected text.

Example: Customizing Selected Text

::selection {
  background-color: #ff0000;
  color: #fff;
}

In this example, the ::selection pseudo-element changes the background color of the selected text to red and the text color to white. When the user selects any text on the page, it will show with the set colors.

However, note that the ::selection pseudo-element has limited browser support, and some browsers may not use the set styles. So, it's good to provide backup styles or use other methods for a consistent look across browsers.

Advanced Pseudo-Elements

CSS provides advanced pseudo-elements that let you style specific parts of an element. Let's look at two of these advanced pseudo-elements: ::placeholder and ::marker.

::placeholder

The ::placeholder pseudo-element targets the placeholder text inside form input fields. It lets you style the placeholder text differently from the actual input text.

Example: Styling Placeholder Text

input::placeholder {
  color: #999;
  font-style: italic;
}

You can use various CSS properties to change the look of the placeholder text:

Property Description
color Changes the text color of the placeholder
font-size, font-family, font-weight Modifies the font properties of the placeholder text
background-color Sets the background color of the placeholder
text-transform, text-decoration Applies text transformations or decorations to the placeholder

Note that ::placeholder has some differences in browser support. To handle cross-browser compatibility, you can use vendor prefixes:

Example: Cross-Browser Placeholder Styles

::-webkit-input-placeholder {
  /* Chrome, Safari, Opera */
  color: #999;
}

:-ms-input-placeholder {
  /* Internet Explorer */
  color: #999;
}

::-moz-placeholder {
  /* Firefox 19+ */
  color: #999;
}

:-moz-placeholder {
  /* Firefox 18- */
  color: #999;
}

::marker

The ::marker pseudo-element lets you style the marker box of a list item, such as the bullet in an unordered list or the number in an ordered list.

Example: Styling List Markers

ul li::marker {
  color: #ff0000;
  font-size: 1.2em;
}

ol li::marker {
  font-weight: bold;
  content: counter(list-item) ") ";
}
  • For the unordered list, the color property changes the color of the bullet to red, and the font-size property increases the size of the bullet.
  • For the ordered list, the font-weight property makes the number bold, and the content property modifies the marker content. The counter(list-item) function generates the current list item number, and the ) " part adds a closing parenthesis and a space after the number.

Browser support for the ::marker pseudo-element is relatively new. As an alternative, you can use the list-style-type property to change the marker style or create custom markers using pseudo-elements like ::before.

Example: Alternative List Marker Styles

ul {
  list-style-type: square;
}

ol {
  list-style-type: upper-roman;
}

li::before {
  content: "\2022"; /* Bullet character */
  color: #ff0000;
  margin-right: 5px;
}

These are just a few examples of how advanced pseudo-elements like ::placeholder and ::marker can be used to style specific parts of an element. By using these pseudo-elements and thinking about browser support, you can create more appealing and custom user interfaces.

Pseudo-Elements in Action

Pseudo-elements are a tool in CSS that let you add extra style to your web pages. Let's look at some examples that show how pseudo-elements can be used in creative and practical ways.

One use of pseudo-elements is to add icons or symbols to links or buttons:

Example: Adding icons or symbols to links or buttons

a[href$=".pdf"]::before {
  content: "\f1c1"; /* PDF icon */
  font-family: FontAwesome;
  margin-right: 5px;
  color: #ff0000;
}

button::after {
  content: "\f054"; /* Right arrow icon */
  font-family: FontAwesome;
  margin-left: 10px;
  transition: transform 0.3s;
}

button:hover::after {
  transform: translateX(5px);
}
  • The ::before pseudo-element adds a PDF icon before links that lead to PDF files. The content property sets the icon using a Unicode character, and the font-family property uses the FontAwesome icon font. The color property sets the icon color to red.
  • The ::after pseudo-element adds a right arrow icon after <button> elements. On hover, the arrow moves to the right using the transform property and a transition effect.

Another use of pseudo-elements is to create text effects:

Example: Creating text effects

h1::before {
  content: attr(data-text);
  position: absolute;
  color: rgba(255, 255, 255, 0.1);
  font-size: 2em;
  z-index: -1;
}

p::first-letter {
  font-size: 3em;
  font-weight: bold;
  color: #ff0000;
  float: left;
  margin-right: 10px;
}
  • The ::before pseudo-element creates a text shadow effect for <h1> elements. It uses the attr() function to get the text content from a data-text attribute and positions it behind the actual heading using position: absolute. The text color is set to a semi-transparent white using rgba().
  • The ::first-letter pseudo-element creates a drop cap for the first letter of <p> elements. The letter is enlarged, made bold, colored red, and floated to the left with some margin on the right.

You can also combine pseudo-elements with other CSS selectors and properties to create design patterns:

Example: Creating design patterns

.ribbon {
  position: relative;
  background-color: #ff0000;
  color: #fff;
  padding: 10px;
  margin-bottom: 20px;
}

.ribbon::before {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 0;
  border-top: 10px solid #ff0000;
  border-left: 10px solid transparent;
}

.ribbon::after {
  content: "";
  position: absolute;
  bottom: -10px;
  right: 0;
  border-top: 10px solid #ff0000;
  border-right: 10px solid transparent;
}
  • The .ribbon class styles the main ribbon element with a red background, white text, padding, and margin.
  • The ::before and ::after pseudo-elements create triangular shapes at the bottom left and right corners of the ribbon using the border properties. These shapes give the ribbon a folded or pointed appearance.

These are a few examples of how pseudo-elements can be used to improve the appearance and functionality of web pages. By combining pseudo-elements with other CSS techniques, you can create appealing and interactive designs.