Master useful CSS pseudo-elements
Tags: css, html, webdev, ux
Published: 2021-03-07 04:46:43

In this blog we will cover some of the lesser used but pretty useful **Pseudo Elements**.

## Selection

The `::selection` CSS **pseudo-element** styles the part of a document that is selected by a user (such as clicking and dragging the mouse across text to highlight it).

{% codepen https://codepen.io/ruppysuppy/pen/YzpOqMJ %}

## Marker

The `::marker` CSS **pseudo-element** selects the marker box of a list item and generally contains a bullet or number. It works on any element or **pseudo-element** where the `display` property is set to `list-item` (`display: list-item`), such as `<li>` elements.

{% codepen https://codepen.io/ruppysuppy/pen/OJboNrY %}

## Placeholder

The `::placeholder` CSS **pseudo-element** can be used to target the placeholder text in an `<input>` or `<textarea>` element.

{% codepen https://codepen.io/ruppysuppy/pen/oNYPyXZ %}

## Slotted()

The `::slotted()` CSS **pseudo-element** represents any element that has been placed into a `slot` inside an HTML template.

This only works when used inside CSS placed within a [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM). Note also that this selector won't select a text node placed into a slot, but targets actual elements.

{% codepen https://codepen.io/ruppysuppy/pen/KKNxzBp %}

## First Line & First Letter

Just as the name suggests, the `::first-line` and `::first-letter` CSS **pseudo-elements** applies styles to the first line and the first letter respectively (of a block-level element). In case of  `::first-line`, the length of the first line depends on many factors, including the width of the element, the width of the document, the font size of the text and may even depend on the screen size. `::first-letter` only styles the first letter when not preceded by other content (such as images or inline tables).

{% codepen https://codepen.io/ruppysuppy/pen/RwoYymZ %}

### Note

I have deliberately skipped some **pseudo-elements** like `::target-text` as its difficult to create an example in a codepen sandbox for it, and some like `::grammar-error` and `::spelling-error` as no browser supports it as of yet.

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Reference

### MDN Web Docs

  1. [::first-letter](https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter)
  2. [::first-line](https://developer.mozilla.org/en-US/docs/Web/CSS/::first-line)
  3. [::placeholder](https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder)
  4. [::marker](https://developer.mozilla.org/en-US/docs/Web/CSS/::marker)
  5. [::selection](https://developer.mozilla.org/en-US/docs/Web/CSS/::selection)
  6. [::slotted()](https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted)
  7. [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM)

### CSS Tricks

  1. [Article on Marker](https://css-tricks.com/almanac/selectors/m/marker/)

# Thanks for reading

Reach out to me on:

-   [Portfolio](https://tapajyoti-bose.web.app)
-   [GitHub](https://github.com/ruppysuppy)
-   [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose-429a601a0)