Color scheme

Pure CSS Icons

A pure CSS icon is a shape built entirely from CSS properties applied to an element and its pseudo-elements. No SVG, no image file, no extra markup beyond a single element. The technique relies on a small set of properties: border, border-radius, transform, clip-path, and the ::before and ::after pseudo-elements that give you two extra paintable surfaces per element.

CSS icons are not a replacement for SVG sprites. They are a targeted tool for specific situations, and knowing when they are the right call matters as much as knowing how to build them. That comparison is covered at the end of this page.

The Technique Toolkit

Borders as Shapes

CSS borders meet at 45-degree mitered corners. When an element has zero width and height, those corners become the entire visible surface, producing triangles. By making three of the four borders transparent and one colored, you control which triangle appears and which direction it points.


    /* A downward-pointing triangle */
    .arrow-down {
        width: 0;
        height: 0;
        border-left: 8px solid transparent;
        border-right: 8px solid transparent;
        border-top: 12px solid currentColor;
    }

Rotated Borders as Chevrons

A more flexible approach for arrow and chevron shapes uses two visible borders on adjacent sides of a small square, then rotates the whole element to point in the desired direction. This produces a crisp L-shape that reads as a chevron:


    .chevron-right {
        display: inline-block;
        width: 10px;
        height: 10px;
        border-top: 2px solid currentColor;
        border-right: 2px solid currentColor;
        transform: rotate(45deg);
    }

::before and ::after as Extra Layers

Every element has two pseudo-elements, ::before and ::after, that can each be positioned independently. For icons, these function as extra paintable surfaces. A hamburger menu icon uses the element itself as the middle bar, ::before as the top bar, and ::after as the bottom bar, all with position: absolute relative to the element:


    .hamburger,
    .hamburger::before,
    .hamburger::after {
        display: block;
        width: 24px;
        height: 2px;
        background: currentColor;
        border-radius: 2px;
    }

    .hamburger {
        position: relative;
    }

    .hamburger::before,
    .hamburger::after {
        content: '';
        position: absolute;
        left: 0;
    }

    .hamburger::before { top: -8px; }
    .hamburger::after  { top:  8px; }

The Hamburger-to-Close Morph

The hamburger-to-close morph is the canonical CSS icon animation. When the menu opens, the middle bar fades out and the top and bottom bars rotate into an X. Everything is driven by a class toggle and CSS transitions: no JavaScript animation, no keyframes.


    .hamburger,
    .hamburger::before,
    .hamburger::after {
        display: block;
        width: 24px;
        height: 2px;
        background: currentColor;
        border-radius: 2px;
        transition:
            transform 300ms ease-in-out,
            opacity 200ms ease-in-out,
            top 300ms ease-in-out;
    }

    .hamburger {
        position: relative;
    }

    .hamburger::before,
    .hamburger::after {
        content: '';
        position: absolute;
        left: 0;
    }

    .hamburger::before { top: -8px; }
    .hamburger::after  { top:  8px; }

    /* Open state */
    .hamburger.is-open {
        background: transparent; /* hide middle bar */
    }

    .hamburger.is-open::before {
        top: 0;
        transform: rotate(45deg);
    }

    .hamburger.is-open::after {
        top: 0;
        transform: rotate(-45deg);
    }

The sequence: top transitions bring both bars to center, then transform rotates them into the X. The middle bar fades immediately via background: transparent. Setting transition on all three surface states means both the open and close directions animate smoothly.

Hamburger Morph Demo

Click the button to toggle between hamburger and close. The slow-motion toggle stretches the transition duration so the three-part sequence is clearly visible. Use Prefers Reduced Motion to emulate the guard students add in their portfolio demo: transitions drop out and the icon still toggles instantly.

Options:
State: Closed

SVG vs. CSS Icons

Neither approach is universally better. The right choice depends on the icon's complexity, how it will be used, and what the project already has in place.

flowchart LR
    A["Simple geometric shapes\nchevron, X, hamburger"] -->|"prefer"| B["Pure CSS Icons"]
    C["Complex shapes\nlogos, illustrations, custom paths"] -->|"prefer"| D["SVG Sprites"]
    E["Reused across many pages"] -->|"prefer"| D
    F["Tightly coupled to one element\nmorph animation"] -->|"prefer"| B
  • Use CSS Icons when the shape is simple and geometric (chevrons, close X, hamburger, basic arrows), when you want zero extra files or markup, when the icon is tightly coupled to a specific interactive element and unlikely to be reused, or when the morph animation itself is the point.
  • Use SVG Sprites when the icon set is larger than a handful of shapes, when icons need to be reused across multiple elements and pages, when the shapes are too complex for CSS alone, or when you need the accessibility patterns from SVG Sprites and SVG Accessibility.
  • Accessibility Note: CSS icons have no intrinsic accessible name or role. The element carrying the icon must supply its own label via aria-label or visually hidden text on the button or link. The CSS shape itself is invisible to assistive technology, which is both a limitation and an advantage: there is nothing to accidentally expose incorrectly.

Build Your Demo

Create a new directory at unit-6/css-icons/ in your Practice Site with index.html and css-icons.css. Your demo must include:

  • At least three distinct CSS icons built from borders, pseudo-elements, or transforms. At least one must use both ::before and ::after.
  • The hamburger-to-close morph with a working button toggle and proper aria-expanded state management on the button.
  • currentColor used throughout so all icons inherit their color from the parent context.
  • A prefers-reduced-motion guard that removes the morph transitions for users who prefer reduced motion. The icon should still toggle states instantly.

Add a link to your demo on your Practice Site homepage and in your README.md.

Teaching Video Checklist

When you record your teaching video, make sure your teammates come away understanding these concepts:

  • How the zero-width-zero-height border triangle technique works. Draw the invisible borders in a different color first so teammates can see the full square before three sides go transparent.
  • How ::before and ::after function as extra paintable layers, and why position: absolute is required on them when building multi-part icons.
  • The hamburger-to-close morph step by step: show the three transitions (top, transform, background) separately in slow motion so teammates see each one contributing to the final effect.
  • When CSS icons are the right choice and when SVG is better, with at least one concrete example of each from your demo.

Build the hamburger live from a blank element during your video. Starting from nothing and arriving at a working morph in real time is the most effective way to teach this technique.

Resources

  • MDN: ::before — Pseudo-element reference including content, positioning, and inheritance behavior
  • MDN: border — Shorthand reference; the border-triangle technique depends on understanding how corners are rendered
  • MDN: transform — rotate(), translate(), and scale() reference for icon construction and morphing
  • CSS-Tricks: The Shapes of CSS — Comprehensive reference for building geometric shapes from CSS borders and properties