Format Painter

This plugin is only available for paid TinyMCE subscriptions.

The Format Painter plugin allows a user to copy and paste formatting from one location to another, such as font style and size. The format painter is capable of working with a wide variety of formats such as inline and block formats, table styles, and inline styles applied directly to block elements, such as the text color of a heading.

Interactive example

The format painter retains the formatting after application making it possible to apply the same formatting multiple times by using the Ctrl+Alt+V keyboard shortcut.

  • TinyMCE

  • HTML

  • JS

  • Edit on CodePen

<textarea id="format-painter">
  <p><img style="display: block; margin-left: auto; margin-right: auto;" title="Tiny Logo" src="https://www.tiny.cloud/docs/tinymce/latest/_images/logos/android-chrome-256x256.png" alt="TinyMCE Logo" width="128" height="128"></p>

  <h2 style="text-align: center;">Welcome to the TinyMCE editor demo!</h2>

  <h2>Got questions or need help?</h2>

  <ul>
    <li>Our <a href="https://www.tiny.cloud/docs/tinymce/8/">documentation</a> is a great resource for learning how to configure TinyMCE.</li>
    <li>Have a specific question? Try the <a href="https://stackoverflow.com/questions/tagged/tinymce" target="_blank" rel="noopener"><code>tinymce</code> tag at Stack Overflow</a>.</li>
    <li>We also offer enterprise grade support as part of <a href="https://www.tiny.cloud/pricing">TinyMCE premium plans</a>.</li>
  </ul>

  <h2>A simple table to play with</h2>

  <table style="border-collapse: collapse; width: 100%;" border="1">
    <thead>
      <tr>
        <th>Product</th>
        <th>Cost</th>
        <th>Really?</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align: center;">TinyMCE</td>
        <td style="text-align: center;">Free</td>
        <td style="text-align: center;">YES!</td>
      </tr>
    </tbody>
  </table>

  <h2>Found a bug?</h2>

  <p>
    If you think you have found a bug please create an issue on the <a href="https://github.com/tinymce/tinymce/issues">GitHub repo</a> to report it to the developers.
  </p>

  <h2>Finally ...</h2>

  <p>
    Need file uploads in your app? Consider using <a href="https://www.tiny.cloud/docs/tinymce/latest/uploadcare/" target="_blank" rel="noopener noreferrer">Uploadcare</a> with TinyMCE for a fast, modern upload experience.
  </p>
  <p>
    Thanks for supporting TinyMCE! We hope it helps you and your users create great content.<br>All the best from the TinyMCE team.
  </p>
</textarea>
tinymce.init({
  selector: 'textarea#format-painter',
  plugins: 'formatpainter code',
  toolbar: 'formatpainter code',
  formats: {
    borderstyle: { selector: 'td,th', styles: { borderTopStyle: 'solid', borderRightStyle: 'solid', borderBottomStyle: 'solid', borderLeftStyle: 'solid', }, remove_similar: true },
    bordercolor: { selector: 'td,th', styles: { borderTopColor: '#32CD32', borderRightColor: '#32CD32', borderBottomColor: '#32CD32', borderLeftColor: '#32CD32' }, remove_similar: true },
    backgroundcolor: { selector: 'td,th', styles: { backgroundColor: '#006400' }, remove_similar: true },
    formatpainter_removeformat: [
      { selector: 'b,strong,em,i,font,u,strike,sub,sup,dfn,code,samp,kbd,var,cite,mark,q,del,ins',
        remove: 'all',
        split: true,
        expand: false,
        block_expand: true,
        deep: true
      },
      { selector: 'span',
        attributes: ['style', 'class'],
        remove: 'empty',
        split: true,
        expand: false,
        deep: true
      },
      { selector: '*:not(tr,td,th,table)',
        attributes: ['style', 'class'],
        split: false,
        expand: false,
        deep: true
      }
    ]
  },
  height: 500,
  content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:16px }'
});

Basic setup

To add the Format Painter plugin to the editor, add formatpainter to the plugins option in the editor configuration.

For example:

tinymce.init({
  selector: 'textarea',  // change this value according to your HTML
  plugins: 'formatpainter',
  toolbar: 'formatpainter'
});

For information on bundling this plugin with module bundlers, see Bundling TinyMCE - Overview.

Using Format Painter

The format painter is accessed using either keyboard shortcuts or a toolbar button.

The format painter operates in two modes, one for retrieval and one for application of formatting. The usage of the keyboard is slightly different from the usage of the toolbar buttons.

Usage with toolbar button

  1. Position the cursor at the source content to retrieve the formatting.

  2. Enable the Format Painter button to copy the formatting. The Format Painter toolbar button background color changes to grey the Format Painter toolbar button icon.

  3. Select the goal content to apply the formatting.

Result: The formatting from the source content is applied to the goal content.

