Skip to content

Font Size

FontSize adds font size styling via the TextStyle carrier mark. It provides a dropdown with 6 default sizes, setFontSize and unsetFontSize commands, and a dynamic label that shows the current font size in the toolbar trigger. The trigger reads both inline and computed styles, so it always reflects the actual rendered size. The size list is fully customizable, and any size is accepted from pasted HTML regardless of the configured list.

Not included in StarterKit. Add it separately.

Requires the TextStyle mark, which is automatically included as a dependency.

Select some text and pick a size from the dropdown. The dropdown trigger label updates to show the font size at the cursor position.

With the default theme. The toolbar shows a font size dropdown with a dynamic label and 6 size options.

Click to try it out
import { Editor, Document, Paragraph, Text, FontSize } from '@domternal/core';
import '@domternal/theme';
const editor = new Editor({
element: document.getElementById('editor')!,
extensions: [Document, Paragraph, Text, FontSize],
content: '<p>Select text and pick a size from the toolbar dropdown.</p>',
});

With the full theme and ToolbarController, FontSize renders as a dropdown with a dynamic label that shows the current font size. The label reads both inline marks and computed styles.

OptionTypeDefaultDescription
fontSizesstring[]['12px', '14px', '16px', '18px', '24px', '32px']Size values shown in the toolbar dropdown. Commands accept any size regardless of this list.
showResetbooleanfalseShow a reset button at the end of the dropdown that calls unsetFontSize()

The default list includes 6 sizes:

12px, 14px, 16px, 18px, 24px, 32px

FontSize.configure({
fontSizes: ['10px', '13px', '16px', '20px', '28px', '36px', '48px'],
showReset: true,
})

Passing an empty array disables the toolbar dropdown entirely. No toolbar items are registered, but the commands still work.

FontSize.configure({ fontSizes: [] }) // no toolbar, commands only

Sets the font size on the current selection. Accepts any CSS size value as a string.

editor.commands.setFontSize('24px');
// With chaining
editor.chain().focus().setFontSize('32px').run();

Under the hood, this calls setMark('textStyle', { fontSize }) to apply the size attribute on the TextStyle carrier mark.

Removes the font size from the current selection, resetting it to the browser default. Also cleans up empty <span> wrappers via removeEmptyTextStyle().

editor.commands.unsetFontSize();

FontSize does not register any keyboard shortcuts.

FontSize does not register any input rules.

FontSize registers a dropdown with dynamicLabel in the toolbar.

DropdownIconGroupPriorityDisplay mode
fontSizetextSizetextStyle100text

The dropdown trigger uses dynamicLabel mode with computedStyleProperty: 'font-size' and dynamicLabelFallback: '16px': instead of showing a fixed icon, it displays the current font size as text. The trigger label updates as the cursor moves through differently-sized text.

Cursor positionTrigger shows
Text with font-size: 24px24px
Text with font-size: 32px32px
Text with no explicit sizeComputed size (e.g. 16px)
Custom size not in listComputed size from DOM

The label resolution follows this order:

  1. If a dropdown item is active (cursor in text matching a configured size), show that item’s label
  2. Otherwise, read the computed font-size at the cursor position (inline style first, then inherited)
  3. If nothing found, show the dynamicLabelFallback value (16px)

Each size in the fontSizes array becomes a dropdown item:

PropertyValue
namefontSize-{size} (e.g., fontSize-24px)
commandsetFontSize
commandArgs[size]
isActive{ name: 'textStyle', attributes: { fontSize: size } }
icontextSize
labelSize value (e.g., 24px)
priority200 - index (first size = 200, last = 195)

When showReset: true, a reset button is added at the end of the dropdown:

PropertyValue
nameunsetFontSize
commandunsetFontSize
icontextSize
label
priority0 (always last)

FontSize does not define its own mark. It injects a fontSize attribute into the TextStyle mark using addGlobalAttributes():

addGlobalAttributes() {
return [{
types: ['textStyle'],
attributes: {
fontSize: {
default: null,
parseHTML: (element) => element.style.fontSize || null,
renderHTML: (attributes) => {
if (!attributes.fontSize) return null;
return { style: `font-size: ${attributes.fontSize}` };
},
},
},
}];
}

This means FontSize, TextColor, FontFamily, and Highlight all share the same <span> wrapper:

<span style="font-size: 24px; color: #e03131; font-family: Georgia">styled text</span>

FontSize declares TextStyle as both a dependency (dependencies: ['textStyle']) and includes it via addExtensions(). You do not need to add TextStyle separately.

FontSize reads computed styles at the cursor position, not just inline styles. This is different from FontFamily:

ExtensionStyle readingWhy
FontFamilyInline onlyPrevents showing browser default font (e.g. “Times New Roman”)
FontSizeInline + computedAlways shows a meaningful size value

The Angular toolbar reads computed styles via getComputedStyleAtCursor(), which checks inline style first (explicit marks), then falls back to window.getComputedStyle() (inherited/default sizes).

When you call unsetFontSize(), it:

  1. Sets the fontSize attribute to null via setMark('textStyle', { fontSize: null })
  2. Calls removeEmptyTextStyle() to check if the TextStyle mark has any remaining non-null attributes
  3. If all attributes are null (no font-size, no color, no font-family, no background-color), removes the <span> wrapper entirely

Font sizes render as inline styles on <span> elements:

<!-- With size -->
<span style="font-size: 24px">Large text</span>
<!-- No size (default) - no span wrapper -->
Normal text

The fontSizes option only controls the toolbar dropdown. The commands and parser accept any font size string. Pasted HTML with sizes like 48px or 0.875rem is preserved even if those sizes are not in the configured list.

import { FontSize } from '@domternal/core';
import type { FontSizeOptions } from '@domternal/core';
ExportTypeDescription
FontSizeExtensionThe font size extension
FontSizeOptionsTypeScript typeOptions for FontSize.configure()

@domternal/core - FontSize.ts