22 CSS Avatars16 / 22

Pure CSSMIT licensed

CSS Letter Avatar Color Palette Set

A complete A-Z colour system for letter avatars, built from one formula instead of 26 hand-picked hex values: the letter's alphabet index becomes a hue, and oklch() holds lightness and chroma fixed so every one of the 26 swatches passes WCAG AA against its own text. Twelve letters are shown as the working proof, with a live contrast toggle that switches between tinted and saturated fills.

Published

Live Demo
Try it

The code

<div class="av-16">
  <input class="av-16__sr" type="checkbox" id="av-16-dark">
  <label class="av-16__theme" for="av-16-dark">
    <svg class="av-16__i av-16__i--moon" viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"><path d="M20.5 14.3A8.4 8.4 0 0 1 9.7 3.5a8.5 8.5 0 1 0 10.8 10.8Z" stroke="currentColor" stroke-width="1.7" stroke-linejoin="round"/></svg>
    <svg class="av-16__i av-16__i--sun" viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="4.1" stroke="currentColor" stroke-width="1.7"/><path d="M12 3.2v2M12 18.8v2M3.2 12h2M18.8 12h2M5.7 5.7l1.4 1.4M16.9 16.9l1.4 1.4M18.3 5.7l-1.4 1.4M7.1 16.9l-1.4 1.4" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>
    <span class="av-16__vh">Dark theme</span>
  </label>

  <div class="av-16__stage">
    <section class="av-16__wrap" aria-labelledby="av-16-h">
      <header class="av-16__head">
        <div class="av-16__headtext">
          <p class="av-16__eyebrow">System · Letter palette</p>
          <h2 class="av-16__h" id="av-16-h">A to Z, one formula</h2>
          <p class="av-16__sub">Letter index × 13.85° of hue, with lightness pinned so every tile clears AA.</p>
        </div>
        <fieldset class="av-16__seg">
          <legend class="av-16__vh">Palette weight</legend>
          <label class="av-16__segbtn"><input type="radio" name="av-16-p" id="av-16-tint" checked><span>Tinted</span></label>
          <label class="av-16__segbtn"><input type="radio" name="av-16-p" id="av-16-solid"><span>Saturated</span></label>
        </fieldset>
      </header>

      <ul class="av-16__grid">
        <li class="av-16__cell"><span class="av-16__tile" style="--n:0" aria-hidden="true">A</span><span class="av-16__who">Alex Chen</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:1" aria-hidden="true">B</span><span class="av-16__who">Ben Achterberg</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:4" aria-hidden="true">E</span><span class="av-16__who">Elena Vasquez</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:6" aria-hidden="true">G</span><span class="av-16__who">Grace Tan</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:8" aria-hidden="true">I</span><span class="av-16__who">Ines Duarte</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:9" aria-hidden="true">J</span><span class="av-16__who">Jordan Lee</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:12" aria-hidden="true">M</span><span class="av-16__who">Maya Okonkwo</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:14" aria-hidden="true">O</span><span class="av-16__who">Omar Haddad</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:15" aria-hidden="true">P</span><span class="av-16__who">Priya Sharma</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:18" aria-hidden="true">S</span><span class="av-16__who">Sam Rivera</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:19" aria-hidden="true">T</span><span class="av-16__who">Tom Bergström</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:25" aria-hidden="true">Z</span><span class="av-16__who">Zoe Alvarez</span></li>
      </ul>

      <div class="av-16__notes">
        <p class="av-16__note"><span class="av-16__k">Tinted</span> fill L 94% · text L 42% · ≈ 7.4:1 for every hue</p>
        <p class="av-16__note"><span class="av-16__k">Saturated</span> fill L 56% · text L 98% · ≥ 4.6:1 for every hue</p>
      </div>
    </section>
  </div>
</div>
@supports (color: oklch(50% .1 250)) {
  .av-16 {
    --page: oklch(97.6% .004 265);
    --panel: oklch(100% 0 0);
    --panel-2: oklch(96.4% .006 265);
    --ink: oklch(20% .022 265);
    --muted: oklch(53% .022 265);
    --line: oklch(92% .008 265);
    --accent: oklch(58% .196 272);
  }

  .av-16__tile {
    background: oklch(94% .045 var(--hue));
    color: oklch(42% .14 var(--hue));
  }

  .av-16:has(#av-16-solid:checked) .av-16__tile {
    background: oklch(56% .16 var(--hue));
    color: oklch(98% .015 var(--hue));
  }
}

