How to Use the contenteditable Attribute

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.

  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
Author: Chris Minnick

Chris Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and XHTML.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.