How to Use the contenteditable Attribute

  • google plus

In Brief...

HTML5's contenteditable attribute can be applied to almost any element. By using contenteditable, you can make a static web page into a rich text editor. The following three steps show how.

Instructions

  1. Create an HTML document, add one or more elements to the body, and style it as you wish.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>My Editor</title>
        <style>
            body {
                font-family: sans-serif;
            }
            #blueBox {
                width: 200px;
                height: 200px;
                background-color: blue;
                color: #fff;
                padding: 10px;
                margin: 8px;
            }
            #redBox {
                width: 200px;
                height: 200px;
                background-color: red;
                color: #fff;
                padding: 10px;
                margin: 8px;
            }
        </style>
    </head>
    <body>
    <h1>Welcome to My Web Page</h1>
    <p>It can be your web page too. Just click on anything and change the text.</p>
    <div id="blueBox">
        Like this.
    </div>
    <div id="redBox">
        Or this.
    </div>
    </body>
    </html>
            
  2. Add contenteditable="true" to every element that you want to be editable.
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>My Editor</title>
        <style>
            body {
                font-family: sans-serif;
            }
            #blueBox {
                width: 200px;
                height: 200px;
                background-color: blue;
                color: #fff;
                padding: 10px;
                margin: 8px;
            }
            #redBox {
                width: 200px;
                height: 200px;
                background-color: red;
                color: #fff;
                padding: 10px;
                margin: 8px;
            }
        </style>
    </head>
    <body>
    <h1 contenteditable="true">Welcome to My Web Page</h1>
    <p contenteditable="true">It can be your web page too. Just click on anything and change the text.</p>
    <div contenteditable="true" id="blueBox">
        Like this.
    </div>
    <div contenteditable="true" id="redBox">
        Or this.
    </div>
    </body>
    </html>
            
    Elements with contenteditable=true look the same as they normally would until you give them focus by clicking on them or tabbing to them.
    Editable Elements Look Like Normal Elements
  3. Open the document in a browser and click on different elements and change the text.
    Changing Text with contenteditable

Related Training

Categories

Courses

Author: Chris Minnick

Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.

Discuss