Contribute to this guide

guideBasic text styles

The basic styles feature lets you apply the most essential formatting such as bold, italic, underline, strikethrough, subscript, superscript, and code. Coupled with more formatting features, these serve as a base for any WYSIWYG editor toolset.

# Demo

You may apply basic formatting options with toolbar buttons. You can also make use of the autoformatting feature that changes Markdown code to formatted text as you type. Use one of these to format text:

  • Bold – Use the bold toolbar button Bold or type **text** or __text__.
  • Italic – Use the italic toolbar button Italic or type *text* or _text_.
  • Code – Use the code toolbar button Code or type `text`.
  • Strikethrough – Use the strikethrough toolbar button Strikethrough or type ~~text~~.

When you need to make something seem important, bold is the right choice.

You can use italics for foreign words like the Greek týpos — “reflection, form” and gráphō — “I am writing”, which together form “typography”.

Underlined text should be used with extreme caution as it stands out a lot.

Sometimes you may need to mark text as deleted. Strikethrough is the right choice then.

If you write about software, the option to mark inline code like printf("hello, world\n"); comes in handy.

There are also subscript and superscript types that might be useful in texts about chemistry or math where you have to deal with things like H2O or x2.

This demo presents a limited set of features. Visit the feature-rich editor example to see more in action.

# Available text styles

Style feature Command name Toolbar component name Output element
Bold 'bold' 'bold' <strong>bold</strong>
Italic 'italic' 'italic' <i>italic</i>
Underline 'underline' 'underline' <u>underline</u>
Strikethrough 'strikethrough' 'strikethrough' <s>strikethrough</s>
Code 'code' 'code' <code>code</code>
Subscript 'subscript' 'subscript' <sub>subscript</sub>
Superscript 'superscript' 'superscript' <sup>superscript</sup>

The Code feature provides support for inline code formatting. To create blocks of pre-formatted code with a specific programming language assigned, use the code block feature.

# Supported input

By default, each feature can upcast more than one type of content. Here is the full list of elements supported by each feature, either when pasting from the clipboard, loading data on start, or using the data API.

Style feature Supported input elements
Bold <strong>, <b>, <* style="font-weight: bold"> (or numeric values that are greater or equal 600)
Italic <i>, <em>, <* style="font-style: italic">
Underline <u>, <* style="text-decoration: underline">
Strikethrough <s>, <del>, <strike>, <* style="text-decoration: line-through">
Code <code>, <* style="word-wrap: break-word">
Subscript <sub>, <* style="vertical-align: sub">
Superscript <sup>, <* style="vertical-align: super">

# Typing around inline code

CKEditor 5 allows for typing both at the inner and outer boundaries of code to make editing easier for the users.

To type inside a code element, move the caret to its (start or end) boundary. As long as the code remains highlighted (by default: less transparent gray), typing and applying formatting will be done within its boundaries:

The animation showing typing inside the code element in CKEditor 5 rich text editor.

To type before or after a code element, move the caret to its boundary, then press the Arrow key ( or ) once. The code is no longer highlighted and whatever text you type or formatting you apply will not be enclosed by the code element:

The animation showing typing after the code element in CKEditor 5 rich text editor.

# Installation

⚠️ New import paths

Starting with version 42.0.0, we changed the format of import paths. This guide uses the new, shorter format. Refer to the Packages in the legacy setup guide if you use an older version of CKEditor 5.

After installing the editor, add the plugins which you need to your plugin list. Then, simply configure the toolbar items to make the features available in the user interface.

import { ClassicEditor, Bold, Code, Italic, Strikethrough, Subscript, Superscript, Underline } from 'ckeditor5';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ Bold, Code, Italic, Strikethrough, Subscript, Superscript, Underline ],
        toolbar: {
            items: [ 'bold', 'italic', 'underline', 'strikethrough', 'code', 'subscript', 'superscript'  ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

Check out also these CKEditor 5 features to gain better control over your content style and format:

  • Font styles – Easily and efficiently control the font family, size, text or background color.
  • Styles – Apply pre-configured styles to existing elements in the editor content.
  • Text alignment – Because it does matter whether the content is left, right, centered, or justified.
  • Case change – Turn a text fragment or block into uppercase, lowercase, or title case.
  • Code blocks – Insert longer, multiline code listings, expanding the inline code style greatly.
  • Highlight – Mark important words and passages, aiding a review or drawing attention to specific parts of the content.
  • Format painter – Easily copy text formatting and apply it in a different place in the edited document.
  • Autoformatting – Format the text on the go with Markdown code.
  • Remove format – Easily clean basic text formatting.

You can remove all basic text styles with the remove format feature.

# Common API

Each style feature registers a command which can be executed from code. For example, the following snippet will apply the bold style to the current selection in the editor:

editor.execute( 'bold' );

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

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