DEV Community

Cover image for Master useful CSS pseudo-elements
Tapajyoti Bose
Tapajyoti Bose

Posted on • Edited on

15 7

Master useful CSS pseudo-elements

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

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.

Placeholder

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

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. Note also that this selector won't select a text node placed into a slot, but targets actual elements.

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

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.

Reference

MDN Web Docs

  1. ::first-letter
  2. ::first-line
  3. ::placeholder
  4. ::marker
  5. ::selection
  6. ::slotted()
  7. Shadow DOM

CSS Tricks

  1. Article on Marker

Thanks for reading

Need a Top Rated Software Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

Follow my blogs for bi-weekly new Tidbits on Medium

FAQ

These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Buzz words
    2. Front End Development Roadmap
    3. Front End Project Ideas
    4. Transition from a Beginner to an Intermediate Frontend Developer
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (2)

Collapse
 
machineno15 profile image
Tanvir Shaikh

can you tell me more about counter, itemCounter (::marker)? & how it works or any reference links

Collapse
 
ruppysuppy profile image
Tapajyoti Bose • Edited

You may check out the mdn docs or w3schools. Basically counter is used to count elements, counter-increment increments the count for the counter and counter-reset resets it (used to start the count from 1 again incase multiple ol tags are present in the page). I used the name itemCounter, but you may name it as you please

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay