Sign up (with export icon)

Highlight

Contribute to this guide Show the table of contents

The highlight feature lets you mark text fragments with different colors. You can use it both as a marker (to change the background color) and as a pen (to change the text color).

Demo

Copy link

Select the text you want to highlight. Then use the highlight toolbar button Highlight to choose a desired color from the dropdown.

The Highlight feature example.

Here are some markers:

  • The yellow one
  • The pink one
  • The green one
  • The blue one

Here are some pens: the red one and the green one.

Installation

Copy link

After installing the editor, add the feature to your plugin list and toolbar configuration:

import { ClassicEditor, Highlight } from 'ckeditor5';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		licenseKey: '<YOUR_LICENSE_KEY>', // Or 'GPL'.
		plugins: [ Highlight, /* ... */ ],
		toolbar: [ 'highlight', /* ... */ ]
		highlight: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
Copy code

Configuring the highlight options

Copy link

The highlight feature comes with flexible configuration options.

However, the plugin has a predefined and limited number of available colors. It also focuses more on functionality than aesthetics. To change the text and background colors, use the font color and background color plugin.

The highlight feature uses inline <mark> elements in the view.

Copy link

You can configure which highlight options are supported by the editor. Use the highlight.options configuration and define your highlight styles.

For example, the following editor supports two styles (a green marker and a red pen):

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        highlight: {
            options: [
                {
                    model: 'greenMarker',
                    class: 'marker-green',
                    title: 'Green marker',
                    color: 'var(--ck-content-highlight-marker-green)',
                    type: 'marker'
                },
                {
                    model: 'redPen',
                    class: 'pen-red',
                    title: 'Red pen',
                    color: 'var(--ck-content-highlight-pen-red)',
                    type: 'pen'
                }
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

This editor comes with two highlighters: the green one and the red one.

Inline buttons

Copy link

Instead of using the (default) 'highlight' button, the feature also supports a configuration with separate buttons available directly in the toolbar:

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        toolbar: {
            items: [
                'heading',
                '|',
                'highlight:yellowMarker', 'highlight:greenMarker', 'highlight:pinkMarker',
                'highlight:greenPen', 'highlight:redPen', 'removeHighlight',
                '|',
                'undo', 'redo'
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

The Highlight feature example.

Here are some markers:

  • The pink one
  • The green one

Here are some pens: the red one and the green one.

Colors and styles

Copy link
Note

See the plugin options to learn more about defaults.

Using CSS variables

Copy link

The highlight feature is using the power of CSS variables defined in the style sheet. Thanks to that, both the UI and the content styles share the same color definitions that you can customize:

:root {
    /* Make green a little darker. */
    --ck-content-highlight-marker-green: #199c19;

    /* Make the yellow more "dirty". */
    --ck-content-highlight-marker-yellow: #cac407;

    /* Make red more pinkish. */
    --ck-content-highlight-pen-red: #ec3e6e;
}
Copy code

An example of highlight colors customized using CSS variables:

  • The green one
  • The yellow one
  • The red one

Inline color definitions

Copy link

You can use inline color values in the rgba(R, G, B, A), #RRGGBB[AA], or hsla(H, S, L, A) formats instead of CSS variables. To do that, customize the options and define the color property for each option:

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        highlight: {
            options: [
                {
                    model: 'greenMarker',
                    class: 'marker-green',
                    title: 'Green marker',
                    color: 'rgb(25, 156, 25)',
                    type: 'marker'
                },
                {
                    model: 'yellowMarker',
                    class: 'marker-yellow',
                    title: 'Yellow marker',
                    color: '#cac407',
                    type: 'marker'
                },
                {
                    model: 'redPen',
                    class: 'pen-red',
                    title: 'Red pen',
                    color: 'hsl(343, 82%, 58%)',
                    type: 'pen'
                }
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Then, update the classes in the style sheet so the content corresponds to the UI of the editor. The UI buttons and the actual highlights in the text should be the same color.

.marker-green {
    background-color: rgb(25, 156, 25);
}
.marker-yellow {
    background-color: #cac407;
}
.pen-red {
    color: hsl(343, 82%, 58%);
}
Copy code

An example of highlight colors defined as RGB, HEX or HSL:

  • The green one
  • The yellow one
  • The red one
Copy link

CKEditor 5 has more features that can help you style your content:

Common API

Copy link

The Highlight plugin registers:

  • The 'highlight' dropdown.

  • The 'highlight' command.

    The number of options and their names correspond to the highlight.options configuration option.

    You can change the highlight of the current selection by executing the command with a desired value:

    editor.execute( 'highlight', { value: 'yellowMarker' } );
    
    Copy code

    The value corresponds to the model property in the configuration object. For the default configuration:

    highlight.options = [
        { model: 'yellowMarker', class: 'marker-yellow', title: 'Yellow Marker', color: 'var(--ck-content-highlight-marker-yellow)', type: 'marker' },
        { model: 'greenMarker', class: 'marker-green', title: 'Green marker', color: 'var(--ck-content-highlight-marker-green)', type: 'marker' },
        { model: 'pinkMarker', class: 'marker-pink', title: 'Pink marker', color: 'var(--ck-content-highlight-marker-pink)', type: 'marker' },
        { model: 'blueMarker', class: 'marker-blue', title: 'Blue marker', color: 'var(--ck-content-highlight-marker-blue)', type: 'marker' },
        { model: 'redPen', class: 'pen-red', title: 'Red pen', color: 'var(--ck-content-highlight-pen-red)', type: 'pen' },
        { model: 'greenPen', class: 'pen-green', title: 'Green pen', color: 'var(--ck-content-highlight-pen-green)', type: 'pen' }
    ]
    
    Copy code

    the 'highlight' command will accept the corresponding strings as values:

    • 'yellowMarker' – available as the 'highlight:yellowMarker' button,
    • 'greenMarker' – available as the 'highlight:greenMarker' button,
    • 'pinkMarker' – available as the 'highlight:pinkMarker' button,
    • 'blueMarker' – available as the 'highlight:blueMarker' button,
    • 'redPen' – available as the 'highlight:redPen' button,
    • 'greenPen' – available as the 'highlight:greenPen' button.

    Passing an empty value will remove any highlight attribute from the selection:

    editor.execute( 'highlight' );
    
    Copy code
Note

We recommend using the official CKEditor 5 inspector for development and debugging. It will give you tons of useful information about the state of the editor such as internal data structures, selection, commands, and many more.

Contribute

Copy link

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