Component Library

← Back to gallery

Code snippet with result in HTML, CSS and vanilla JS

A copy-paste Code snippet with result in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Data display · Tags: code, data-display, responsive

Live preview

Source (copy-ready)

<div class="dcl-code-snippet-with-result" data-dcl-scheme="light">
<header><p class="dcl-code-snippet-with-result-eyebrow">INPUT AND OUTPUT</p><h3>Group files by suffix</h3></header><div class="dcl-code-snippet-with-result-grid"><section><h4>Python</h4><pre class="dcl-code-snippet-with-result-well"><code>from collections import Counter
suffixes = [&quot;csv&quot;, &quot;txt&quot;, &quot;csv&quot;]
print(Counter(suffixes))</code></pre></section><section><h4>Printed result</h4><pre class="dcl-code-snippet-with-result-well"><code>Counter({&#x27;csv&#x27;: 2, &#x27;txt&#x27;: 1})</code></pre></section></div>
<script>
(() => {
  const root = document.currentScript.closest(".dcl-code-snippet-with-result");
  const preference = matchMedia("(prefers-color-scheme: dark)");
  function setScheme() { root.dataset.dclScheme = preference.matches ? "dark" : "light"; }
  preference.addEventListener("change", setScheme);
  setScheme();
})();
</script>
</div>
<style>
.dcl-code-snippet-with-result, .dcl-code-snippet-with-result * { box-sizing: border-box; }
.dcl-code-snippet-with-result { max-inline-size: 44rem; margin-inline: auto; padding: clamp(.875rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-code-snippet-with-result h3, .dcl-code-snippet-with-result h4, .dcl-code-snippet-with-result p, .dcl-code-snippet-with-result figure, .dcl-code-snippet-with-result dl, .dcl-code-snippet-with-result ul, .dcl-code-snippet-with-result ol { margin: 0; }
.dcl-code-snippet-with-result h3 { font-size: 1.18rem; line-height: 1.25; }
.dcl-code-snippet-with-result header { margin-block-end: .875rem; }
.dcl-code-snippet-with-result .dcl-code-snippet-with-result-eyebrow { font-size: .8125rem; font-weight: 700; letter-spacing: .06em; }
.dcl-code-snippet-with-result .dcl-code-snippet-with-result-well { padding: .7rem .8rem; border: 1px solid; border-radius: .55rem; }
.dcl-code-snippet-with-result .dcl-code-snippet-with-result-muted { font-size: .875rem; }
.dcl-code-snippet-with-result .dcl-code-snippet-with-result-row { display: flex; gap: .55rem .8rem; align-items: center; flex-wrap: wrap; }
.dcl-code-snippet-with-result .dcl-code-snippet-with-result-grid { display: grid; gap: .65rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); }
.dcl-code-snippet-with-result dt { font-size: .875rem; font-weight: 650; }
.dcl-code-snippet-with-result dd { margin: 0; }
.dcl-code-snippet-with-result .dcl-code-snippet-with-result-value { font-size: clamp(1.35rem, 5vw, 1.9rem); font-weight: 750; font-variant-numeric: tabular-nums; line-height: 1.2; }
.dcl-code-snippet-with-result button, .dcl-code-snippet-with-result summary, .dcl-code-snippet-with-result input { font: inherit; }
.dcl-code-snippet-with-result button, .dcl-code-snippet-with-result summary { min-height: 2.75rem; cursor: pointer; }
.dcl-code-snippet-with-result button { padding: .35rem .7rem; border: 1px solid; border-radius: .4rem; }
.dcl-code-snippet-with-result button:active { transform: translateY(1px); }
.dcl-code-snippet-with-result button:disabled { cursor: default; border-style: dashed; }
.dcl-code-snippet-with-result [hidden] { display: none; }
.dcl-code-snippet-with-result pre { font-size:.875rem; white-space:pre-wrap; overflow-wrap:anywhere; margin:0; }
.dcl-code-snippet-with-result pre code { font-family: ui-monospace, monospace; }
.dcl-code-snippet-with-result[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-code-snippet-with-result[data-dcl-scheme="light"] .dcl-code-snippet-with-result-muted, .dcl-code-snippet-with-result[data-dcl-scheme="light"] .dcl-code-snippet-with-result-eyebrow { color: #405a60; }
.dcl-code-snippet-with-result[data-dcl-scheme="light"] .dcl-code-snippet-with-result-well, .dcl-code-snippet-with-result[data-dcl-scheme="light"] button, .dcl-code-snippet-with-result[data-dcl-scheme="light"] kbd { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-code-snippet-with-result[data-dcl-scheme="light"] .dcl-code-snippet-with-result-accent, .dcl-code-snippet-with-result[data-dcl-scheme="light"] button:hover { color: #ffffff; background-color: #00535a; border-color: #00535a; }
.dcl-code-snippet-with-result[data-dcl-scheme="light"] :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-code-snippet-with-result[data-dcl-scheme="light"] meter, .dcl-code-snippet-with-result[data-dcl-scheme="light"] progress, .dcl-code-snippet-with-result[data-dcl-scheme="light"] input { accent-color: #00535a; }

.dcl-code-snippet-with-result[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-code-snippet-with-result[data-dcl-scheme="dark"] .dcl-code-snippet-with-result-muted, .dcl-code-snippet-with-result[data-dcl-scheme="dark"] .dcl-code-snippet-with-result-eyebrow { color: #b0c9ce; }
.dcl-code-snippet-with-result[data-dcl-scheme="dark"] .dcl-code-snippet-with-result-well, .dcl-code-snippet-with-result[data-dcl-scheme="dark"] button, .dcl-code-snippet-with-result[data-dcl-scheme="dark"] kbd { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-code-snippet-with-result[data-dcl-scheme="dark"] .dcl-code-snippet-with-result-accent, .dcl-code-snippet-with-result[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #8fe1da; border-color: #8fe1da; }
.dcl-code-snippet-with-result[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-code-snippet-with-result[data-dcl-scheme="dark"] meter, .dcl-code-snippet-with-result[data-dcl-scheme="dark"] progress, .dcl-code-snippet-with-result[data-dcl-scheme="dark"] input { accent-color: #8fe1da; }
</style>

When to use

Use when seeing the exact output makes the example easier to understand. Run the example and paste its actual result beside it.

Avoid

Do not show a hand-edited result as if the program produced it.

Accessibility

The input precedes the output in reading order even when stacked. Both preserve code whitespace. Main text contrast is 15.2:1 in light and 13.1:1 in dark.

Was this useful? Send feedback.