HTML contenteditable Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 2 Examples
Interactive UI

Introduction

The contenteditable attribute is a powerful feature in HTML that allows developers to make elements editable by users. This attribute provides a straightforward way to turn elements like <div>, <span>, or even sections of a page into a user-editable area. It is particularly useful for creating rich-text editors and interactive content.

What You’ll Learn

01

true / false

Edit on or off.

02

Global Attr

div, p, span.

03

Live Editing

Type in browser.

04

contentEditable

JS property.

05

vs textarea

Rich vs plain.

06

A11y & Save

Labels, sanitize.

Purpose of contenteditable

The primary purpose of the contenteditable attribute is to make an element editable, enabling users to modify the content directly in the browser. This can be advantageous for creating dynamic and interactive web pages where users can engage with the content in real time.

💡
Not the same as content

The content attribute sets metadata on <meta> tags. contenteditable controls whether users can edit visible page content.

📝 Syntax

Add contenteditable to any supported element:

contenteditable.html
<div contenteditable="true">Edit me</div>
<p contenteditable="false">Read only</p>
<span contenteditable>Empty value = true</span>

Syntax Rules

  • Boolean-style attribute: true, false, or empty (empty means editable).
  • inherit follows the parent’s editable state when explicitly set.
  • Default is not editable unless inherited from an editable ancestor.
  • JavaScript uses element.contentEditable (capital E).

💎 Values

The contenteditable attribute accepts different values to define the editing behavior of the element:

  • true — The element is editable. Users can modify the content.
  • false — The element is not editable. This is the default when no editable ancestor exists.
  • inherit — The element inherits the editable behavior from its parent element.
contenteditable-values.html
<div contenteditable="true">
  <p contenteditable="inherit">Inherits from parent</p>
  <p contenteditable="false">Locked paragraph</p>
</div>

⚡ Quick Reference

ValueMarkupBehavior
Editablecontenteditable="true"User can type and edit
Editable (shorthand)contenteditableSame as true
Not editablecontenteditable="false"Read-only region
Inheritcontenteditable="inherit"Follow parent
JavaScriptel.contentEditable = "true"Toggle at runtime
Read edited HTMLel.innerHTMLSave or process content

Applicable Elements

ElementSupported?Notes
<div>, <p>, <span>YesCommon editable containers
<h1><h6>, <li>YesHeadings and list items
<textarea>, <input>Already editableUse native form controls for forms
<button>DiscouragedConflicts with button behavior
<meta>No practical useNot for metadata values

contenteditable vs textarea

Featurecontenteditabletextarea
Rich formattingYes (bold, lists via browser)Plain text only
Form submissionNot automaticNative form field
Best forInline editors, notes UIComments, messages
AccessibilityNeeds extra ARIA/labelsBuilt-in form semantics

Examples Gallery

Editable div with contenteditable and a JavaScript toggle button.

👀 Live Preview

Click inside the box and edit the text:

This content is editable. Try typing here!

Example

Let’s look at a simple example of how to use the contenteditable attribute in an HTML document:

contenteditable.html
<div contenteditable="true">
  <p>This content is editable. Try typing here!</p>
</div>
Try It Yourself

How It Works

In this example, the contenteditable attribute is set to true for a <div> element, allowing users to edit the contained paragraph.

Dynamic Values with JavaScript

You can dynamically toggle the contenteditable attribute using JavaScript to create interactive experiences. Here’s an example:

toggle-contenteditable.html
<button type="button" onclick="toggleEditable()">Toggle Editable</button>
<div id="editableElement">
  <p>This content can be dynamically edited.</p>
</div>

<script>
  function toggleEditable() {
    var element = document.getElementById("editableElement");
    element.contentEditable = (element.contentEditable === "true") ? "false" : "true";
  }
</script>
Try It Yourself

How It Works

In this script, a button click toggles the contenteditable state of a <div> element with the id editableElement. This showcases how you can use JavaScript to provide dynamic control over the editing behavior.

♿ Accessibility

  • Announce editability — Use visible labels or aria-label so users know an area is editable.
  • Provide instructions — Tell users how to save or that changes are temporary.
  • Keyboard support — Editable regions should be focusable; avoid trapping keyboard users.
  • Screen readers — Test with assistive tech; editing may behave differently than form fields.
  • Prefer native inputs — For simple text entry in forms, use <input> or <textarea>.

🧠 How contenteditable Works

1

Author enables editing

Set contenteditable to true on a container element.

Markup
2

User focuses the region

Click or Tab into the element to start editing.

Interaction
3

Browser updates the DOM

Typed text and formatting changes live in innerHTML.

Edit
=

Interactive editing experience

Save with JavaScript or use a library for production rich-text editors.

Browser Support

The contenteditable attribute is supported in all modern browsers. Formatting behavior may vary slightly between engines.

HTML5 · Fully supported

Universal in-browser editing

All major browsers honor contenteditable on supported elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
contenteditable attribute 99% supported

Bottom line: Use contenteditable confidently; test rich formatting if your app depends on it.

💡 Best Practices

✅ Do

  • Use contenteditable thoughtfully for inline notes or simple editors
  • Provide clear visual cues that an area is editable
  • Sanitize HTML before saving user-edited content to a server
  • Test across browsers for formatting consistency
  • Use textarea or input for standard form fields

❌ Don’t

  • Confuse contenteditable with the meta content attribute
  • Make entire pages editable without user consent
  • Trust raw innerHTML from users without sanitization
  • Replace accessible form controls without ARIA planning
  • Forget to persist edits if users expect saved data
  • Use the contenteditable attribute thoughtfully, considering the user experience and the nature of the content.
  • Be mindful of accessibility concerns, as editing content may affect users who rely on assistive technologies.
  • Test your implementation across different browsers to ensure consistent behavior.

Conclusion

The contenteditable attribute is a versatile tool for creating interactive and editable content on the web. By understanding its usage and incorporating it judiciously into your projects, you can enhance user engagement and create dynamic user interfaces.

For production rich-text editing, many teams combine contenteditable with JavaScript libraries or sanitize and save content explicitly. For simple forms, native inputs remain the best choice.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about contenteditable

Bookmark these before building an editable UI.

5
Core concepts
02

true / false

Edit control.

Values
⚙️ 03

contentEditable

JS toggle.

Script
📝 04

innerHTML

Read edits.

Save
05

A11y Matters

Label regions.

UX

❓ Frequently Asked Questions

It lets users edit the content inside an element directly in the browser, similar to a mini word processor.
No. content is for meta tag values. contenteditable controls user editing of visible elements.
It works on most HTML elements. Avoid buttons and prefer semantic containers like div or p.
Read element.innerHTML or textContent with JavaScript and send it to your server. Sanitize HTML before storing.
Set element.contentEditable = "true" or "false". The property name uses a capital E.
Yes in all modern browsers. Test formatting commands if your app relies on bold, lists, or paste behavior.

Build editable web experiences

Practice the contenteditable attribute with live editing and JavaScript toggle examples in the Try It editor.

Try editable div example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

2 people found this page helpful