.av-16 {
  --page: #f7f8fa;
  --panel: #ffffff;
  --panel-2: #f1f3f7;
  --ink: #10131a;
  --muted: #5f6672;
  --line: #e4e7ee;
  --accent: #4f46e5;
  --glass: rgba(255,255,255,.68);
  --shadow: 0 1px 2px rgba(16,19,26,.05), 0 16px 36px -20px rgba(16,19,26,.3);
  --r: 18px;
  --size: clamp(3rem,8vw,3.75rem);
  width: 100%;
  inline-size: 100%;
  min-height: 100vh;
  min-height: 100svh;
  display: block;
  position: relative;
  background: var(--page);
  color: var(--ink);
  font-family: system-ui,-apple-system,"Segoe UI",sans-serif;
  -webkit-font-smoothing: antialiased;
}

.av-16 *,
.av-16 *::before,
.av-16 *::after {
  box-sizing: border-box;
}

.av-16__vh {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.av-16__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  margin: 0;
  pointer-events: none;
}

.av-16__theme {
  position: absolute;
  inset-block-start: clamp(.85rem,2.2vw,1.5rem);
  inset-inline-end: clamp(.85rem,2.2vw,1.5rem);
  z-index: 20;
  display: grid;
  place-items: center;
  inline-size: 2.5rem;
  block-size: 2.5rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--glass);
  color: var(--ink);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  cursor: pointer;
  transition: translate .2s ease;
}

.av-16__theme::after {
  content: "";
  position: absolute;
  inset: -.4rem;
  border-radius: inherit;
}

.av-16__theme:hover {
  translate: 0 -1px;
}

.av-16__i {
  inline-size: 1.15rem;
  block-size: 1.15rem;
}

.av-16__i--sun {
  display: none;
}

.av-16__sr:focus-visible + .av-16__theme {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.av-16__stage {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: clamp(1.25rem,5vw,4rem);
}

.av-16__wrap {
  inline-size: min(58rem,100%);
  display: flex;
  flex-direction: column;
  gap: clamp(1.1rem,2.6vw,1.6rem);
}

.av-16__head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  padding-inline-end: 3.5rem;
}

.av-16__headtext {
  display: flex;
  flex-direction: column;
  gap: .35rem;
}

.av-16__eyebrow {
  margin: 0;
  font-size: .7rem;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}

.av-16__h {
  margin: 0;
  font-size: clamp(1.6rem,4.2vw,2.3rem);
  font-weight: 650;
  letter-spacing: -.03em;
  line-height: 1.05;
}

.av-16__sub {
  margin: 0;
  max-inline-size: 34rem;
  font-size: .92rem;
  line-height: 1.55;
  color: var(--muted);
  text-wrap: pretty;
}

.av-16__seg {
  display: inline-flex;
  gap: .15rem;
  margin: 0;
  padding: .25rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
}

.av-16__segbtn {
  position: relative;
}

.av-16__segbtn input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  margin: 0;
}

.av-16__segbtn span {
  display: grid;
  place-items: center;
  min-inline-size: 5rem;
  min-block-size: 2.25rem;
  padding: 0 .8rem;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background .18s ease, color .18s ease;
}

.av-16__segbtn:hover span {
  background: var(--panel-2);
}

.av-16__segbtn input:checked + span {
  background: var(--ink);
  color: var(--panel);
}

.av-16__segbtn input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.av-16__grid {
  list-style: none;
  margin: 0;
  padding: clamp(1.35rem,3.5vw,2rem) clamp(1rem,3vw,1.75rem);
  display: grid;
  gap: clamp(1rem,2.5vw,1.4rem);
  grid-template-columns: repeat(auto-fit,minmax(7.5rem,1fr));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
}

.av-16__cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .55rem;
  text-align: center;
}

