CodeBlanks Logo Icon CodeBlanks

Challenge - Pseudo-element card

CSS course · Advanced · Lesson 49/52

Build a decorative card with pseudo-elements.

Decorative card with ::before and ::after

The ::before and ::after pseudo-elements add decorative content to an element. Add ::before to .badge with content: "New" and a small badge appears before the label.

These elements are positioned with position: absolute; the host element needs position: relative. A linear-gradient can paint the background; border-radius rounds the corners. Three declarations build a visual accent card.

Exercise Complete the decorative card

Complete the CSS below. Give .badge a position: relative. In .badge::before add content: "New", position: absolute, top: -8px and background: linear-gradient(90deg, #f97316, #ef4444).

Show example solution
/* badge container */
.badge {
  position: relative;
}

/* badge front face */
.badge::before {
  content: "New";
  position: absolute;
  top: -8px;
  background: linear-gradient(90deg, #f97316, #ef4444);
}

Test yourself

  1. Which property is required to give content to ::before?

    .badge::before {
      /* ??? */
      position: absolute;
    }
    • color
    • content
    • display
    • visibility
    Show answer

    content — Correct. ::before and ::after show nothing without the content property.

  2. Complete the content value

    .badge::before {
      content: ____;
      position: absolute;
      top: -8px;
      right: -8px;
      background: linear-gradient(90deg, #f97316, #ef4444);
      color: white;
      padding: 2px 8px;
      border-radius: 999px;
    }
    Show answer
    .badge::before {
      content: "New";
      position: absolute;
      top: -8px;
      right: -8px;
      background: linear-gradient(90deg, #f97316, #ef4444);
      color: white;
      padding: 2px 8px;
      border-radius: 999px;
    }
  3. Match each pseudo-element piece

    ::before
    Adds content before the element
    ::after
    Adds content after the element
    content
    Pseudo-element text
    position: relative
    Anchors the host element

All lessons in Advanced