Sign up (with export icon)

Editor placeholder

Contribute to this guide Show the table of contents

You can prompt the user to input content by displaying a configurable placeholder text when the editor is empty. This works similarly to the native DOM placeholder attribute used by inputs. Not to be confused with the content placeholders offered by the merge fields feature.

Demo

Copy link

See the demo of the placeholder feature:

Installation

Copy link

The editor placeholder feature does not require a separate plugin installation. It does, however, require configuring the editor before use. There are two different ways of configuring the editor placeholder text:

Using the placeholder attribute of a textarea

Copy link

Set the placeholder attribute on a <textarea> element passed to the Editor.create() method (for instance ClassicEditor.create()) to configure the placeholder:

<textarea id="editor" placeholder="Type the content here!"></textarea>
Copy code
import { ClassicEditor, Essentials } from 'ckeditor5';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		licenseKey: '&lt;YOUR_LICENSE_KEY&gt;', // Or 'GPL'.
		plugins: [ Essentials, /* ... */ ],
	} )
	.then( editor =&gt; {
		console.log( editor );
	} )
	.catch( error =&gt; {
		console.error( error );
	} );
Copy code

Using the editor configuration

Copy link

You can use the editor.config.placeholder configuration option:

  • when no element was passed into Editor.create() method,
  • when the element passed into Editor.create() was not a <textarea> (for instance, a <div> element),
  • to override the placeholder text of a <textarea>, if one was passed into Editor.create() but the placeholder text should be different.
ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        placeholder: 'Type the content here!'
    } )
    .then( editor => {
        console.log( editor );
    } )
    .catch( error => {
        console.error( error );
    } );
Copy code

If your editor implementation uses multiple roots, you should pass an object with keys corresponding to the editor roots names and values equal to the placeholder that should be set in each root:

MultiRootEditor
    .create(
    // Roots for the editor:
        {
            header: document.querySelector( '#header' ),
            content: document.querySelector( '#content' ),
            leftSide: document.querySelector( '#left-side' ),
            rightSide: document.querySelector( '#right-side' )
        },
        // Config:
        {
            placeholder: {
                header: 'Type header...',
                content: 'Type content...',
                leftSide: 'Type left-side...',
                rightSide: 'Type right-side...'
            }
        } )
    .then( editor => {
        console.log( editor );
    } )
    .catch( error => {
        console.error( error );
    } );
Copy code

Styling the placeholder

Copy link

The editor placeholder text is displayed using a CSS pseudo–element (::before) related to the first empty element in the editor content (usually a paragraph). You can use the following snippet to change the appearance of the placeholder:

.ck.ck-editor__editable > .ck-placeholder::before {
    color: #d21714;
    font-family: Georgia;
}
Copy code

Note: The .ck-placeholder class is also used to display placeholders in other places, for instance, image captions. Make sure your custom styles apply to the right subset of placeholders.

Changing the placeholder

Copy link

The editor placeholder could be updated at runtime by changing the placeholder property in the editing root.

editor.editing.view.document.getRoot( 'main' ).placeholder = 'new placeholder';
Copy code

Contribute

Copy link

The source code of the feature is available on GitHub at https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-core.