The format option enables you to override and add custom **formats** to the editor.
The format options are used to override and add custom **formats** to the editor.
A format is a style that gets applied to text when you press, for example, the bold button inside the editor. TinyMCE is equipped with a text formatting engine that enables you to precisely specify what it should produce when the user clicks (in this example) the bold button.
For example, a format is a style that gets applied to text when the bold button is enabled inside the editor. TinyMCE is equipped with a text formatting engine that allows specifying what it should produce when the user clicks (in this example) the bold button.
Check out the [custom formats example]({{ site.baseurl }}/demo/format-custom/) for a demonstration of this option.
Formats are objects where the value is either an object with format options or an array of format variants.
When a format is applied the first item, the formats array will be applied. However, when trying to match the format to remove or update the UI, the other formats in the array is considered as well. So the first format is a kind of primary format, and the rest are secondary formats.
The following is an example of an array of format variants:
Similar elements and styles are merged by default to reduce the output HTML size. So for example, if you select a word and select a font size and font face for it, it merges these two styles into one `span` element instead of one `span` for **each format type**.
Similar elements and styles are merged by default to reduce the output HTML size. So for example, if a font size and font face are selected for a word, it merges these two styles into one `span` element instead of one `span` for **each format type**.
### Built-in formats
TinyMCE has some built in formats that you can override. These are:
TinyMCE has some built in formats that can be overridden. These are:
* alignleft
* aligncenter
@ -38,22 +52,287 @@ TinyMCE has some built in formats that you can override. These are:
* dt, dd
* samp
Some built-in formats `fontsize`, `fontname`, `forecolor`, `hilitecolor` uses a variable in their definition named `%value`. This one gets replaced with the user selected item such as a color value. Check the variable substitution section below for details.
Some built-in formats `fontsize`, `fontname`, `forecolor`, `hilitecolor` uses a variable in their definition named `%value`. This one gets replaced with the user selected item such as a `color` value. Check the variable substitution section below for details.
### Format Type
There are three format types that the default classes can be applied to:
* Block format
* Inline format
* Selector format
#### block
Tag name of the block element to use as a wrapper, for example, `h1`. Existing block elements within the selection are replaced with this block element.
Type: string
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for h1 to have a class of heading
h1: { block: 'h1', classes: 'heading' }
},
style_formats: [
// Adds a h1 format to the formats select dropdown
CSS3 selector pattern that is used to find elements within the selection. It can be used to apply classes to specific elements only, for example only to odd rows in a table.
Type: string
Example
tinymce.init({
selector: 'textarea',
formats: {
// Changes the alignment buttons to add a class to each of the matching selector elements
Key/value object with CSS styles to apply to the selected or newly created inline/block element (e.g. `color`, `backgroundColor`, `textDecoration`, etc).
**Type:** `Object`
##### Example
```js
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the bold button to produce a span with style with font-width: bold
| inline | Tag name of the inline element to use as a wrapper, for example, "span" is used to wrap the current selection.
| block | Tag name of the block element to use as a wrapper, for example, "h1". Existing block elements within the selection are replaced with this block element. |
| selector | CSS3 selector pattern that is used to find elements within the selection. It can be used to apply classes to specific elements only, for example only to odd rows in a table. |
| classes | Space-separated list of classes that are applied to the selected or newly created inline/block element. |
| styles | Key/value object with CSS styles to apply to the selected or newly created inline/block element (e.g. color, backgroundColor, textDecoration, etc). |
| attributes | Key/value object with attributes to apply to the selected or newly created inline/block element. |
| exact | Makes sure that the format is not merged with other wrappers having the same format. We use it to avoid conflicts between text-decorations for underline and strikethrough formats. |
| wrapper | States that the format is a container format for block elements. For example a div wrapper or blockquote. |
#### exact
Makes sure that the format is not merged with other wrappers having the same format. We use it to avoid conflicts between text-decorations for `underline` and `strikethrough` formats.
**Type:** `boolean`
**Default:** `false`
##### Example
```js
tinymce.init({
selector: 'textarea',
formats: {
// Changes the default format for the underline button to produce a span with a class and not merge that underline into parent spans
This option controls if the selection should expand upwards to the closest matching block element. This can be useful when configuring `removeformat` to remove block elements. So if the selection start is at the beginning of a matching block, then that matching block will be included as well. If the end of the selection is at the end of a matching block element then that parent element will be included as well.
So if the selection is from _a_ to _b_ in this html contents `<h1><b>[a</b></h1><p>b]</p>` then the _h1_ will be removed even if it's not part of the actual selection.
Enables control for removing the child elements of the matching format. This is set to `false` by default on selector formats. As a result, when a class is removed from a selected table class, disabling `deep` retains the class in the child elements within the other nested tables.
**Type:** `boolean`
**Default:** `false` for `selector` formats
##### Example
```js
tinymce.init({
selector: 'textarea',
style_formats: [
// A custom format that wraps blocks into a div with the specified wrapper class
You can use variables in your format definition. These variables are then replaced with the ones specified in the call to the apply function. Here is an example of how to use variables within formats.
Variables can be used in the format definition. These variables are then replaced with the ones specified in the call to the apply function. Here is an example of how to use variables within formats.
This option enables you to add more advanced style formats for text and other elements to the editor. The value of this option will be rendered as styles in the `styleselect` dropdown toolbar item.
It's important to note that the `style_formats` option, while similar in syntax, is entirely separate from the `formats` option. This option will only add items to the *Formats* dropdown in the toolbar, not the *Format* dropdown in the menu bar.
It's important to note that the `style_formats` option, while similar in syntax, is entirely separate from the `formats` option. For a complete reference of the different format options then check the [formats]({{ site.baseurl }}/configure/content-formatting/#formats)