Text styling extension for TipTap rich text editor providing color, background, font family, font size, and line height capabilities.
—
The FontSize extension provides font size styling capabilities, allowing users to apply custom font sizes to text selections with support for all CSS font-size units.
Extension for applying font sizes to text with full CSS font-size unit support.
/**
* Extension for setting font size on specified node types
* Extends textStyle mark with fontSize attribute
*/
const FontSize: Extension<FontSizeOptions>;
interface FontSizeOptions {
/**
* Node types where font size can be applied
* @default ['textStyle']
* @example ['heading', 'paragraph', 'textStyle']
*/
types: string[];
}Usage Example:
import { Editor } from "@tiptap/core";
import { FontSize } from "@tiptap/extension-text-style/font-size";
import { TextStyle } from "@tiptap/extension-text-style/text-style";
const editor = new Editor({
extensions: [
TextStyle, // Required dependency
FontSize.configure({
types: ['textStyle', 'paragraph']
})
]
});Apply a specific font size to the current text selection.
/**
* Set the font size
* @param fontSize - CSS font-size value with units (px, em, rem, %, pt, etc.)
* @returns Command result indicating success/failure
*/
setFontSize(fontSize: string): Command;Usage Examples:
// Pixel units
editor.commands.setFontSize("16px");
editor.commands.setFontSize("24px");
// Em units (relative to parent)
editor.commands.setFontSize("1.5em");
editor.commands.setFontSize("0.8em");
// Rem units (relative to root)
editor.commands.setFontSize("1.2rem");
editor.commands.setFontSize("2rem");
// Percentage
editor.commands.setFontSize("120%");
editor.commands.setFontSize("75%");
// Point units (print)
editor.commands.setFontSize("12pt");
editor.commands.setFontSize("18pt");
// Keyword values
editor.commands.setFontSize("large");
editor.commands.setFontSize("x-small");
// Chain with other styling commands
editor.chain()
.setFontSize("18px")
.setFontFamily("Arial")
.setColor("#333")
.run();Remove font size styling from the current text selection.
/**
* Unset the font size, removing fontSize attribute and cleaning up empty textStyle marks
* @returns Command result indicating success/failure
*/
unsetFontSize(): Command;Usage Example:
// Remove font size and clean up empty styling
editor.commands.unsetFontSize();
// Chain removal with other operations
editor.chain()
.unsetFontSize()
.setColor("#000000")
.run();The FontSize extension includes straightforward HTML parsing:
font-size value directly from element.style.fontSizestyle="font-size: {value}" attributeThe FontSize extension augments TipTap's type system:
// Extends core Commands interface
declare module '@tiptap/core' {
interface Commands<ReturnType> {
fontSize: {
setFontSize: (fontSize: string) => ReturnType;
unsetFontSize: () => ReturnType;
}
}
}
// Extends TextStyleAttributes interface
declare module '@tiptap/extension-text-style' {
interface TextStyleAttributes {
fontSize?: string | null;
}
}import { FontSize } from "@tiptap/extension-text-style/font-size";
const fontSizeExtension = FontSize.configure({
types: ['textStyle']
});const fontSizeExtension = FontSize.configure({
types: ['textStyle', 'heading', 'paragraph']
});This allows font sizes to be applied directly to headings and paragraphs in addition to inline text styling.
// Pixels (most common for web)
editor.commands.setFontSize("12px"); // Small text
editor.commands.setFontSize("16px"); // Body text
editor.commands.setFontSize("24px"); // Large text
editor.commands.setFontSize("32px"); // Heading size
// Points (print-oriented)
editor.commands.setFontSize("10pt"); // Small print
editor.commands.setFontSize("12pt"); // Body text
editor.commands.setFontSize("18pt"); // Large text// Em units (relative to parent element)
editor.commands.setFontSize("0.8em"); // 80% of parent
editor.commands.setFontSize("1.2em"); // 120% of parent
editor.commands.setFontSize("1.5em"); // 150% of parent
// Rem units (relative to root element)
editor.commands.setFontSize("0.875rem"); // 14px if root is 16px
editor.commands.setFontSize("1.125rem"); // 18px if root is 16px
editor.commands.setFontSize("1.5rem"); // 24px if root is 16px
// Percentages
editor.commands.setFontSize("90%"); // 90% of parent
editor.commands.setFontSize("110%"); // 110% of parent
editor.commands.setFontSize("150%"); // 150% of parent// Absolute size keywords
editor.commands.setFontSize("xx-small");
editor.commands.setFontSize("x-small");
editor.commands.setFontSize("small");
editor.commands.setFontSize("medium"); // Default size
editor.commands.setFontSize("large");
editor.commands.setFontSize("x-large");
editor.commands.setFontSize("xx-large");
// Relative size keywords
editor.commands.setFontSize("smaller"); // Smaller than parent
editor.commands.setFontSize("larger"); // Larger than parentFor responsive designs, consider using relative units:
// Use rem for consistent scaling
editor.commands.setFontSize("1rem"); // Base size
editor.commands.setFontSize("1.25rem"); // Slightly larger
editor.commands.setFontSize("1.5rem"); // Medium heading
// Use em for contextual scaling
editor.commands.setFontSize("1.1em"); // Slightly larger than context
editor.commands.setFontSize("0.9em"); // Slightly smaller than contextInstall with Tessl CLI
npx tessl i tessl/npm-tiptap--extension-text-style