Learn about content editable CSS attribute and how it is used to make editors for blogging websites in this post.

Cover Image

The contenteditable attribute specifies whether the content of an element is editable or not.
It is generally used to make rich text editors for blogging websites.
You can check out detailed video explanation of this tutorial here:-

Video tutorial

Or follow along in this blog as well:-

We first add a contenteditable attribute to a div tag in HTML:-

<div class=”editor-buttons”>
<a href=”#” data-command=”bold”>
<i class=”fa fa-bold”></i>
<a href=”#” data-command=”italic”>
<i class=”fa fa-italic”></i>
<div contenteditable=”true” class=”editor”>
<p>Lorem ipsum dolor sit amet.</p>

On third last line in the code above we have added content editable attribute with value set to true to allow our div tag to be editable.

Next we add support for fontawesome icons and add some css in our code:-

.editor {
border: 1px solid lightgray;
padding: 5px;

Finally we use execCommand in Javascript to allow our editor to make text bold or italic:-

const links = document.querySelectorAll(‘a’);

You can learn more about execCommand here:- W3Schools

Final codepen can be found here:-

Content editable property is used widely in a lot of popular rich text editor libraries like Draft.js as well.

I hope this short explanation was useful for you all. Let me know you views in comments section.


Blogger,web and hybrid applications developer