Aral Code

Selectors & the Cascade Lesson 26 of 33

CSS Pseudo-elements — ::before, ::after and More

Add decorative content with ::before and ::after, style the first letter and line, the placeholder and text selection, and learn the sibling combinators + and ~.

A pseudo-element lets you style a specific part of an element, or add extra decorative content to it, without changing the HTML. Pseudo-elements use two colons: p::first-line, h2::before.

::before and ::after

These two create a small, invisible "child" element at the very beginning (::before) or end (::after) of an element's content. They only appear if you set the content property, even if it's an empty string.

Example Adding content with ::before and ::after
<style>
  .new::after {
    content: "NEW";
    background: #CE1126;
    color: white;
    font-size: 11px;
    font-weight: bold;
    padding: 2px 6px;
    border-radius: 4px;
    margin-left: 8px;
    vertical-align: middle;
  }
  .phone::before { content: "📞 "; }
  .req::after { content: " *"; color: #CE1126; }
  p { font-family: Arial, sans-serif; font-size: 18px; }
</style>
<p class="new">Lesson 12: Fonts</p>
<p class="phone">0917 123 4567</p>
<p class="req">Full name</p>

Decorative shapes

With content: "" plus a size and background, pseudo-elements become shapes: underlines, dots, ribbons, overlays. This keeps the HTML clean.

Example A decorative heading underline
<style>
  h2 { font-family: Arial, sans-serif; position: relative; padding-bottom: 12px; }
  h2::after {
    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    width: 60px;
    height: 5px;
    border-radius: 3px;
    background: linear-gradient(90deg, #0038A8, #CE1126);
  }
</style>
<h2>Kasaysayan ng Pilipinas</h2>
<p>A short accent line under the heading, made with ::after.</p>

This is the same technique this website uses for the colored bar next to each lesson heading.

Custom bullets

Example Check-mark bullets
<style>
  ul { list-style: none; padding: 0; font-family: Arial, sans-serif; }
  li { position: relative; padding-left: 30px; margin: 8px 0; }
  li::before {
    content: "✓";
    position: absolute;
    left: 0;
    top: 0;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #1d7a43;
    color: white;
    font-size: 13px;
    display: grid;
    place-items: center;
  }
</style>
<h3>Requirements for enrollment</h3>
<ul>
  <li>PSA birth certificate</li>
  <li>Report card (SF9)</li>
  <li>Two 1x1 ID pictures</li>
</ul>

::first-letter and ::first-line

Style just the first letter (great for a magazine-style "drop cap") or just the first line of a paragraph:

Example Drop cap and first line
<style>
  p { font-family: Georgia, serif; line-height: 1.6; max-width: 460px; }
  p::first-letter {
    float: left;
    font-size: 3.4em;
    line-height: 0.9;
    padding: 4px 8px 0 0;
    color: #0f5c4d;
    font-weight: bold;
  }
  p::first-line { font-variant: small-caps; letter-spacing: 1px; }
</style>
<p>Noong unang panahon, sa isang maliit na baryo sa tabi ng dagat, may isang batang mahilig magbasa. Tuwing hapon, umuupo siya sa ilalim ng puno ng mangga at nagbabasa hanggang lumubog ang araw.</p>

::placeholder

Styles the gray hint text inside empty inputs:

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

::selection

Styles text when the user highlights it:

Example Custom selection color
<style>
  ::selection { background: #f2b33d; color: #1d2321; }
  p { font: 18px Arial, sans-serif; }
</style>
<p>Highlight this sentence with your mouse or finger to see the custom color.</p>

::marker

You met this in the Lists lesson: it styles the bullet or number of list items.

Bonus: sibling combinators

While we're talking about selectors, two more combinators are worth knowing:

  • A + B — the adjacent sibling: B that comes immediately after A
  • A ~ B — the general sibling: every B that comes after A (same parent)
Example + and ~ in action
<style>
  p { font-family: Arial, sans-serif; margin: 4px 0; padding: 4px 8px; }
  h3 + p { font-weight: bold; color: #0f5c4d; }     /* only the first p after h3 */
  .start ~ p { background: #fdf3dc; }              /* every p after .start */
</style>
<h3>Announcement</h3>
<p>This paragraph is right after the h3, so it's bold green.</p>
<p>This one is not.</p>
<p class="start">Everything after this line gets a yellow background…</p>
<p>…like this one…</p>
<p>…and this one.</p>

One colon or two?

A quick way to remember: one colon = a state or position of a whole element (pseudo-class: :hover, :first-child). Two colons = a part of an element or something added to it (pseudo-element: ::before, ::first-line). Old CSS used one colon for :before and :after, and browsers still accept it, but two colons is the modern standard.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What property is required for ::before to appear?
2. Which selector styles only the first letter of a paragraph?
3. What does h2 + p select?
4. Why shouldn't important information be added with content?

By Sir Reyel · Last updated 2026-12-04