Heading click-to-edit in HTML, CSS and vanilla JS
A copy-paste Heading click-to-edit in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-inline-edit-heading" data-dcl-scheme="light" aria-labelledby="example-inline-edit-heading-title">
<header><h2 id="example-inline-edit-heading-title">Heading click-to-edit</h2>
<p>A document-style heading turns into a single text field only after explicit activation.</p></header>
<div class="parts">
<div data-view>
<h3 class="editable-heading"><button type="button" class="heading-button" aria-describedby="example-inline-edit-heading-hint" data-open>Weekly product update</button></h3>
<p class="hint" id="example-inline-edit-heading-hint">Activate the heading to rename this document.</p>
</div>
<form class="edit-form" data-form hidden novalidate>
<div class="field">
<label for="example-inline-edit-heading-input">Document heading</label>
<input type="text" id="example-inline-edit-heading-input" value="Weekly product update" required data-input>
</div>
<div class="actions"><button type="submit">Save</button><button type="button" data-cancel>Cancel</button></div>
</form>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-inline-edit-heading");
const instance = Array.from(document.getElementsByClassName("example-inline-edit-heading")).indexOf(root);
if (instance > 0) {
const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
for (const element of [root, ...root.querySelectorAll("*")]) {
for (const attr of singleReferences) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
}
for (const attr of tokenReferences) {
if (element.hasAttribute(attr)) {
const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
element.setAttribute(attr, rewritten.join(" "));
}
}
const href = element.getAttribute("href");
if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
}
}
const preference = matchMedia("(prefers-color-scheme: dark)");
const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
preference.addEventListener("change", apply);
apply();
const view = root.querySelector("[data-view]");
const form = root.querySelector("[data-form]");
const open = root.querySelector("[data-open]");
const input = root.querySelector("[data-input]");
const cancel = root.querySelector("[data-cancel]");
const status = root.querySelector("[data-status]");
function openEdit() {
input.value = open.textContent.trim();
view.hidden = true;
form.hidden = false;
input.focus();
input.select();
}
function closeEdit(message) {
form.hidden = true;
view.hidden = false;
status.textContent = message;
open.focus();
}
open.addEventListener("click", openEdit);
cancel.addEventListener("click", () => closeEdit("Rename cancelled."));
input.addEventListener("keydown", (event) => {
if (event.key === "Escape") closeEdit("Rename cancelled.");
});
function submitForm() {
if (!input.checkValidity()) {
input.reportValidity();
input.focus();
return;
}
open.textContent = input.value.trim();
closeEdit("Heading updated.");
}
form.addEventListener("submit", (event) => {
event.preventDefault();
submitForm();
});
const submitButton = form.querySelector('button[type="submit"]');
if (submitButton) {
submitButton.addEventListener("click", (event) => {
event.preventDefault();
submitForm();
});
}
form.addEventListener("keydown", (event) => {
if (event.key !== "Enter" || event.defaultPrevented) return;
if (!event.target.matches("input, select")) return;
event.preventDefault();
submitForm();
});
})();
</script></section>
<style>
.example-inline-edit-heading { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-inline-edit-heading a { color: #005c62; }
.example-inline-edit-heading button, .example-inline-edit-heading summary, .example-inline-edit-heading input, .example-inline-edit-heading select, .example-inline-edit-heading textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-inline-edit-heading button:hover, .example-inline-edit-heading summary:hover, .example-inline-edit-heading button[aria-selected="true"], .example-inline-edit-heading button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-inline-edit-heading dialog { color: #11292f; background-color: #ffffff; }
.example-inline-edit-heading [data-note] { color: #11292f; background-color: #e4edef; }
.example-inline-edit-heading [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-inline-edit-heading [data-success] { color: #14532d; background-color: #dcfce7; }
.example-inline-edit-heading [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-inline-edit-heading :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-inline-edit-heading[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-inline-edit-heading[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-inline-edit-heading[data-dcl-scheme="dark"] button, .example-inline-edit-heading[data-dcl-scheme="dark"] summary,
.example-inline-edit-heading[data-dcl-scheme="dark"] input, .example-inline-edit-heading[data-dcl-scheme="dark"] select, .example-inline-edit-heading[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-inline-edit-heading[data-dcl-scheme="dark"] button:hover, .example-inline-edit-heading[data-dcl-scheme="dark"] summary:hover,
.example-inline-edit-heading[data-dcl-scheme="dark"] button[aria-selected="true"], .example-inline-edit-heading[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-inline-edit-heading[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-inline-edit-heading[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-inline-edit-heading[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-inline-edit-heading[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-inline-edit-heading[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-inline-edit-heading[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-inline-edit-heading, .example-inline-edit-heading * { box-sizing: border-box; min-width: 0; }
.example-inline-edit-heading { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-inline-edit-heading h2, .example-inline-edit-heading h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-inline-edit-heading header p, .example-inline-edit-heading p { margin-block: 0.5rem; }
.example-inline-edit-heading ul, .example-inline-edit-heading ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-inline-edit-heading dl { margin-block: 0.5rem; }
.example-inline-edit-heading dt { font-weight: 700; }
.example-inline-edit-heading dd { margin: 0 0 0.6rem; }
.example-inline-edit-heading fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-inline-edit-heading legend { padding-inline: 0.25rem; font-weight: 700; }
.example-inline-edit-heading button, .example-inline-edit-heading input, .example-inline-edit-heading select, .example-inline-edit-heading textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-inline-edit-heading input, .example-inline-edit-heading select, .example-inline-edit-heading textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-inline-edit-heading input[type="checkbox"], .example-inline-edit-heading input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-inline-edit-heading input[type="file"] { padding: 0.45rem; }
.example-inline-edit-heading input[type="range"] { padding-inline: 0; }
.example-inline-edit-heading textarea { min-block-size: 6rem; resize: vertical; }
.example-inline-edit-heading button, .example-inline-edit-heading summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-inline-edit-heading button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-inline-edit-heading table { border-collapse: collapse; inline-size: 100%; }
.example-inline-edit-heading th, .example-inline-edit-heading td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-inline-edit-heading [hidden] { display: none !important; }
.example-inline-edit-heading [data-note], .example-inline-edit-heading [data-error], .example-inline-edit-heading [data-success], .example-inline-edit-heading [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-inline-edit-heading .parts { display: grid; gap: 1rem; }
.example-inline-edit-heading .field, .example-inline-edit-heading .fields { display: grid; gap: 0.4rem; }
.example-inline-edit-heading .fields { gap: 0.75rem; }
.example-inline-edit-heading .actions, .example-inline-edit-heading .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-inline-edit-heading .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-inline-edit-heading .plain-list { margin: 0; padding: 0; list-style: none; }
.example-inline-edit-heading .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-inline-edit-heading .choice input { margin-block-start: 0.25rem; }
.example-inline-edit-heading .choice-stack { display: grid; gap: 0.55rem; }
.example-inline-edit-heading .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-inline-edit-heading .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-inline-edit-heading .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-inline-edit-heading .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-inline-edit-heading .hint { font-size: 0.875rem; }
.example-inline-edit-heading .status { min-block-size: 1.5rem; margin-block: 0; }
.example-inline-edit-heading .two-column, .example-inline-edit-heading .split-layout { display: grid; gap: 1rem; }
.example-inline-edit-heading .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-inline-edit-heading .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-inline-edit-heading .visually-hidden {
position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
.example-inline-edit-heading .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-inline-edit-heading .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-inline-edit-heading { padding: 0.75rem; }
.example-inline-edit-heading .actions, .example-inline-edit-heading .inline-row { align-items: stretch; }
.example-inline-edit-heading .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-inline-edit-heading * { transition: none !important; } }
.example-inline-edit-heading .edit-form[hidden] { display: none !important; }
.example-inline-edit-heading .editable-heading { margin-block: 0; }
.example-inline-edit-heading .heading-button { font-size: 1.25rem; font-weight: 700; text-align: start; inline-size: 100%; }
.example-inline-edit-heading .edit-form { display: grid; gap: 0.75rem; }
</style>
When to use
Use this for titles that are read far more often than they are renamed. Keep the heading-looking control focusable and keep explicit Save and Cancel buttons rather than saving on blur.
Avoid
Do not make static text editable only through a mouse click; there must be a focusable control and a keyboard path.
Accessibility
The heading text is inside a native button with an accessible activation hint, focus moves into the labelled input, and Escape or Cancel leaves the original unchanged. Save confirmation is announced politely. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.
Was this useful? Send feedback.