20 CSS Code Blocks12 / 20

Pure CSSMIT licensed

CSS Code Block with Line Numbers and Filename Header

Line numbers that cannot be copied, generated by CSS counters, beside a filename header with a path breadcrumb and a line-count chip. The gutter is a real column with a divider — so long lines scroll under the numbers rather than pushing them away — and the numbers highlight with their row on hover.

Published Updated

Live Demo
Try it

The code

<div class="cbk-12">
  <button class="cbk-12__theme" type="button" aria-pressed="false" aria-label="Switch to dark theme">
    <svg class="cbk-12__moon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M20 14.2A8.2 8.2 0 0 1 9.8 4 8.4 8.4 0 1 0 20 14.2"/></svg>
    <svg class="cbk-12__sun" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="4"/><path d="M12 3.4v2M12 18.6v2M3.4 12h2M18.6 12h2M6 6l1.4 1.4M16.6 16.6 18 18M18 6l-1.4 1.4M7.4 16.6 6 18"/></svg>
  </button>
  <div class="cbk-12__stage">
    <input class="cbk-12__tbox" id="cbk-12-tbox" type="checkbox" data-alt="light" aria-label="Toggle light and dark theme">
    <label class="cbk-12__tbtn" for="cbk-12-tbox" title="Toggle light and dark theme">
      <svg class="cbk-12__tsun" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" aria-hidden="true"><circle cx="12" cy="12" r="4.2"></circle><path d="M12 2.4v2.3M12 19.3v2.3M2.4 12h2.3M19.3 12h2.3M5.2 5.2l1.6 1.6M17.2 17.2l1.6 1.6M18.8 5.2l-1.6 1.6M6.8 17.2l-1.6 1.6"></path></svg>
      <svg class="cbk-12__tmoon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20.2 14.6A8.6 8.6 0 0 1 9.4 3.8a8.6 8.6 0 1 0 10.8 10.8Z"></path></svg>
    </label>
    <figure class="cbk-12__block">
      <figcaption class="cbk-12__bar">
        <span class="cbk-12__path">
          <svg viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"><path d="M3.5 6.5A2 2 0 0 1 5.5 4.5h3.2l1.8 2h8A2 2 0 0 1 20.5 8.5v9a2 2 0 0 1-2 2h-13a2 2 0 0 1-2-2z" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/></svg>
          styles<span class="cbk-12__sep">/</span>tokens<span class="cbk-12__sep">/</span><b>theme.css</b>
        </span>
        <span class="cbk-12__count">14 lines</span>
      </figcaption>

      <div class="cbk-12__scroll">
<pre class="cbk-12__pre"><code class="language-css cbk-12__code"><span class="cbk-12__l"><span class="tok-comment">/* Semantic tokens — every surface derives from two ramps. */</span></span>
<span class="cbk-12__l"><span class="tok-function">:root</span> <span class="tok-punct">{</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--ink-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">93%</span> <span class="tok-number">.008</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--ink-2</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">66%</span> <span class="tok-number">.018</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--surface-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">16%</span> <span class="tok-number">.013</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--surface-2</span><span class="tok-punct">:</span> <span class="tok-fn2">color-mix</span><span class="tok-punct">(</span><span class="tok-keyword">in</span> oklab<span class="tok-punct">,</span> <span class="tok-fn2">var</span><span class="tok-punct">(</span><span class="tok-variable">--surface-1</span><span class="tok-punct">)</span> <span class="tok-number">88%</span><span class="tok-punct">,</span> <span class="tok-number">#fff</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--radius</span><span class="tok-punct">:</span> <span class="tok-number">14px</span><span class="tok-punct">;</span></span>
<span class="cbk-12__l"><span class="tok-punct">}</span></span>
<span class="cbk-12__l"></span>
<span class="cbk-12__l"><span class="tok-keyword">@media</span> <span class="tok-punct">(</span><span class="tok-variable">prefers-color-scheme</span><span class="tok-punct">:</span> light<span class="tok-punct">) {</span></span>
<span class="cbk-12__l">  <span class="tok-function">:root</span> <span class="tok-punct">{</span></span>
<span class="cbk-12__l">    <span class="tok-variable">--ink-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">18%</span> <span class="tok-number">.01</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">    <span class="tok-variable">--surface-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">99%</span> <span class="tok-number">0</span> <span class="tok-number">0</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-punct">}</span></span>
<span class="cbk-12__l"><span class="tok-punct">}</span></span></code></pre>
      </div>
    </figure>
  </div>
