# Attributes and inline formatting

In CKEditor 5, inline formatting (such as bold, italic, or font size) is stored as text attributes on the model’s text nodes. This guide explains how those attributes work and how they are converted between the model and view layers. To follow it, you should first understand how [conversion](intro.md) works.

<a id="inline-and-block-content">

## Inline and block content

Generally speaking, there are two main types of content in the editor view and data output: inline and block.

The inline content means elements like `<strong>`, `<a>`, or `<span>`. Unlike `<p>`, `<blockquote>`, or `<div>`, the inline elements do not structure the data. Instead, they format some text in a specific (visual and semantical) way. These elements are characteristic of text. For instance, you could say that some part of the text is bold, is linked, and so on. This concept has its reflection in the model of the rich-text editor, where `<a>` or `<strong>` are not represented as elements. Instead, they are the attributes of the text.

For example – in the model, you might have a `<paragraph>` element with the “Foo bar” text, where “bar” has the `bold` attribute set to `true`. A pseudo-code of this _model_ data structure could look as follows:

```html
<paragraph>
	"Foo "			// no attributes
	"bar"			// bold=true
</paragraph>
```

> **Note**
>
> Throughout the rest of this guide, the following, shorter convention will be used to represent model text attributes for the sake of clarity:
>
> ```html
> <paragraph>Foo <$text bold="true">bar</$text></paragraph>
> ```

Note that there is no `<strong>` or any other additional element there; it is just some text with an attribute.

So, when does this text become wrapped with a `<strong>` element? This happens during the conversion to the view. It is also important to know what type of view element needs to be used. In the case of the elements that represent inline formatting, this should be an [attribute element](../../../api/module_engine_view_attributeelement-ViewAttributeElement.md).

<a id="conversion-of-multiple-text-attributes">

## Conversion of multiple text attributes

A model text node may have multiple attributes (for example, be bolded and linked), and all of them are converted into their respective view elements by independent converters.

Keep in mind that in the model, attributes do not have any specific order. This is contrary to the editor view or HTML output, where inline elements are nested in one another. Fortunately, the nesting happens automatically during the conversion from the model to the view. This makes working in the model simpler, as features do not need to take care of breaking or rearranging elements in the model.

For instance, consider the following model structure:

```html
<paragraph>
	<$text bold="true" linkHref="url">Foo </$text>
	<$text linkHref="url">bar</$text>
	<$text bold="true"> baz</$text>
</paragraph>
```

During the conversion, it will be converted to the following view structure:

```html
<p>
	<a href="url"><strong>Foo </strong>bar</a><strong> baz</strong>
</p>
```

Note that the `<a>` element is converted in such a way that it always becomes the “topmost” element. This is intentional so that no element ever breaks a link, which would otherwise look as follows:

```html
<p>
	<strong><a href="url">Foo </a></strong><a href="url">bar</a><strong> baz</strong>
</p>
```

There are two links with the same `href` attribute next to each other in the generated view (editor output), which is semantically wrong. To make sure that this never happens, the view element that represents a link must have a [priority](../../../api/module_engine_view_attributeelement-ViewAttributeElement.md#member-priority) defined. Most elements, like `<strong>`, do not care about it and stick to the default priority (`10`). The [Link feature](../../../features/link.md) ensures that all `<a>` view elements have the priority set to `5`, therefore they are kept outside other elements.

<a id="merging-attribute-elements-during-conversion">

## Merging attribute elements during conversion

Priority is not only important for determining the nesting order of attribute elements. It also plays a crucial role in deciding whether adjacent attribute elements can be merged during the conversion process.

Inline elements in the view, such as `<strong>` or `<em>`, are typically simple formatting elements without additional attributes. However, some features require more complex styling. For example, the [Font family feature](../../../features/font.md) adds a `fontFamily` attribute to text in the model. During downcast, this model attribute is converted to a view `<span>` element with a corresponding `style` attribute, which then appears as a `<span style="font-family: ...">` in the HTML output.

What would, then, happen if several model attributes were set on the same part of the text? Take this model example where `fontSize` is used next to `fontFamily`:

```html
<paragraph>
	<$text fontFamily="Tahoma" fontSize="big">foo</$text>
</paragraph>
```

CKEditor 5 features are implemented in a granular way, which means that the font size converter is completely independent from the font family converter. This means that the above example is converted as follows:

* `fontFamily="value"` converts to `<span style="font-family: value;">`,
* `fontSize="value"` converts to `<span class="text-value">`.

And, in theory, you could expect the following HTML as a result:

```html
<p>
	<span style="font-family: Tahoma;">
		<span class="text-big">foo</span>
	</span>
</p>
```

But this is not the most optimal output you can get from the rich-text editor. A single `<span>` element would be better:

```html
<p>
	<span style="font-family: Tahoma;" class="text-big">foo</span>
</p>
```

A single `<span>` makes more sense. And thanks to the merging mechanism built into the conversion process, this is the actual output of the conversion.

Why is this so? In the above scenario, two model attributes are converted to `<span>` elements. When the engine converts the first attribute (say, `fontFamily`), there is no `<span>` in the view yet. So the first `<span>` is added with the `style` attribute. But then, when the engine converts `fontSize`, the `<span>` is already in the view. The [downcast writer](../../../api/module_engine_view_downcastwriter-ViewDowncastWriter.md) recognizes it and checks whether these elements can be merged, following these three rules:

1. Both elements must have the same [name](../../../api/module_engine_view_element-ViewElement.md#member-name).
2. Both elements must have the same [priority](../../../api/module_engine_view_attributeelement-ViewAttributeElement.md#member-priority).
3. Neither can have an [ID](../../../api/module_engine_view_attributeelement-ViewAttributeElement.md#member-id).

If you need to keep attributes separated (for example, to prevent merging between two visually identical but semantically distinct decorations), set a unique `id`. Otherwise, leave the `id` unset and let the engine merge.

> **Note**
>
> When using the [General HTML Support (GHS)](../../../features/html/general-html-support.md) feature, be aware that the merging behavior can affect how the editor processes your HTML. For instance, adjacent `<span>` elements with no distinguishing attributes may be merged into a single element during the model-to-view conversion, as described in [this issue](https://github.com/ckeditor/ckeditor5/issues/15408). Conversely, adding an inline element (like `<a>`) inside a `<span>` may cause the `<span>` to split into multiple elements due to the nesting and priority rules. If you need to preserve the exact structure of your HTML, consider using element IDs or adjusting your content structure to work with CKEditor 5’s attribute element behavior.

<a id="further-reading">

## Further reading

If you want to learn more about converting model attributes, we recommend checking out the following guides:

* [Implementing a block widget](../../tutorials/widgets/implementing-a-block-widget.md#defining-converters) – A practical tutorial showing conversion in action.
* [Implementing an inline widget](../../tutorials/widgets/implementing-an-inline-widget.md#defining-converters) – Learn how to implement inline widgets with custom conversion.

---

Full index of the CKEditor 5 documentation: [llms.txt](../../../../llms.txt)
