contenteditable
Lets the user edit the element’s content directly on the page.
- Applies to
- All HTML elements
Example
#<p contenteditable="true" style="padding: 12px; border: 1px dashed #555; border-radius: 10px;">
Click and change this text. You can also add something of your own.
</p>Definition and usage
#The contenteditable attribute turns an ordinary element into an editing field. The user can click into the text, fix it, add new paragraphs, and even paste formatted content.
Browser-based text editors, notepads and commenting tools are built on this mechanism. Changing the content does not save it anywhere on its own; JavaScript has to take care of that, for example by listening for the input event.
The plaintext-only value allows editing plain text only, without formatting. It is a newer value that recently landed in all major browsers.
Syntax
#<element contenteditable="true | false | plaintext-only">…</element>Values
#true
or empty
The content can be edited, including formatting.false
The content cannot be edited, even if the parent is editable.plaintext-only
Only plain text can be edited; formatting is rejected.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- For ordinary forms, use
<input>and<textarea>.contenteditableis harder to handle and validate. - Always sanitize content pasted by users before you save it on the server.
Related entries
#Spotted a mistake or missing an example? Write to us.