</div>
.cbk-12 {
  inline-size: 100%;
  width: 100%;
  min-block-size: 100vh;
  min-block-size: 100svh;
  min-height: 100vh;
  display: block;
  --mono: ui-monospace,"JetBrains Mono","Geist Mono","SF Mono","IBM Plex Mono",Menlo,monospace;
  --sans: system-ui,"Inter","Geist",-apple-system,"Segoe UI",sans-serif;
  --bg: #07080c;
  --surface: #0e1017;
  --bar: #13161f;
  --gutter: #0b0e14;
  --line: #212533;
  --ink: #e7eaf3;
  --muted: #8e96a7;
  --acc: #22d3ee;
  --tok-key: #d2a8ff;
  --tok-str: #7ee787;
  --tok-fn: #79c0ff;
  --tok-var: #ffcb6b;
  --tok-num: #ff85a1;
  --tok-punct: #8b96a8;
  --tok-fn2: #5eead4;
  --tok-com: #8e96a7;
  background: radial-gradient(60rem 34rem at 50% -10%, #101d24 0%, transparent 62%), var(--bg);
  color: var(--ink);
  font-family: var(--sans);
}

@supports (color: oklch(50% .1 200)) {
  .cbk-12 {
    --bg: oklch(12% .012 260);
    --surface: oklch(16.5% .014 260);
    --bar: oklch(20% .016 260);
    --gutter: oklch(14% .013 260);
    --line: oklch(27% .018 260);
    --ink: oklch(93% .008 260);
    --muted: oklch(69% .02 260);
    --acc: oklch(84% .12 200);
    --tok-com: oklch(69% .02 260);
  }
}

.cbk-12 *,
.cbk-12 *::before,
.cbk-12 *::after {
  box-sizing: border-box;
}

.cbk-12__stage {
  display: grid;
  place-items: center;
  min-block-size: inherit;
  padding: clamp(1rem,4vw,3.5rem);
}

.cbk-12__block {
  margin: 0;
  inline-size: min(58rem,100%);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  overflow: hidden;
  container-type: inline-size;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04), 0 22px 54px -30px rgba(0,0,0,.9);
}

.cbk-12__bar {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .65rem 1rem;
  border-block-end: 1px solid var(--line);
  background: var(--bar);
  font-size: .78125rem;
}

.cbk-12__path {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  color: var(--muted);
  font-family: var(--mono);
}

.cbk-12__path svg {
  inline-size: 15px;
  block-size: 15px;
}

.cbk-12__path b {
  color: var(--ink);
  font-weight: 600;
}

.cbk-12__sep {
  opacity: .45;
  padding-inline: .15rem;
}

.cbk-12__count {
  margin-inline-start: auto;
  padding: .15rem .45rem;
  border-radius: 6px;
  background: color-mix(in oklab,var(--acc) 13%,transparent);
  color: var(--acc);
  font-family: var(--mono);
  font-size: .6875rem;
}

.cbk-12__scroll {
  overflow: auto;
  scrollbar-gutter: stable;
}

.cbk-12__pre {
  margin: 0;
  padding: .9rem 1.1rem 1.1rem 0;
  font-family: var(--mono);
  font-size: clamp(.78125rem,.45rem + .48cqi,.90625rem);
  line-height: 1.8;
  white-space: pre;
  tab-size: 2;
  font-variant-ligatures: none;
}

.cbk-12__code {
  counter-reset: cbk12;
  display: block;
  font: inherit;
}

.cbk-12__l {
  display: block;
  counter-increment: cbk12;
  min-inline-size: 100%;
}

.cbk-12__l::before {
  content: counter(cbk12);
  position: sticky;
  inset-inline-start: 0;
  display: inline-block;
  inline-size: 4.2ch;
  padding-inline-end: 1.1rem;
  margin-inline-end: 1.1rem;
  text-align: end;
  color: var(--muted);
  opacity: .5;
  background: var(--gutter);
  box-shadow: 1px 0 0 var(--line);
  user-select: none;
  font-variant-numeric: tabular-nums;
}

.cbk-12__l:hover {
  background: color-mix(in oklab,var(--ink) 4%,transparent);
}

.cbk-12__l:hover::before {
  opacity: .95;
  color: var(--acc);
}

.cbk-12 .tok-keyword {
  color: var(--tok-key);
}

.cbk-12 .tok-string {
  color: var(--tok-str);
}

.cbk-12 .tok-function {
  color: var(--tok-fn);
}

.cbk-12 .tok-variable {
  color: var(--tok-var);
}

.cbk-12 .tok-number {
  color: var(--tok-num);
}

.cbk-12 .tok-punct {
  color: var(--tok-punct);
}

.cbk-12 .tok-fn2 {
  color: var(--tok-fn2);
}

