How to add a placeholder to a contenteditable div
Published 2026-11-09 · Works in all current browsers, with or without a framework.
The CSS-only placeholder
The placeholder attribute only exists on <input> and <textarea>. For a contenteditable element you draw it yourself: keep the text in a data attribute and print it with a ::before pseudo-element while the element has no children.
<div class="editor" contenteditable="true" data-placeholder="Write something..."></div>
<style>
.editor:empty::before {
content: attr(data-placeholder);
color: #9ca3af;
pointer-events: none; /* clicks go to the editor, not the text */
}
</style>Two details matter. :empty only matches when there are no child nodes at all, so the opening and closing tags must touch: a space or a line break between them is a text node and hides the placeholder. And pointer-events: none keeps clicks on the placeholder going to the editor.
Why the placeholder disappears for good
Type a character, delete it, and the placeholder often stays hidden. The element looks empty, but the browser has left markup behind so the caret has a line to sit on.
<!-- What the editor contains after typing "a" and pressing Backspace -->
<!-- Chrome / Safari / Firefox: often a lone line break -->
<div class="editor" contenteditable="true"><br></div>
<!-- After Enter and Backspace: an empty block -->
<div class="editor" contenteditable="true"><div><br></div></div>
<!-- Neither matches :empty, so the placeholder never comes back -->This is the same reason a "required" check on innerHTML === '' lets empty forms through. Emptiness has to be judged by what the user sees, not by the markup.
The fix: decide emptiness in JavaScript
On every input event, check whether the element has any text or media left. Toggle a class for the placeholder, and optionally clear the leftover <br> so the stored HTML is an empty string as well.
const editor = document.querySelector('.editor');
function isVisuallyEmpty(el) {
// No text and no media: only <br>, empty blocks or whitespace remain
return el.textContent.trim() === '' && !el.querySelector('img, video, iframe, hr, table');
}
function updatePlaceholder() {
const empty = isVisuallyEmpty(editor);
editor.classList.toggle('is-empty', empty);
// Optional: clear the leftover <br> so the stored HTML is really empty
if (empty && editor.innerHTML !== '') editor.innerHTML = '';
}
editor.addEventListener('input', updatePlaceholder);
updatePlaceholder(); // initial state, e.g. after loading saved content.editor.is-empty::before {
content: attr(data-placeholder);
color: #9ca3af;
pointer-events: none;
}Run the check once after you load saved content too, for example after setting innerHTML from your API, because loading content does not fire input.
Keep the caret at the start
An inline ::before box takes up room in the line, so in some browsers the blinking caret appears after the placeholder text instead of at the left edge. Take the placeholder out of the text flow with absolute positioning. If your editor has padding, give the ::before the same offsets.
.editor {
position: relative;
}
.editor.is-empty::before {
content: attr(data-placeholder);
color: #9ca3af;
pointer-events: none;
/* Out of the text flow: the caret starts at the left edge,
not after the placeholder text */
position: absolute;
inset-inline-start: 0;
inset-block-start: 0;
}Accessibility
Screen readers announce generated content inconsistently, and a div is not a form field to them. Describe it as one: role="textbox", aria-multiline="true", an accessible name, and aria-placeholder with the same text as the visual placeholder. Make sure the placeholder color still meets your contrast target.
<div
class="editor"
contenteditable="true"
role="textbox"
aria-multiline="true"
aria-label="Message"
aria-placeholder="Write something..."
data-placeholder="Write something..."
></div>React
The same approach works in a component: track the empty state in onInput and switch the class. Keep the element uncontrolled, because re-rendering its children on every keystroke moves the caret.
function Editable({ placeholder }: { placeholder: string }) {
const ref = React.useRef<HTMLDivElement>(null);
const [empty, setEmpty] = React.useState(true);
const onInput = () => {
const el = ref.current!;
setEmpty(el.textContent?.trim() === '' && !el.querySelector('img'));
};
return (
<div
ref={ref}
contentEditable
suppressContentEditableWarning
role="textbox"
aria-multiline="true"
aria-placeholder={placeholder}
data-placeholder={placeholder}
className={empty ? 'editor is-empty' : 'editor'}
onInput={onInput}
/>
);
}Checklist
- The placeholder lives in CSS, never as real text in the element
- No whitespace between the opening and closing tags
- Emptiness is checked on input with textContent, not with :empty alone
- The check also runs after loading saved content
- The placeholder is absolutely positioned so the caret starts at the edge
- role="textbox", an accessible name and aria-placeholder are set
- Form validation uses the same emptiness check as the placeholder
How Scribe Editor handles it
Scribe has a placeholder option, so you do not write the CSS or the input listener yourself, and editor.isEmpty() tells you whether the editor has content when you validate a form. See the API reference and the form builder use case for required-field validation.
import { Scribe } from 'scribejs-editor';
const editor = Scribe.init('#editor', {
placeholder: 'Write something...',
});
// For validation: true when there is no real content
if (editor.isEmpty()) {
showError('Please write a message');
}Placeholders and validation built in
Scribe Editor is an MIT-licensed rich text editor under 50KB with a placeholder option, isEmpty() validation and clean paste for React, Vue, Svelte and Vanilla JS.
contenteditable placeholder: FAQ
Does contenteditable support the placeholder attribute?
No. The placeholder attribute only works on input and textarea. For a contenteditable element, store the text in a data attribute such as data-placeholder and show it with a ::before pseudo-element and content: attr(data-placeholder) while the element is empty.
Why does the placeholder not come back after I delete the text?
Browsers often leave a <br> or an empty <div><br></div> in the element after the last character is deleted. The element is no longer :empty, so the CSS rule stops matching. Check the text content on the input event and toggle a class instead, or remove the leftover markup when the element is visually empty.
Why is the caret shown after the placeholder text?
An inline ::before box takes up space in the text flow, so some browsers place the caret after it. Give the editor position: relative and the ::before rule position: absolute so the placeholder sits under the caret instead of before it.
Do screen readers read a CSS placeholder?
Not reliably. Generated content from ::before is announced inconsistently. Add role="textbox", aria-multiline="true" and aria-placeholder with the same text, and give the field an accessible name with aria-label or aria-labelledby.
Should the placeholder be real text inside the element?
No. Text nodes inserted as a placeholder end up in the saved HTML, have to be removed on focus, and break undo. Keep the placeholder in CSS so the element content stays exactly what the user typed.