Cookie policy disclosure in HTML, CSS and vanilla JS
A copy-paste Cookie policy disclosure in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-cookie-policy-disclosure" data-dcl-scheme="light" aria-label="Cookie policy disclosure example">
<div data-stack>
<h2>Choose whether to share visit statistics</h2>
<div data-stack data-choices>
<details data-well><summary>What is recorded?</summary><p>Whether a support article was opened. The example does not collect this data.</p></details>
<div data-row><button type="button" data-pick="none">Reject optional</button><button type="button" data-pick="all">Allow optional</button></div>
</div>
<p role="status"></p><div><button type="button" data-reopen hidden>Reopen choices</button></div>
</div>
<script>
(() => {
const root = document.currentScript.closest(".dcl-cookie-policy-disclosure");
const preference = matchMedia("(prefers-color-scheme: dark)");
const setTheme = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
preference.addEventListener("change", setTheme);
setTheme();
const find = (selector) => root.querySelector(selector);
const all = (selector) => Array.from(root.querySelectorAll(selector));
const result = find('[role="status"]');
const panel = find("[data-choices]");
const reopen = find("[data-reopen]");
const toggles = () => all('input[type="checkbox"]').filter((input) => !input.disabled);
function save(message) {
result.textContent = message + " (demo only).";
panel.hidden = true;
reopen.hidden = false;
reopen.focus();
}
reopen.addEventListener("click", () => {
panel.hidden = false;
reopen.hidden = true;
panel.querySelector("button, summary, input:not([disabled])").focus();
});
all("[data-pick]").forEach((button) => button.addEventListener("click", () => {
const allow = button.dataset.pick === "all";
toggles().forEach((input) => { input.checked = allow; });
save(allow ? "Optional cookies accepted" : "Optional cookies rejected");
}));
const apply = find("[data-apply]");
if (apply) apply.addEventListener("click", () => {
const allowed = toggles().filter((input) => input.checked).map((input) => input.value);
save(allowed.length ? "Allowed: " + allowed.join(", ") : "Only essential cookies allowed");
});
})();
</script>
</section>
<style>
.dcl-cookie-policy-disclosure, .dcl-cookie-policy-disclosure * { box-sizing: border-box; }
.dcl-cookie-policy-disclosure { font: 1rem/1.5 system-ui, sans-serif; max-inline-size: 40rem; padding: clamp(0.75rem, 3vw, 1.25rem); border: 1px solid; border-radius: 0.75rem; overflow-wrap: anywhere; }
.dcl-cookie-policy-disclosure :where(h2, h3, p, ol, ul, dl, dd) { margin: 0; }
.dcl-cookie-policy-disclosure h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-cookie-policy-disclosure h3 { font-size: 1rem; }
.dcl-cookie-policy-disclosure p + p { margin-block-start: 0.5rem; }
.dcl-cookie-policy-disclosure [data-stack], .dcl-cookie-policy-disclosure form { display: grid; gap: 0.75rem; }
.dcl-cookie-policy-disclosure [data-row] { display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; align-items: center; }
.dcl-cookie-policy-disclosure [data-split] { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); gap: 0.75rem 1.25rem; align-items: start; }
.dcl-cookie-policy-disclosure [data-spread] { justify-content: space-between; }
.dcl-cookie-policy-disclosure [hidden] { display: none !important; }
.dcl-cookie-policy-disclosure [data-well] { padding: 0.75rem; border-radius: 0.375rem; }
.dcl-cookie-policy-disclosure [data-chip] { display: inline-block; width: fit-content; padding: 0.125rem 0.55rem; border-radius: 99rem; font-size: 0.875rem; font-weight: 700; }
.dcl-cookie-policy-disclosure :where(button, summary, input, select) { font: inherit; }
.dcl-cookie-policy-disclosure :where(button, input, select) { min-block-size: 44px; min-inline-size: 0; max-inline-size: 100%; }
.dcl-cookie-policy-disclosure :where(button, input, select) { border: 1px solid; border-radius: 0.375rem; padding: 0.45rem 0.7rem; }
.dcl-cookie-policy-disclosure :where(button, summary) { cursor: pointer; font-weight: 650; }
.dcl-cookie-policy-disclosure :where(button, summary):active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-cookie-policy-disclosure summary:hover { text-decoration: underline; }
.dcl-cookie-policy-disclosure :where(button, input, select):disabled, .dcl-cookie-policy-disclosure [aria-disabled=true] { cursor: not-allowed; border-style: dashed; }
.dcl-cookie-policy-disclosure label { display: grid; gap: 0.25rem; }
.dcl-cookie-policy-disclosure :where(input, select) { inline-size: 100%; }
.dcl-cookie-policy-disclosure label[data-choice] { display: flex; gap: 0.5rem; align-items: center; min-block-size: 44px; }
.dcl-cookie-policy-disclosure label[data-choice] input { inline-size: 1.25rem; block-size: 1.25rem; min-block-size: 0; flex-shrink: 0; }
.dcl-cookie-policy-disclosure fieldset { min-inline-size: 0; margin: 0; border-radius: 0.375rem; padding: 0.5rem 0.75rem; }
.dcl-cookie-policy-disclosure :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-cookie-policy-disclosure { color-scheme: light; border-color: #697f84; }
.dcl-cookie-policy-disclosure { color: #11292f; background-color: #ffffff; }
.dcl-cookie-policy-disclosure [data-well], .dcl-cookie-policy-disclosure [data-chip] { color: #11292f; background-color: #e4edef; }
.dcl-cookie-policy-disclosure button, .dcl-cookie-policy-disclosure input, .dcl-cookie-policy-disclosure select { color: #11292f; background-color: #e4edef; }
.dcl-cookie-policy-disclosure button[data-primary] { color: #ffffff; background-color: #00535a; }
.dcl-cookie-policy-disclosure button:hover { color: #ffffff; background-color: #003c42; }
.dcl-cookie-policy-disclosure button[disabled], .dcl-cookie-policy-disclosure button[aria-disabled=true] { color: #405a60; background-color: #e4edef; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] { color-scheme: dark; border-color: #839fa5; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] [data-well], .dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] [data-chip] { color: #eff8f9; background-color: #1c3c44; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] button, .dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] input, .dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] select { color: #eff8f9; background-color: #1c3c44; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] button[data-primary] { color: #11292f; background-color: #8fe1da; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #b2f2ec; }
.dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] button[disabled], .dcl-cookie-policy-disclosure[data-dcl-scheme="dark"] button[aria-disabled=true] { color: #b0c9ce; background-color: #1c3c44; }
</style>
When to use
Put concise accept and reject actions beside the privacy explanation, with optional detail expanded on request.
Avoid
Do not require opening the disclosure to see that optional cookies can be rejected.
Accessibility
A native disclosure toggles the policy detail; the equal choice buttons remain available with it open or closed. Text contrast is at least 6.1:1 in light and 6.7:1 in dark.
Was this useful? Send feedback.