What is a pseudo-element?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#CSS #HTML

Brief Answer

A pseudo-element is a virtual element created by CSS that allows styling specific parts of an element:

  1. ::before — inserts content before element 📍
  2. ::after — inserts content after element 📌
  3. ::first-line — styles the first line 📝
  4. ::first-letter — styles the first letter 🔤
  5. ::selection — styles selected text ✨
  6. ::placeholder — styles placeholder in input fields 💬
  7. ::marker — styles list markers 🔸
/* Main pseudo-elements */
.element::before { content: "★"; }
.element::after { content: ""; }
p::first-line { font-weight: bold; }
p::first-letter { font-size: 2em; }

Full Answer

Pseudo-elements are like invisible helpers that create additional elements where they don’t exist in HTML! They allow adding decorative elements without changing the markup. 🎭

Pseudo-element syntax

/* Modern syntax (double colon) */
element::pseudo-element {
  property: value;
}
 
/* Legacy syntax (single colon) */
element:pseudo-element {
  property: value;
}

1. ::before and ::after — generated content

Most popular pseudo-elements for creating decorative elements:

.quote::before {
  content: """;
  font-size: 2em;
  color: #666;
}
 
.quote::after {
  content: """;
  font-size: 2em;
  color: #666;
}
 
/* Icons */
.icon::before {
  content: "★";
  color: gold;
  margin-right: 5px;
}
 
/* Decorative elements */
.button::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  background: blue;
  bottom: 0;
  left: 0;
}
<p class="quote">This is a quote with quotation marks</p>
<button class="icon">Favorite</button>
<button class="button">Button with underline</button>

2. ::first-line — first line

Styles the first line of text:

p::first-line {
  font-weight: bold;
  color: #333;
  text-transform: uppercase;
}
 
.article::first-line {
  font-size: 1.2em;
  letter-spacing: 1px;
}

3. ::first-letter — first letter

Creates drop cap effect:

p::first-letter {
  font-size: 3em;
  float: left;
  line-height: 1;
  margin: 0 8px 0 0;
  color: #c00;
}
 
.drop-cap::first-letter {
  font-family: serif;
  font-weight: bold;
  background: #f0f0f0;
  padding: 5px;
}

4. ::selection — selected text

Styles text selected by user:

::selection {
  background: #007bff;
  color: white;
}
 
p::selection {
  background: yellow;
  color: black;
}
 
.special::selection {
  background: linear-gradient(45deg, red, blue);
  color: white;
}

5. ::placeholder — placeholder text

Styles placeholder in input fields:

input::placeholder {
  color: #999;
  font-style: italic;
  opacity: 0.7;
}
 
textarea::placeholder {
  color: #666;
  font-size: 14px;
  line-height: 1.4;
}
<input type="text" placeholder="Enter your name">
<textarea placeholder="Write a message..."></textarea>

6. ::marker — list markers

Styles list markers:

li::marker {
  color: red;
  font-size: 1.2em;
}
 
.custom-list li::marker {
  content: "→ ";
  color: blue;
}
 
ol li::marker {
  font-weight: bold;
  color: green;
}

Practical examples

Button with effect

.btn {
  position: relative;
  padding: 10px 20px;
  background: #007bff;
  color: white;
  border: none;
  overflow: hidden;
}
 
.btn::before {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.2);
  transition: left 0.3s ease;
}
 
.btn:hover::before {
  left: 100%;
}

Tooltip

.tooltip {
  position: relative;
}
 
.tooltip::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: black;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}
 
.tooltip:hover::after {
  opacity: 1;
}
<span class="tooltip" data-tooltip="Tooltip text">Hover me</span>

Counters

.counter {
  counter-reset: item;
}
 
.counter li {
  counter-increment: item;
}
 
.counter li::before {
  content: counter(item) ". ";
  font-weight: bold;
  color: #007bff;
}

Pseudo-element limitations

  1. content is required for ::before and ::after
  2. Don’t work with replaced elements (img, input)
  3. Inherit styles from parent element
  4. Not accessible to JavaScript directly
/* Must specify content */
.element::before {
  content: ""; /* Even if empty */
  display: block;
  width: 20px;
  height: 20px;
  background: red;
}
 
/* Doesn't work with img */
img::before {
  content: "Text"; /* Won't display */
}

Best practices

  1. Use double colon (::) for pseudo-elements 📝
  2. Always specify content for ::before/::after ✅
  3. Don’t overuse — use for decoration, not content 🎨
  4. Remember accessibility — important content should be in HTML 👁️

Common mistakes

Wrong:

.element::before {
  /* Forgot content */
  background: red;
}

Correct:

.element::before {
  content: "";
  background: red;
}

Conclusion

Pseudo-elements are powerful tools for creating decorative effects:

  • ::before/::after — for generated content
  • ::first-line/::first-letter — for typography
  • ::selection — for custom selection styling
  • ::placeholder — for input field styling

Use them to enhance visual design without changing HTML! 🎯