.cbk-12 .tok-comment {
  color: var(--tok-com);
  font-style: italic;
}

@container (max-width: 24rem) {
  .cbk-12__l::before {
    inline-size: 2.6ch;
    padding-inline-end: .6rem;
    margin-inline-end: .6rem;
  }
}

.cbk-12[data-theme="light"] {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --bar: #f1f4f9;
  --gutter: #f7f9fc;
  --line: #e2e6f0;
  --ink: #11131a;
  --muted: #5b6373;
  --acc: #0e7490;
  --tok-key: #8250df;
  --tok-str: #0a7d3f;
  --tok-fn: #0550ae;
  --tok-var: #8a5a00;
  --tok-num: #b1265c;
  --tok-punct: #57606a;
  --tok-fn2: #0f766e;
  --tok-com: #5b6373;
  background: radial-gradient(60rem 34rem at 50% -10%, #e4f1f6 0%, transparent 62%), var(--bg);
}

@media (prefers-color-scheme: light) {
  .cbk-12:not([data-theme="dark"]) {
    --bg: #f6f8fb;
    --surface: #ffffff;
    --bar: #f1f4f9;
    --gutter: #f7f9fc;
    --line: #e2e6f0;
    --ink: #11131a;
    --muted: #5b6373;
    --acc: #0e7490;
    --tok-key: #8250df;
    --tok-str: #0a7d3f;
    --tok-fn: #0550ae;
    --tok-var: #8a5a00;
    --tok-num: #b1265c;
    --tok-punct: #57606a;
    --tok-fn2: #0f766e;
    --tok-com: #5b6373;
    background: radial-gradient(60rem 34rem at 50% -10%, #e4f1f6 0%, transparent 62%), var(--bg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cbk-12 * {
    transition: none !important;
    animation: none !important;
  }
}

@media (forced-colors: active) {
  .cbk-12__block {
    border: 2px solid CanvasText;
    background: Canvas;
  }

  .cbk-12__l::before {
    background: Canvas;
    color: CanvasText;
    box-shadow: 1px 0 0 CanvasText;
  }

  .cbk-12 [class^="tok-"] {
    color: CanvasText;
  }
}
/* theme toggle — pure CSS, :has() driven; :checked = light theme, top-right of the stage */

.cbk-12__stage {
  position: relative;
}

.cbk-12__tbox {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  margin: 0;
  padding: 0;
  opacity: 0;
  pointer-events: none;
}

.cbk-12__tbtn {
  position: absolute;
  inset-block-start: clamp(.7rem,2vw,1.35rem);
  inset-inline-end: clamp(.7rem,2vw,1.35rem);
  z-index: 7;
  display: grid;
  place-items: center;
  inline-size: 2.375rem;
  block-size: 2.375rem;
  border: 1px solid rgba(127,127,127,.34);
  border-radius: 11px;
  background: rgba(127,127,127,.14);
  color: var(--ink,#e7eaf3);
  cursor: pointer;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  transition: background .18s ease, border-color .18s ease, transform .18s ease;
}

@supports (color: color-mix(in srgb,red,blue)) {
  .cbk-12__tbtn {
    background: color-mix(in srgb, var(--ink,#e7eaf3) 10%, transparent);
    border-color: color-mix(in srgb, var(--ink,#e7eaf3) 26%, transparent);
  }
}

.cbk-12__tbtn:hover {
  transform: translateY(-1px);
}

.cbk-12__tbtn svg {
  inline-size: 1.0625rem;
  block-size: 1.0625rem;
}

.cbk-12__tbox:focus-visible + .cbk-12__tbtn {
  outline: 2px solid var(--ink,#e7eaf3);
  outline-offset: 2px;
}

.cbk-12__tmoon {
  display: none;
}

.cbk-12:has(.cbk-12__tbox:checked) .cbk-12__tsun {
  display: none;
}

.cbk-12:has(.cbk-12__tbox:checked) .cbk-12__tmoon {
  display: block;
}

@media (prefers-reduced-motion: reduce) {
  .cbk-12__tbtn {
    transition: none;
  }

  .cbk-12__tbtn:hover {
    transform: none;
  }
}

@media (forced-colors: active) {
  .cbk-12__tbtn {
    border: 1px solid ButtonBorder;
    color: ButtonText;
    background: ButtonFace;
  }
}

.cbk-12:has(.cbk-12__tbox:checked) {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --bar: #f1f4f9;
  --gutter: #f7f9fc;
  --line: #e2e6f0;
  --ink: #11131a;
  --muted: #5b6373;
  --acc: #0e7490;
  --tok-key: #8250df;
  --tok-str: #0a7d3f;
  --tok-fn: #0550ae;
  --tok-var: #8a5a00;
  --tok-num: #b1265c;
  --tok-punct: #57606a;
  --tok-fn2: #0f766e;
  --tok-com: #5b6373;
  background: radial-gradient(60rem 34rem at 50% -10%, #e4f1f6 0%, transparent 62%), var(--bg);
}

.cbk-12__theme {
  position: absolute;
  inset-block-start: clamp(.75rem,2vw,1.25rem);
  inset-inline-end: clamp(.75rem,2vw,1.25rem);
  z-index: 50;
  inline-size: 2.5rem;
  block-size: 2.5rem;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  border-radius: 999px;
  border: 1px solid rgba(0,0,0,.12);
  color: currentColor;
  background: rgba(255,255,255,.72);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  transition: background .25s ease, transform .25s cubic-bezier(.16,1,.3,1), border-color .25s ease;
}

.cbk-12[data-theme="dark"] .cbk-12__theme {
  background: rgba(20,20,28,.72);
  border-color: rgba(255,255,255,.15);
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  .cbk-12:not([data-theme="light"]) .cbk-12__theme {
    background: rgba(20,20,28,.72);
    border-color: rgba(255,255,255,.15);
    color: #fff;
  }
}

.cbk-12__theme:hover {
  transform: translateY(-1px);
  border-color: rgba(0,0,0,.24);
}

.cbk-12[data-theme="dark"] .cbk-12__theme:hover,
.cbk-12:not([data-theme="light"]) .cbk-12__theme:hover {
  border-color: rgba(255,255,255,.3);
}

.cbk-12__theme:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 3px;
}

.cbk-12__theme svg {
  grid-area: 1/1;
  inline-size: 1.1rem;
  block-size: 1.1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.cbk-12__sun {
  display: none;
}

.cbk-12__theme[aria-pressed="true"] .cbk-12__sun {
  display: block;
}

.cbk-12__theme[aria-pressed="true"] .cbk-12__moon {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .cbk-12__theme {
    transition: none;
  }

  .cbk-12__theme:hover {
    transform: none;
  }
}
(()=>{const r=document.querySelector('.cbk-12');if(!r)return;const t=r.querySelector('.cbk-12__theme');if(!t)return;const dark=()=>(r.dataset.theme||(matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'))==='dark';const sync=()=>{t.setAttribute('aria-pressed',String(dark()));t.setAttribute('aria-label',dark()?'Switch to light theme':'Switch to dark theme');};t.addEventListener('click',()=>{r.dataset.theme=dark()?'light':'dark';sync();});matchMedia('(prefers-color-scheme: dark)').addEventListener('change',()=>{if(!r.dataset.theme)sync();});sync();})();
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 Code Block 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 Code Block with Line Numbers and Filename Header
Source: https://codefronts.com/snippets/css-code-blocks/code-container-with-line-numbers-and-filename-header/

Line numbers that cannot be copied, generated by CSS counters, beside a filename header with a path breadcrumb and a line-count chip. The gutter is a real column with a divider — so long lines scroll under the numbers rather than pushing them away — and the numbers highlight with their row on hover.
## HTML
```html
<div class="cbk-12">
  <button class="cbk-12__theme" type="button" aria-pressed="false" aria-label="Switch to dark theme">
    <svg class="cbk-12__moon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M20 14.2A8.2 8.2 0 0 1 9.8 4 8.4 8.4 0 1 0 20 14.2"/></svg>
    <svg class="cbk-12__sun" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="4"/><path d="M12 3.4v2M12 18.6v2M3.4 12h2M18.6 12h2M6 6l1.4 1.4M16.6 16.6 18 18M18 6l-1.4 1.4M7.4 16.6 6 18"/></svg>
  </button>
  <div class="cbk-12__stage">
    <input class="cbk-12__tbox" id="cbk-12-tbox" type="checkbox" data-alt="light" aria-label="Toggle light and dark theme">
    <label class="cbk-12__tbtn" for="cbk-12-tbox" title="Toggle light and dark theme">
      <svg class="cbk-12__tsun" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" aria-hidden="true"><circle cx="12" cy="12" r="4.2"></circle><path d="M12 2.4v2.3M12 19.3v2.3M2.4 12h2.3M19.3 12h2.3M5.2 5.2l1.6 1.6M17.2 17.2l1.6 1.6M18.8 5.2l-1.6 1.6M6.8 17.2l-1.6 1.6"></path></svg>
      <svg class="cbk-12__tmoon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20.2 14.6A8.6 8.6 0 0 1 9.4 3.8a8.6 8.6 0 1 0 10.8 10.8Z"></path></svg>
    </label>
    <figure class="cbk-12__block">
      <figcaption class="cbk-12__bar">
        <span class="cbk-12__path">
          <svg viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"><path d="M3.5 6.5A2 2 0 0 1 5.5 4.5h3.2l1.8 2h8A2 2 0 0 1 20.5 8.5v9a2 2 0 0 1-2 2h-13a2 2 0 0 1-2-2z" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/></svg>
          styles<span class="cbk-12__sep">/</span>tokens<span class="cbk-12__sep">/</span><b>theme.css</b>
        </span>
        <span class="cbk-12__count">14 lines</span>
      </figcaption>

      <div class="cbk-12__scroll">
<pre class="cbk-12__pre"><code class="language-css cbk-12__code"><span class="cbk-12__l"><span class="tok-comment">/* Semantic tokens — every surface derives from two ramps. */</span></span>
<span class="cbk-12__l"><span class="tok-function">:root</span> <span class="tok-punct">{</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--ink-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">93%</span> <span class="tok-number">.008</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--ink-2</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">66%</span> <span class="tok-number">.018</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--surface-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">16%</span> <span class="tok-number">.013</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--surface-2</span><span class="tok-punct">:</span> <span class="tok-fn2">color-mix</span><span class="tok-punct">(</span><span class="tok-keyword">in</span> oklab<span class="tok-punct">,</span> <span class="tok-fn2">var</span><span class="tok-punct">(</span><span class="tok-variable">--surface-1</span><span class="tok-punct">)</span> <span class="tok-number">88%</span><span class="tok-punct">,</span> <span class="tok-number">#fff</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-variable">--radius</span><span class="tok-punct">:</span> <span class="tok-number">14px</span><span class="tok-punct">;</span></span>
<span class="cbk-12__l"><span class="tok-punct">}</span></span>
<span class="cbk-12__l"></span>
<span class="cbk-12__l"><span class="tok-keyword">@media</span> <span class="tok-punct">(</span><span class="tok-variable">prefers-color-scheme</span><span class="tok-punct">:</span> light<span class="tok-punct">) {</span></span>
<span class="cbk-12__l">  <span class="tok-function">:root</span> <span class="tok-punct">{</span></span>
<span class="cbk-12__l">    <span class="tok-variable">--ink-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">18%</span> <span class="tok-number">.01</span> <span class="tok-number">265</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">    <span class="tok-variable">--surface-1</span><span class="tok-punct">:</span> <span class="tok-fn2">oklch</span><span class="tok-punct">(</span><span class="tok-number">99%</span> <span class="tok-number">0</span> <span class="tok-number">0</span><span class="tok-punct">);</span></span>
<span class="cbk-12__l">  <span class="tok-punct">}</span></span>
<span class="cbk-12__l"><span class="tok-punct">}</span></span></code></pre>
      </div>
    </figure>
  </div>
</div>
```
## CSS
```css
.cbk-12 {
  inline-size: 100%;
  width: 100%;
  min-block-size: 100vh;
  min-block-size: 100svh;
  min-height: 100vh;
  display: block;
  --mono: ui-monospace,"JetBrains Mono","Geist Mono","SF Mono","IBM Plex Mono",Menlo,monospace;
  --sans: system-ui,"Inter","Geist",-apple-system,"Segoe UI",sans-serif;
  --bg: #07080c;
  --surface: #0e1017;
  --bar: #13161f;
  --gutter: #0b0e14;
  --line: #212533;
  --ink: #e7eaf3;
  --muted: #8e96a7;
  --acc: #22d3ee;
  --tok-key: #d2a8ff;
  --tok-str: #7ee787;
  --tok-fn: #79c0ff;
  --tok-var: #ffcb6b;
  --tok-num: #ff85a1;
  --tok-punct: #8b96a8;
  --tok-fn2: #5eead4;
  --tok-com: #8e96a7;
  background: radial-gradient(60rem 34rem at 50% -10%, #101d24 0%, transparent 62%), var(--bg);
  color: var(--ink);
  font-family: var(--sans);
}

@supports (color: oklch(50% .1 200)) {
  .cbk-12 {
    --bg: oklch(12% .012 260);
    --surface: oklch(16.5% .014 260);
    --bar: oklch(20% .016 260);
    --gutter: oklch(14% .013 260);
    --line: oklch(27% .018 260);
    --ink: oklch(93% .008 260);
    --muted: oklch(69% .02 260);
    --acc: oklch(84% .12 200);
    --tok-com: oklch(69% .02 260);
  }
}

.cbk-12 *,
.cbk-12 *::before,
.cbk-12 *::after {
  box-sizing: border-box;
}

.cbk-12__stage {
  display: grid;
  place-items: center;
  min-block-size: inherit;
  padding: clamp(1rem,4vw,3.5rem);
}

.cbk-12__block {
  margin: 0;
  inline-size: min(58rem,100%);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  overflow: hidden;
  container-type: inline-size;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04), 0 22px 54px -30px rgba(0,0,0,.9);
}

.cbk-12__bar {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .65rem 1rem;
  border-block-end: 1px solid var(--line);
  background: var(--bar);
  font-size: .78125rem;
}

.cbk-12__path {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  color: var(--muted);
  font-family: var(--mono);
}

.cbk-12__path svg {
  inline-size: 15px;
  block-size: 15px;
}

.cbk-12__path b {
  color: var(--ink);
  font-weight: 600;
}

.cbk-12__sep {
  opacity: .45;
  padding-inline: .15rem;
}

.cbk-12__count {
  margin-inline-start: auto;
  padding: .15rem .45rem;
  border-radius: 6px;
  background: color-mix(in oklab,var(--acc) 13%,transparent);
  color: var(--acc);
  font-family: var(--mono);
  font-size: .6875rem;
}

.cbk-12__scroll {
  overflow: auto;
  scrollbar-gutter: stable;
}

.cbk-12__pre {
  margin: 0;
  padding: .9rem 1.1rem 1.1rem 0;
  font-family: var(--mono);
  font-size: clamp(.78125rem,.45rem + .48cqi,.90625rem);
  line-height: 1.8;
  white-space: pre;
  tab-size: 2;
  font-variant-ligatures: none;
}

.cbk-12__code {
  counter-reset: cbk12;
  display: block;
  font: inherit;
}

.cbk-12__l {
  display: block;
  counter-increment: cbk12;
  min-inline-size: 100%;
}

.cbk-12__l::before {
  content: counter(cbk12);
  position: sticky;
  inset-inline-start: 0;
  display: inline-block;
  inline-size: 4.2ch;
  padding-inline-end: 1.1rem;
  margin-inline-end: 1.1rem;
  text-align: end;
  color: var(--muted);
  opacity: .5;
  background: var(--gutter);
  box-shadow: 1px 0 0 var(--line);
  user-select: none;
  font-variant-numeric: tabular-nums;
}

.cbk-12__l:hover {
  background: color-mix(in oklab,var(--ink) 4%,transparent);
}

.cbk-12__l:hover::before {
  opacity: .95;
  color: var(--acc);
}

.cbk-12 .tok-keyword {
  color: var(--tok-key);
}

.cbk-12 .tok-string {
  color: var(--tok-str);
}

.cbk-12 .tok-function {
  color: var(--tok-fn);
}

.cbk-12 .tok-variable {
  color: var(--tok-var);
}

.cbk-12 .tok-number {
  color: var(--tok-num);
}

.cbk-12 .tok-punct {
  color: var(--tok-punct);
}

.cbk-12 .tok-fn2 {
  color: var(--tok-fn2);
}

.cbk-12 .tok-comment {
  color: var(--tok-com);
  font-style: italic;
}

@container (max-width: 24rem) {
  .cbk-12__l::before {
    inline-size: 2.6ch;
    padding-inline-end: .6rem;
    margin-inline-end: .6rem;
  }
}

.cbk-12[data-theme="light"] {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --bar: #f1f4f9;
  --gutter: #f7f9fc;
  --line: #e2e6f0;
  --ink: #11131a;
  --muted: #5b6373;
  --acc: #0e7490;
  --tok-key: #8250df;
  --tok-str: #0a7d3f;
  --tok-fn: #0550ae;
  --tok-var: #8a5a00;
  --tok-num: #b1265c;
  --tok-punct: #57606a;
  --tok-fn2: #0f766e;
  --tok-com: #5b6373;
  background: radial-gradient(60rem 34rem at 50% -10%, #e4f1f6 0%, transparent 62%), var(--bg);
}

@media (prefers-color-scheme: light) {
  .cbk-12:not([data-theme="dark"]) {
    --bg: #f6f8fb;
    --surface: #ffffff;
    --bar: #f1f4f9;
    --gutter: #f7f9fc;
    --line: #e2e6f0;
    --ink: #11131a;
    --muted: #5b6373;
    --acc: #0e7490;
    --tok-key: #8250df;
    --tok-str: #0a7d3f;
    --tok-fn: #0550ae;
    --tok-var: #8a5a00;
    --tok-num: #b1265c;
    --tok-punct: #57606a;
    --tok-fn2: #0f766e;
    --tok-com: #5b6373;
    background: radial-gradient(60rem 34rem at 50% -10%, #e4f1f6 0%, transparent 62%), var(--bg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cbk-12 * {
    transition: none !important;
    animation: none !important;
  }
}

@media (forced-colors: active) {
  .cbk-12__block {
    border: 2px solid CanvasText;
    background: Canvas;
  }

  .cbk-12__l::before {
    background: Canvas;
    color: CanvasText;
    box-shadow: 1px 0 0 CanvasText;
  }

  .cbk-12 [class^="tok-"] {
    color: CanvasText;
  }
}
/* theme toggle — pure CSS, :has() driven; :checked = light theme, top-right of the stage */

.cbk-12__stage {
  position: relative;
}

.cbk-12__tbox {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  margin: 0;
  padding: 0;
  opacity: 0;
  pointer-events: none;
}

.cbk-12__tbtn {
  position: absolute;
  inset-block-start: clamp(.7rem,2vw,1.35rem);
  inset-inline-end: clamp(.7rem,2vw,1.35rem);
  z-index: 7;
  display: grid;
  place-items: center;
  inline-size: 2.375rem;
  block-size: 2.375rem;
  border: 1px solid rgba(127,127,127,.34);
  border-radius: 11px;
  background: rgba(127,127,127,.14);
  color: var(--ink,#e7eaf3);
  cursor: pointer;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  transition: background .18s ease, border-color .18s ease, transform .18s ease;
}

@supports (color: color-mix(in srgb,red,blue)) {
  .cbk-12__tbtn {
    background: color-mix(in srgb, var(--ink,#e7eaf3) 10%, transparent);
    border-color: color-mix(in srgb, var(--ink,#e7eaf3) 26%, transparent);
  }
}

.cbk-12__tbtn:hover {
  transform: translateY(-1px);
}

.cbk-12__tbtn svg {
  inline-size: 1.0625rem;
  block-size: 1.0625rem;
}

.cbk-12__tbox:focus-visible + .cbk-12__tbtn {
  outline: 2px solid var(--ink,#e7eaf3);
  outline-offset: 2px;
}

.cbk-12__tmoon {
  display: none;
}

.cbk-12:has(.cbk-12__tbox:checked) .cbk-12__tsun {
  display: none;
}

.cbk-12:has(.cbk-12__tbox:checked) .cbk-12__tmoon {
  display: block;
}

@media (prefers-reduced-motion: reduce) {
  .cbk-12__tbtn {
    transition: none;
  }

  .cbk-12__tbtn:hover {
    transform: none;
  }
}

@media (forced-colors: active) {
  .cbk-12__tbtn {
    border: 1px solid ButtonBorder;
    color: ButtonText;
    background: ButtonFace;
  }
}

.cbk-12:has(.cbk-12__tbox:checked) {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --bar: #f1f4f9;
  --gutter: #f7f9fc;
  --line: #e2e6f0;
  --ink: #11131a;
  --muted: #5b6373;
  --acc: #0e7490;
  --tok-key: #8250df;
  --tok-str: #0a7d3f;
  --tok-fn: #0550ae;
  --tok-var: #8a5a00;
  --tok-num: #b1265c;
  --tok-punct: #57606a;
  --tok-fn2: #0f766e;
  --tok-com: #5b6373;
  background: radial-gradient(60rem 34rem at 50% -10%, #e4f1f6 0%, transparent 62%), var(--bg);
}

.cbk-12__theme {
  position: absolute;
  inset-block-start: clamp(.75rem,2vw,1.25rem);
  inset-inline-end: clamp(.75rem,2vw,1.25rem);
  z-index: 50;
  inline-size: 2.5rem;
  block-size: 2.5rem;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  border-radius: 999px;
  border: 1px solid rgba(0,0,0,.12);
  color: currentColor;
  background: rgba(255,255,255,.72);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  transition: background .25s ease, transform .25s cubic-bezier(.16,1,.3,1), border-color .25s ease;
}

.cbk-12[data-theme="dark"] .cbk-12__theme {
  background: rgba(20,20,28,.72);
  border-color: rgba(255,255,255,.15);
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  .cbk-12:not([data-theme="light"]) .cbk-12__theme {
    background: rgba(20,20,28,.72);
    border-color: rgba(255,255,255,.15);
    color: #fff;
  }
}

.cbk-12__theme:hover {
  transform: translateY(-1px);
  border-color: rgba(0,0,0,.24);
}

.cbk-12[data-theme="dark"] .cbk-12__theme:hover,
.cbk-12:not([data-theme="light"]) .cbk-12__theme:hover {
  border-color: rgba(255,255,255,.3);
}

.cbk-12__theme:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 3px;
}

.cbk-12__theme svg {
  grid-area: 1/1;
  inline-size: 1.1rem;
  block-size: 1.1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.cbk-12__sun {
  display: none;
}

.cbk-12__theme[aria-pressed="true"] .cbk-12__sun {
  display: block;
}

.cbk-12__theme[aria-pressed="true"] .cbk-12__moon {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .cbk-12__theme {
    transition: none;
  }

  .cbk-12__theme:hover {
    transform: none;
  }
}
```

## JavaScript
```js
(()=>{const r=document.querySelector('.cbk-12');if(!r)return;const t=r.querySelector('.cbk-12__theme');if(!t)return;const dark=()=>(r.dataset.theme||(matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'))==='dark';const sync=()=>{t.setAttribute('aria-pressed',String(dark()));t.setAttribute('aria-label',dark()?'Switch to light theme':'Switch to dark theme');};t.addEventListener('click',()=>{r.dataset.theme=dark()?'light':'dark';sync();});matchMedia('(prefers-color-scheme: dark)').addEventListener('change',()=>{if(!r.dataset.theme)sync();});sync();})();
```

How this works

1 — counters in four declarations. Reset on the container, increment on each line, print with generated content:

.cbk-12__code{ counter-reset:cbk12; }
.cbk-12__l{ display:block; counter-increment:cbk12; }
.cbk-12__l::before{
  content:counter(cbk12);
  inline-size:3ch; text-align:end;
  font-variant-numeric:tabular-nums;
  user-select:none;              /* belt and braces — generated content already is */
}

2 — <ol> or counters? Counters, and here is why. Wrapping each line in <li> does give you free numbering, but it also tells assistive tech "list, 14 items" and makes every line an item to navigate — a 200-line file becomes a 200-item list. A code block is a single preformatted text region, and <pre><code> says exactly that. The numbers are presentational, so generated content is the honest place for them. (If you are numbering a genuine ordered procedure — steps a reader follows — that is a list, and <ol> is right.)

3 — the sticky gutter. The numbers live in the inline-start of each line, and position: sticky; inset-inline-start: 0 pins them while the line scrolls beneath. Without it, horizontal scrolling on mobile takes your line numbers off-screen — the most common flaw in hand-rolled gutters:

.cbk-12__l::before{
  position:sticky; inset-inline-start:0;
  background:var(--gutter);          /* opaque, so code slides under it */
  box-shadow:1px 0 0 var(--line);
}

4 — tabular numerals matter. In most monospace fonts digits are already fixed-width, but the moment your gutter falls back to a UI font, proportional digits make the column twitch as the count crosses 10 and 100. One declaration removes the class of bug entirely.

Make it yours

  • Start at a different line: counter-reset: cbk12 41 renders the next line as 42 — handy for excerpts from a real file.
  • Widen the gutter for 3- and 4-digit files: inline-size: 4ch or 5ch. Because it is ch, it stays correct at any font size.
  • Hide numbers below a breakpoint: inside @container (max-width:26rem) set .cbk-12__l::before{display:none}.
  • Add a copy button (demo 11) — the numbers are generated content, so they never enter the clipboard.
  • Make numbers clickable anchors for line-linking by adding a real <a> per line; keep the counter for display and the link for the target.
  • Change the header to show a path breadcrumb, a git status dot, or a line count — it is a flex row of independent spans.

Gotchas — read before shipping

  • Do not put line numbers in the markup as text. They will be copied with the code, and every reader who pastes gets numbered garbage.
  • Avoid <ol><li> for code lines: it announces a list of N items and turns a single code region into hundreds of navigation stops. Use it only when the numbered thing really is an ordered list.
  • Counters must be reset on an ancestor of the elements that increment them. Reset and increment on the same element and every line renders 1.
  • A non-sticky gutter scrolls away horizontally. If your block can overflow (it can), make the number sticky and opaque.
  • user-select: none does not protect against programmatic copy — but generated content is not in the DOM text at all, which does. That is the real reason to use ::before.
  • Empty lines still need to increment. Keep an empty <span class="cbk-12__l"></span> rather than a bare newline, or your numbering drifts out of step with the file.
  • Line-height changes must apply to the whole line box, not just the code; setting it on the ::before alone misaligns numbers by a fraction of a line and looks subtly broken.

Browser support

ChromeSafariFirefoxEdge
111+16.2+113+111+

CSS counters, generated content and tabular-nums work in every browser in use. position: sticky on an inline-block ::before is Chrome 56 / Safari 13 / Firefox 59. oklch()/color-mix() are behind @supports over hex fallbacks; container queries (Chrome 105 / Safari 16 / Firefox 110) only drive the optional narrow-width tweak. No JavaScript.

Techniques used in this demo

Search CodeFronts

Loading…