.av-16__tile {
  --hue: calc(var(--n) * 13.85);
  display: grid;
  place-items: center;
  inline-size: var(--size);
  aspect-ratio: 1;
  border-radius: 50%;
  background: hsl(calc(var(--n) * 13.85) 62% 90%);
  color: hsl(calc(var(--n) * 13.85) 58% 28%);
  font-size: calc(var(--size) * .42);
  font-weight: 700;
  line-height: 1;
  letter-spacing: .01em;
  transition: background .22s ease, color .22s ease, translate .22s cubic-bezier(.2,.7,.3,1);
}

.av-16__cell:hover .av-16__tile {
  translate: 0 -4px;
}

.av-16:has(#av-16-solid:checked) .av-16__tile {
  background: hsl(calc(var(--n) * 13.85) 58% 42%);
  color: #fff;
}

.av-16__who {
  font-size: .8rem;
  font-weight: 550;
  color: var(--muted);
}

.av-16__notes {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem 1.5rem;
  padding: .85rem 1.1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.av-16__note {
  margin: 0;
  font-size: .8rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.av-16__k {
  display: inline-block;
  min-inline-size: 5rem;
  font-weight: 650;
  color: var(--ink);
}

.av-16:has(#av-16-dark:checked) {
  --page: #0a0b0e;
  --panel: #14161c;
  --panel-2: #1c1f27;
  --ink: #f2f4f9;
  --muted: #98a0b0;
  --line: #262a33;
  --accent: #8b8cf9;
  --glass: rgba(255,255,255,.07);
  --shadow: 0 1px 2px rgba(0,0,0,.6), 0 20px 44px -24px rgba(0,0,0,.85);
}

.av-16:has(#av-16-dark:checked) .av-16__tile {
  background: hsl(calc(var(--n) * 13.85) 38% 22%);
  color: hsl(calc(var(--n) * 13.85) 72% 80%);
}

.av-16:has(#av-16-dark:checked) .av-16__i--moon {
  display: none;
}

.av-16:has(#av-16-dark:checked) .av-16__i--sun {
  display: block;
}

@supports (color: oklch(50% .1 250)) {
  .av-16:has(#av-16-dark:checked) .av-16__tile {
    background: oklch(30% .06 var(--hue));
    color: oklch(88% .13 var(--hue));
  }

  .av-16:has(#av-16-dark:checked):has(#av-16-solid:checked) .av-16__tile {
    background: oklch(58% .16 var(--hue));
    color: oklch(16% .03 var(--hue));
  }
}

@media (prefers-color-scheme: dark) {
  .av-16:not(:has(#av-16-dark:checked)) {
    --page: #0a0b0e;
    --panel: #14161c;
    --panel-2: #1c1f27;
    --ink: #f2f4f9;
    --muted: #98a0b0;
    --line: #262a33;
    --accent: #8b8cf9;
    --glass: rgba(255,255,255,.07);
    --shadow: 0 1px 2px rgba(0,0,0,.6), 0 20px 44px -24px rgba(0,0,0,.85);
  }

  .av-16:not(:has(#av-16-dark:checked)) .av-16__tile {
    background: hsl(calc(var(--n) * 13.85) 38% 22%);
    color: hsl(calc(var(--n) * 13.85) 72% 80%);
  }

  .av-16:not(:has(#av-16-dark:checked)) .av-16__i--moon {
    display: none;
  }

  .av-16:not(:has(#av-16-dark:checked)) .av-16__i--sun {
    display: block;
  }

  @supports (color: oklch(50% .1 250)) {
    .av-16:not(:has(#av-16-dark:checked)) .av-16__tile {
      background: oklch(30% .06 var(--hue));
      color: oklch(88% .13 var(--hue));
    }

    .av-16:not(:has(#av-16-dark:checked)):has(#av-16-solid:checked) .av-16__tile {
      background: oklch(58% .16 var(--hue));
      color: oklch(16% .03 var(--hue));
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .av-16 *,
    .av-16 *::before,
    .av-16 *::after {
    transition: none !important;
    animation: none !important;
  }
}

@media (forced-colors: active) {
  .av-16__tile {
    background: Canvas;
    color: CanvasText;
    box-shadow: inset 0 0 0 1px CanvasText;
  }

  .av-16__segbtn input:checked + span {
    background: Highlight;
    color: HighlightText;
  }

  .av-16__grid,
    .av-16__notes,
    .av-16__seg,
    .av-16__theme {
    border-color: CanvasText;
  }
}
Paste this into ChatGPT, Claude, Cursor, or any coding assistant. The block below is pre-framed with everything the AI needs to integrate this demo into your project — markup, styles, scoping notes, and the source URL. Hit Copy and paste straight into your chat.
Here's a working CSS Avatar from CodeFronts. Use it as-is or adapt to your framework. All classes are scoped under a unique prefix so the code won't collide with your existing styles. MIT licensed.
Demo: CSS Letter Avatar Color Palette Set
Source: https://codefronts.com/components/css-avatars/css-letter-avatar-color-palette-set/

A complete A-Z colour system for letter avatars, built from one formula instead of 26 hand-picked hex values: the letter's alphabet index becomes a hue, and oklch() holds lightness and chroma fixed so every one of the 26 swatches passes WCAG AA against its own text. Twelve letters are shown as the working proof, with a live contrast toggle that switches between tinted and saturated fills.
## HTML
```html
<div class="av-16">
  <input class="av-16__sr" type="checkbox" id="av-16-dark">
  <label class="av-16__theme" for="av-16-dark">
    <svg class="av-16__i av-16__i--moon" viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"><path d="M20.5 14.3A8.4 8.4 0 0 1 9.7 3.5a8.5 8.5 0 1 0 10.8 10.8Z" stroke="currentColor" stroke-width="1.7" stroke-linejoin="round"/></svg>
    <svg class="av-16__i av-16__i--sun" viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="4.1" stroke="currentColor" stroke-width="1.7"/><path d="M12 3.2v2M12 18.8v2M3.2 12h2M18.8 12h2M5.7 5.7l1.4 1.4M16.9 16.9l1.4 1.4M18.3 5.7l-1.4 1.4M7.1 16.9l-1.4 1.4" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>
    <span class="av-16__vh">Dark theme</span>
  </label>

  <div class="av-16__stage">
    <section class="av-16__wrap" aria-labelledby="av-16-h">
      <header class="av-16__head">
        <div class="av-16__headtext">
          <p class="av-16__eyebrow">System · Letter palette</p>
          <h2 class="av-16__h" id="av-16-h">A to Z, one formula</h2>
          <p class="av-16__sub">Letter index × 13.85° of hue, with lightness pinned so every tile clears AA.</p>
        </div>
        <fieldset class="av-16__seg">
          <legend class="av-16__vh">Palette weight</legend>
          <label class="av-16__segbtn"><input type="radio" name="av-16-p" id="av-16-tint" checked><span>Tinted</span></label>
          <label class="av-16__segbtn"><input type="radio" name="av-16-p" id="av-16-solid"><span>Saturated</span></label>
        </fieldset>
      </header>

      <ul class="av-16__grid">
        <li class="av-16__cell"><span class="av-16__tile" style="--n:0" aria-hidden="true">A</span><span class="av-16__who">Alex Chen</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:1" aria-hidden="true">B</span><span class="av-16__who">Ben Achterberg</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:4" aria-hidden="true">E</span><span class="av-16__who">Elena Vasquez</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:6" aria-hidden="true">G</span><span class="av-16__who">Grace Tan</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:8" aria-hidden="true">I</span><span class="av-16__who">Ines Duarte</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:9" aria-hidden="true">J</span><span class="av-16__who">Jordan Lee</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:12" aria-hidden="true">M</span><span class="av-16__who">Maya Okonkwo</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:14" aria-hidden="true">O</span><span class="av-16__who">Omar Haddad</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:15" aria-hidden="true">P</span><span class="av-16__who">Priya Sharma</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:18" aria-hidden="true">S</span><span class="av-16__who">Sam Rivera</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:19" aria-hidden="true">T</span><span class="av-16__who">Tom Bergström</span></li>
        <li class="av-16__cell"><span class="av-16__tile" style="--n:25" aria-hidden="true">Z</span><span class="av-16__who">Zoe Alvarez</span></li>
      </ul>

      <div class="av-16__notes">
        <p class="av-16__note"><span class="av-16__k">Tinted</span> fill L 94% · text L 42% · ≈ 7.4:1 for every hue</p>
        <p class="av-16__note"><span class="av-16__k">Saturated</span> fill L 56% · text L 98% · ≥ 4.6:1 for every hue</p>
      </div>
    </section>
  </div>
</div>
```
## CSS
```css
@supports (color: oklch(50% .1 250)) {
  .av-16 {
    --page: oklch(97.6% .004 265);
    --panel: oklch(100% 0 0);
    --panel-2: oklch(96.4% .006 265);
    --ink: oklch(20% .022 265);
    --muted: oklch(53% .022 265);
    --line: oklch(92% .008 265);
    --accent: oklch(58% .196 272);
  }

  .av-16__tile {
    background: oklch(94% .045 var(--hue));
    color: oklch(42% .14 var(--hue));
  }

  .av-16:has(#av-16-solid:checked) .av-16__tile {
    background: oklch(56% .16 var(--hue));
    color: oklch(98% .015 var(--hue));
  }
}

.av-16 {
  --page: #f7f8fa;
  --panel: #ffffff;
  --panel-2: #f1f3f7;
  --ink: #10131a;
  --muted: #5f6672;
  --line: #e4e7ee;
  --accent: #4f46e5;
  --glass: rgba(255,255,255,.68);
  --shadow: 0 1px 2px rgba(16,19,26,.05), 0 16px 36px -20px rgba(16,19,26,.3);
  --r: 18px;
  --size: clamp(3rem,8vw,3.75rem);
  width: 100%;
  inline-size: 100%;
  min-height: 100vh;
  min-height: 100svh;
  display: block;
  position: relative;
  background: var(--page);
  color: var(--ink);
  font-family: system-ui,-apple-system,"Segoe UI",sans-serif;
  -webkit-font-smoothing: antialiased;
}

.av-16 *,
.av-16 *::before,
.av-16 *::after {
  box-sizing: border-box;
}

.av-16__vh {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.av-16__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  margin: 0;
  pointer-events: none;
}

.av-16__theme {
  position: absolute;
  inset-block-start: clamp(.85rem,2.2vw,1.5rem);
  inset-inline-end: clamp(.85rem,2.2vw,1.5rem);
  z-index: 20;
  display: grid;
  place-items: center;
  inline-size: 2.5rem;
  block-size: 2.5rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--glass);
  color: var(--ink);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  cursor: pointer;
  transition: translate .2s ease;
}

.av-16__theme::after {
  content: "";
  position: absolute;
  inset: -.4rem;
  border-radius: inherit;
}

.av-16__theme:hover {
  translate: 0 -1px;
}

.av-16__i {
  inline-size: 1.15rem;
  block-size: 1.15rem;
}

.av-16__i--sun {
  display: none;
}

.av-16__sr:focus-visible + .av-16__theme {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.av-16__stage {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: clamp(1.25rem,5vw,4rem);
}

.av-16__wrap {
  inline-size: min(58rem,100%);
  display: flex;
  flex-direction: column;
  gap: clamp(1.1rem,2.6vw,1.6rem);
}

.av-16__head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  padding-inline-end: 3.5rem;
}

.av-16__headtext {
  display: flex;
  flex-direction: column;
  gap: .35rem;
}

.av-16__eyebrow {
  margin: 0;
  font-size: .7rem;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}

.av-16__h {
  margin: 0;
  font-size: clamp(1.6rem,4.2vw,2.3rem);
  font-weight: 650;
  letter-spacing: -.03em;
  line-height: 1.05;
}

.av-16__sub {
  margin: 0;
  max-inline-size: 34rem;
  font-size: .92rem;
  line-height: 1.55;
  color: var(--muted);
  text-wrap: pretty;
}

.av-16__seg {
  display: inline-flex;
  gap: .15rem;
  margin: 0;
  padding: .25rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
}

.av-16__segbtn {
  position: relative;
}

.av-16__segbtn input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  margin: 0;
}

.av-16__segbtn span {
  display: grid;
  place-items: center;
  min-inline-size: 5rem;
  min-block-size: 2.25rem;
  padding: 0 .8rem;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background .18s ease, color .18s ease;
}

.av-16__segbtn:hover span {
  background: var(--panel-2);
}

.av-16__segbtn input:checked + span {
  background: var(--ink);
  color: var(--panel);
}

.av-16__segbtn input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.av-16__grid {
  list-style: none;
  margin: 0;
  padding: clamp(1.35rem,3.5vw,2rem) clamp(1rem,3vw,1.75rem);
  display: grid;
  gap: clamp(1rem,2.5vw,1.4rem);
  grid-template-columns: repeat(auto-fit,minmax(7.5rem,1fr));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
}

.av-16__cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .55rem;
  text-align: center;
}

.av-16__tile {
  --hue: calc(var(--n) * 13.85);
  display: grid;
  place-items: center;
  inline-size: var(--size);
  aspect-ratio: 1;
  border-radius: 50%;
  background: hsl(calc(var(--n) * 13.85) 62% 90%);
  color: hsl(calc(var(--n) * 13.85) 58% 28%);
  font-size: calc(var(--size) * .42);
  font-weight: 700;
  line-height: 1;
  letter-spacing: .01em;
  transition: background .22s ease, color .22s ease, translate .22s cubic-bezier(.2,.7,.3,1);
}

.av-16__cell:hover .av-16__tile {
  translate: 0 -4px;
}

.av-16:has(#av-16-solid:checked) .av-16__tile {
  background: hsl(calc(var(--n) * 13.85) 58% 42%);
  color: #fff;
}

.av-16__who {
  font-size: .8rem;
  font-weight: 550;
  color: var(--muted);
}

.av-16__notes {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem 1.5rem;
  padding: .85rem 1.1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.av-16__note {
  margin: 0;
  font-size: .8rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.av-16__k {
  display: inline-block;
  min-inline-size: 5rem;
  font-weight: 650;
  color: var(--ink);
}

.av-16:has(#av-16-dark:checked) {
  --page: #0a0b0e;
  --panel: #14161c;
  --panel-2: #1c1f27;
  --ink: #f2f4f9;
  --muted: #98a0b0;
  --line: #262a33;
  --accent: #8b8cf9;
  --glass: rgba(255,255,255,.07);
  --shadow: 0 1px 2px rgba(0,0,0,.6), 0 20px 44px -24px rgba(0,0,0,.85);
}

.av-16:has(#av-16-dark:checked) .av-16__tile {
  background: hsl(calc(var(--n) * 13.85) 38% 22%);
  color: hsl(calc(var(--n) * 13.85) 72% 80%);
}

.av-16:has(#av-16-dark:checked) .av-16__i--moon {
  display: none;
}

.av-16:has(#av-16-dark:checked) .av-16__i--sun {
  display: block;
}

@supports (color: oklch(50% .1 250)) {
  .av-16:has(#av-16-dark:checked) .av-16__tile {
    background: oklch(30% .06 var(--hue));
    color: oklch(88% .13 var(--hue));
  }

  .av-16:has(#av-16-dark:checked):has(#av-16-solid:checked) .av-16__tile {
    background: oklch(58% .16 var(--hue));
    color: oklch(16% .03 var(--hue));
  }
}

@media (prefers-color-scheme: dark) {
  .av-16:not(:has(#av-16-dark:checked)) {
    --page: #0a0b0e;
    --panel: #14161c;
    --panel-2: #1c1f27;
    --ink: #f2f4f9;
    --muted: #98a0b0;
    --line: #262a33;
    --accent: #8b8cf9;
    --glass: rgba(255,255,255,.07);
    --shadow: 0 1px 2px rgba(0,0,0,.6), 0 20px 44px -24px rgba(0,0,0,.85);
  }

  .av-16:not(:has(#av-16-dark:checked)) .av-16__tile {
    background: hsl(calc(var(--n) * 13.85) 38% 22%);
    color: hsl(calc(var(--n) * 13.85) 72% 80%);
  }

  .av-16:not(:has(#av-16-dark:checked)) .av-16__i--moon {
    display: none;
  }

  .av-16:not(:has(#av-16-dark:checked)) .av-16__i--sun {
    display: block;
  }

  @supports (color: oklch(50% .1 250)) {
    .av-16:not(:has(#av-16-dark:checked)) .av-16__tile {
      background: oklch(30% .06 var(--hue));
      color: oklch(88% .13 var(--hue));
    }

    .av-16:not(:has(#av-16-dark:checked)):has(#av-16-solid:checked) .av-16__tile {
      background: oklch(58% .16 var(--hue));
      color: oklch(16% .03 var(--hue));
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .av-16 *,
    .av-16 *::before,
    .av-16 *::after {
    transition: none !important;
    animation: none !important;
  }
}

@media (forced-colors: active) {
  .av-16__tile {
    background: Canvas;
    color: CanvasText;
    box-shadow: inset 0 0 0 1px CanvasText;
  }

  .av-16__segbtn input:checked + span {
    background: Highlight;
    color: HighlightText;
  }

  .av-16__grid,
    .av-16__notes,
    .av-16__seg,
    .av-16__theme {
    border-color: CanvasText;
  }
}
```

How this works

One formula, 26 swatches. 360° divided by 26 is 13.85°, so the alphabet maps evenly around the hue wheel. The letter index is the only per-avatar input:

<span class="av-16__tile" style="--n:0">A</span>   <!-- hue 0 -->
<span class="av-16__tile" style="--n:12">M</span>  <!-- hue 166 -->

.av-16__tile{ --h: calc(var(--n) * 13.85); }

Why this passes and an HSL version doesn't. HSL lightness is not perceptual: hsl(60 70% 50%) reads far brighter than hsl(240 70% 50%), so a uniform formula produces some unreadable pairs. oklch()'s L is perceptual lightness, so fixing it fixes contrast for every hue:

.av-16__tile{
  background: oklch(94% .045 var(--h));
  color:      oklch(42% .14  var(--h));   /* ≈ 7.4:1 for every letter */
}

The saturated variant keeps the same guarantee by moving both ends: fill drops to 56% L, text rises to 98%, and the ratio stays above 4.5:1 across the wheel. One :has() rule swaps the whole alphabet:

.av-16:has(#av-16-solid:checked) .av-16__tile{
  background: oklch(56% .16 var(--h));
  color:      oklch(98% .015 var(--h));
}

For engines without oklch(), an hsl() layer is declared first with per-band lightness compensation — yellows and greens sit slightly darker — so the fallback is close, if not mathematically perfect.

Make it yours

  • Retune the whole system by editing two numbers: fill lightness and text lightness. Never vary them per letter — that is what breaks the guarantee.
  • Reduce to a 12-hue palette (30° steps) and map letters in pairs if you want more repetition and fewer near-identical hues.
  • Skip specific hue ranges your brand reserves — offset the formula, or add a lookup with nth-child exceptions.
  • Use the same --n for related surfaces (project tags, calendar chips) so a user's colour reads consistently across the product.
  • Derive --n from the first letter of a display name server-side with a single charCodeAt — or from a hash if you want distribution rather than alphabetical clustering.
  • For dark mode, swap the two lightness values (fill ~30%, text ~88%) and keep chroma; the palette stays recognisable.

Gotchas — read before shipping

  • Alphabetical hue mapping clusters real user bases: names beginning with A, J, M and S dominate, so a room of people gets four hues. Hash the whole name if you need distribution.
  • An HSL formula cannot guarantee contrast across hues; if you must use HSL, hand-check all 26 pairs rather than trusting the numbers.
  • Don't put the letter in the accessible name — the full name belongs in text, the tile is decorative.
  • Very high chroma at fixed lightness can fall outside sRGB and get clipped, changing the effective contrast. Keep chroma under about .18 for fills.
  • Letters with descenders or narrow glyphs (I, J) look off-centre in a circle; centre with grid, not line-height, and consider a tiny optical nudge.
  • In forced-colors mode the whole palette collapses to system colours — that is correct, and the letter must still be legible without it.

Browser support

ChromeSafariFirefoxEdge
114+17.5+121+114+

Floor set by :has(), oklch(), backdrop-filter, text-wrap as this demo is written. The core technique itself goes back further — Chrome 111+.

oklch() (Chrome 111 / Safari 15.4 / Firefox 113) is what makes the contrast guarantee possible and is gated behind @supports with a compensated hsl() fallback declared first. :has() (Chrome 105 / Safari 15.4 / Firefox 121) drives the palette switch and the theme toggle. calc() in colour channels works everywhere. No JavaScript.

Techniques used in this demo

Search CodeFronts

Loading…