WikiGalaxy

Personalize

CSS Pseudo-elements

Introduction to Pseudo-elements:

Pseudo-elements in CSS are used to style specified parts of an element. They allow you to do things like add content before or after an element's content, style the first letter or line, and much more.

Common Pseudo-elements:

Some of the most common pseudo-elements include ::before, ::after, ::first-letter, and ::first-line. These elements are often used for adding decorative content or styling specific portions of text.

Usage Example:

Below is a simple example demonstrating how to use ::before and ::after pseudo-elements to add content around an element.


      p::before {
        content: "Start: ";
        color: cyan;
      }
      p::after {
        content: " :End";
        color: cyan;
      }
    

Explanation of Example:

In this example, the ::before pseudo-element adds the word "Start: " before the content of every paragraph, and the ::after pseudo-element adds " :End" after it. This is a simple way to dynamically include additional text without modifying the HTML structure.

Example Output:

Start: This is a paragraph. :End

::first-letter and ::first-line

Styling the First Letter:

The ::first-letter pseudo-element is used to apply styles to the first letter of a block-level element, such as making it larger or changing its color to draw attention.

Styling the First Line:

Similarly, the ::first-line pseudo-element allows you to style the first line of a block element, which can be useful for creating drop caps or other typographic effects.

Example Code:

Here is an example of how these pseudo-elements can be used to enhance text appearance.


      p::first-letter {
        font-size: 2em;
        color: teal;
      }
      p::first-line {
        font-weight: bold;
        color: orange;
      }
    

Example Explanation:

In this example, the first letter of the paragraph is styled to be larger and teal in color, while the first line is bolded and colored orange, making the text more visually appealing.

Example Output:

This is the first line of the paragraph.

::selection Pseudo-element

Enhancing Text Selection:

The ::selection pseudo-element is used to customize the appearance of text when it is highlighted or selected by the user. This can improve user experience by providing a cohesive theme.

Practical Use Cases:

Commonly, ::selection is used to change the background color and text color of selected text, matching the overall design of the website.

Example Implementation:

Below is a code snippet demonstrating how to use ::selection to alter the selection colors.


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

Explanation of Example:

In this example, when text is selected, the background turns red and the text becomes white, providing a clear contrast that enhances readability during selection.

Example Output:

Select this text to see the effect.

::marker Pseudo-element

Customizing List Markers:

The ::marker pseudo-element allows styling of the bullet or number markers in lists. This provides greater flexibility in designing list items to match your webpage's style.

Styling Options:

With ::marker, you can change the color, font, and even the content of list markers. This is particularly useful for creating unique and branded list styles.

Example Code:

Here is an example demonstrating how to use ::marker to style list markers.


      ul li::marker {
        color: pink;
        font-size: 1.5em;
      }
    

Explanation of Example:

In this example, the list markers are styled to be pink and larger than the default size, drawing attention to each list item and enhancing the visual hierarchy.

Example Output:

  • List item one
  • List item two

::placeholder Pseudo-element

Styling Placeholder Text:

The ::placeholder pseudo-element is used to style the placeholder text in input fields. This can help guide users by making placeholders more noticeable or aesthetically pleasing.

Customization Options:

You can change the color, font, and size of the placeholder text, allowing it to blend seamlessly with your site's design.

Example Code:

Below is an example of how to use ::placeholder to style input placeholders.


      input::placeholder {
        color: orange;
        font-style: italic;
      }
    

Explanation of Example:

In this example, the placeholder text within input fields is styled to be orange and italicized, making it distinct yet harmonious with the input field design.

Example Output:

::backdrop Pseudo-element

Understanding the ::backdrop:

The ::backdrop pseudo-element is used to style the backdrop of elements such as modals or fullscreen elements. It is particularly useful for creating overlays and enhancing focus on dialog content.

Styling Capabilities:

You can modify the color, opacity, and even add animations to the backdrop, providing a more immersive user experience.

Example Code:

Here is an example demonstrating how to use ::backdrop to style a modal backdrop.


      dialog::backdrop {
        background-color: rgba(0, 0, 255, 0.5);
      }
    

Explanation of Example:

In this example, the backdrop of a dialog element is styled with a semi-transparent blue color, creating a soft overlay that draws attention to the dialog content.

Example Output:

Modal Content

::part and ::slotted Pseudo-elements

Custom Elements Styling:

The ::part and ::slotted pseudo-elements are used in conjunction with web components to style parts of shadow DOM and slotted content, respectively.

Use Cases and Benefits:

These pseudo-elements allow developers to apply styles to parts of custom elements, enabling more granular control over component styling without breaking encapsulation.

Example Code:

Below is an example of how these pseudo-elements can be used to style web components.


      custom-element::part(button) {
        background-color: yellow;
      }
      ::slotted(p) {
        color: teal;
      }
    

Explanation of Example:

In this example, the ::part pseudo-element is used to style a button within a custom element, while ::slotted styles paragraphs that are slotted into the shadow DOM.

Example Output:

Slotted Paragraph

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025