Usage with keyboard

  1. Position the cursor at the source content to retrieve formatting.

  2. Press the Ctrl+Alt+C keys to read the formatting.

  3. Select the goal content to apply the formatting.

  4. Press the Ctrl+Alt+V keys to apply the formatting.

The format painter retains the formatting after application making it possible to apply the same formatting multiple times by using the Ctrl+Alt+V keyboard shortcut.

How formats are copied and applied

Inline styles on block elements

When the source content is inside a block element that has inline styles applied directly to it, such as <h2 style="color: #e03e2d;">, the format painter copies each style that matches a registered inline format. The default inline formats that match styles are forecolor (color), hilitecolor (background-color), fontname (font-family), and fontsize (font-size). The format painter applies these styles to the goal content as inline formatting in a span element. When more than one element around the source content sets the same style, the format painter uses the value from the closest element.

Custom formats registered with the formats option are also copied this way when they are inline formats whose styles use only variable values, for example { inline: 'span', styles: { color: '%value' } }. Formats listed in formatpainter_ignored_formats are not copied.

Block formats

The format painter applies block formats, such as a heading, when the goal content is one of the following:

  • The cursor is placed in a block, with no content selected.

  • The selection crosses a block boundary.

  • The selection covers the entire content of a single block, such as a whole paragraph or a whole list item.

When the selection covers only part of a block, the format painter applies inline formats only.

Options

The format painter acts upon all formats registered in the editor. In addition to any standard formatting, it will treat lists as a block format whenever the lists plugin is made available. For how the format painter copies inline styles applied to block elements and when it applies block formats, see How formats are copied and applied.

formats

The format painter plugin will register many formats upon initialization. To override these formats, use the formats option.

The example below showcases the formats registered automatically by the plugin upon initialization. The formatpainter_removeformat is used to clear any existing formats before applying the new ones. It is similar to the removeformat format.

Type: Object

Example: using formats

tinymce.init({
  selector: 'textarea',  // change this value according to your HTML
  plugins: 'formatpainter',
  toolbar: 'formatpainter',
  formats: {
    formatpainter_checklist: { selector: 'ul', classes: 'tox-checklist' },
    formatpainter_liststyletype: { selector: 'ul,ol', styles: { listStyleType: '%value' } },
    formatpainter_borderstyle: { selector: 'td,th', styles: { borderTopStyle: '%valueTop', borderRightStyle: '%valueRight', borderBottomStyle: '%valueBottom', borderLeftStyle: '%valueLeft', }, remove_similar: true },
    formatpainter_bordercolor: { selector: 'td,th', styles: { borderTopColor: '%valueTop', borderRightColor: '%valueRight', borderBottomColor: '%valueBottom', borderLeftColor: '%valueLeft' }, remove_similar: true },
    formatpainter_backgroundcolor: { selector: 'td,th', styles: { backgroundColor: '%value' }, remove_similar: true },
    formatpainter_removeformat: [
      {
        selector: 'b,strong,em,i,font,u,strike,sub,sup,dfn,code,samp,kbd,var,cite,mark,q,del,ins',
        remove: 'all', split: true, expand: false, block_expand: true, deep: true
      },
      { selector: 'span', attributes: [ 'style', 'class' ], remove: 'empty', split: true, expand: false, deep: true },
      { selector: '*:not(tr,td,th,table)', attributes: [ 'style', 'class' ], split: false, expand: false, deep: true }
    ]
  }
});

formatpainter_ignored_formats

This option makes it possible to block the unwanted formats in the format painter.

Ignored formats are also not copied from inline styles applied directly to block elements. For example, adding forecolor to this option stops the format painter copying the text color of a heading or paragraph. For more information, see How formats are copied and applied.

Type: String

Default value: 'link,address,removeformat,formatpainter_removeformat'

Example: using formatpainter_ignored_formats

tinymce.init({
  selector: 'textarea',  // change this value according to your HTML
  plugins: 'formatpainter',
  toolbar: 'formatpainter',
  formatpainter_ignored_formats: 'link,address,removeformat,formatpainter_removeformat'
});

Toolbar buttons

The Format Painter plugin provides the following toolbar buttons:

Toolbar button identifier Description

formatpainter

Enables and disables format painting.

These toolbar buttons can be added to the editor using:

The Format Painter plugin provides the following menu items:

Menu item identifier Default Menu Location Description

formatpainter

Not Applicable

Enables and disables format painting.

These menu items can be added to the editor using:

Commands

The Format Painter plugin provides the following TinyMCE commands.

Command Description

mceRetrieveFormats

Retrieves the formats from the current selection.

mcePaintFormats

Applies the formats retrieved using mceRetrieveFormats to the current selection.

mceToggleFormatPainter

Toggles the Format Painter.

Examples
tinymce.activeEditor.execCommand('mceRetrieveFormats');
tinymce.activeEditor.execCommand('mcePaintFormats');
tinymce.activeEditor.execCommand('mceToggleFormatPainter');