contenteditable and Drag and Drop

Intermediate · 16 min

How contenteditable makes any element an editor, what plaintext-only changes, and why native drag and drop needs dragover cancelled and a keyboard fallback.

Before thisCustom Elements in HTML

remarkablemark · 16 min

contenteditable and Drag and Drop

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by remarkablemarkReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `contenteditable` makes an element editable and keyboard-focusable with no `tabindex`. Typing, selection and caret all work, but the markup the browser writes when the user presses Enter differs between engines.
  • `contenteditable="plaintext-only"` blocks rich formatting. Enter produced a newline character in Chromium and a `<br>` in Firefox and WebKit builds in a test.
  • Native drag and drop starts with `draggable="true"` and a `dragstart` handler that puts data in `event.dataTransfer`. Links and images are draggable by default.
  • A drop target must cancel `dragover` with `preventDefault()`. Without that, the `drop` event never fired in a test and `dropEffect` ended as `none`.
  • Drag and drop has no keyboard equivalent built in, so a draggable `div` is not accessible by itself. Provide buttons or another way to do the same job.

Two old attributes, one idea

Both of these make a normal element behave like a control. One turns any element into a text field, and the other lets the user pick something up and drop it somewhere. Both have been around since the first HTML5 drafts, and both have sharp edges.

contenteditable

<div contenteditable>Edit me</div>

That is the whole feature. In Chromium 159, Firefox 159 and a WebKit build, the element took focus from Tab with no tabindex, and typing and the caret worked. isContentEditable was true, and contenteditable="false" opted a nested element out.

The edge: what the browser writes. After pressing Enter at the end of “edit me TYPED” and typing “L2”, the markup came out as edit me TYPED<div>L2</div> in Chromium and the WebKit build, and <div>edit me TYPED</div><div>L2</div> in Firefox. If you read innerHTML back, that difference is yours to handle. For plain text, use contenteditable="plaintext-only": Enter produced "plain\nline2" in Chromium and plain<br>line2 in the other two. web-features lists plaintext-only as Baseline from 4 March 2025, when Firefox 136 shipped it.

Remember it is user input: escape or sanitise before storing or rendering it elsewhere, and give an editable region an accessible name (names lesson) and a role such as textbox when it acts as one. For anything beyond one-line notes, use a proper textarea or an editor library.

Native drag and drop

The API has three parts: make something draggable, put data on the drag, and accept it on a target.

<div id="src" draggable="true">drag</div>
<div id="dst">drop here</div>
<script>
  src.addEventListener("dragstart", e => e.dataTransfer.setData("text/plain", "payload-42"));
  dst.addEventListener("dragover", e => e.preventDefault());   // required
  dst.addEventListener("drop", e => { e.preventDefault(); console.log(e.dataTransfer.getData("text/plain")); });
</script>

I drove this with real pointer drags in all three engines:

  • The dragstart fired, the target’s drop received “payload-42” and dragend reported dropEffect=move.
  • On a second target with no preventDefault() in dragover, drop never fired and dropEffect was none. This is the classic “my drop does nothing” bug: the browser only treats a target as accepting drops if you cancel dragover.
  • An element without draggable did nothing when dragged. Links and images are draggable by default: dragging a link put its URL into dataTransfer, and in Firefox an img gave its data URL.

Keyboard and everyone else

A native drag needs a pointer. In the test a draggable div was not focusable, and nothing in HTML starts a drag from the keyboard. Anything done by dragging, like reordering a list, must also be possible another way: buttons for “move up” and “move down”, or a select for the position. The focus lesson explains why custom controls must also work without a mouse. Touch is another gap: native drag and drop is awkward on phones, so libraries that rebuild it with pointer events exist.

What the video covers

This is remarkablemark’s “Drag and drop with HTML draggable” (January 2021, about 16 minutes). The description says it covers how native web drag and drop works with the HTML5 draggable attribute, and links a Repl.it demo and a blog post. It appears to be a livestream recording and is a small channel, so expect an unpolished pace. I could not read a transcript. It does not cover contenteditable; dcode’s short video on that is in further viewing, and the test results above are mine.

Where this fits

This ends the interactive-elements group, after custom elements. The final group is production concerns, starting with HTML security.

Check yourself

  1. 01Your `drop` handler never runs. What is the most likely cause?

    The target did not call `preventDefault()` in its `dragover` handler. In a test the drag with no cancel fired `dragover` repeatedly, `dragend` reported `dropEffect=none`, and `drop` never ran. Cancel `dragover` and the drop fires.

  2. 02Is a `<div draggable="true">` keyboard accessible?

    No. In a test it was not in the tab order, and there is no keyboard way to start a native drag. Offer an alternative such as move up and move down buttons, which also helps touch and voice users.

  3. 03What does `contenteditable="plaintext-only"` give you over `contenteditable`?

    It stops rich-text editing, so pasted or typed content stays plain text. The browsers disagree on line breaks: Chromium inserted a newline, Firefox and WebKit builds inserted `<br>`. Support reached Firefox in version 136, so it is newly Baseline.

  4. 04Why do teams avoid building a rich editor straight on `contenteditable`?

    The markup it generates varies between engines. In a test, pressing Enter produced `<div>` wrappers in Chromium and Firefox, with different surrounding structure, and Chromium left a bare text node. Editors like ProseMirror normalise that.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark