Scenario

Font family changes behave inconsistently

Changing font family in contenteditable elements behaves inconsistently across browsers. The font-family CSS property may be applied inline, as a style attribute, or may not be applied at all. The behavior also varies when editing text after applying a font.

formatting
Scenario ID
scenario-font-family-change

Details

Changing font family in contenteditable elements behaves inconsistently across browsers. The font-family CSS property may be applied inline, as a style attribute, or may not be applied at all. The behavior also varies when editing text after applying a font.

Observed Behavior

Scenario 1: Applying font to selected text

  • Chrome/Edge: Applies font-family as inline style, may persist when typing
  • Firefox: Similar behavior but may not persist
  • Safari: May apply font differently or not persist

Scenario 2: Typing after applying font

  • Chrome/Edge: New text may inherit font or use default
  • Firefox: Font may not persist for new text
  • Safari: Behavior inconsistent

Scenario 3: Removing font formatting

  • Chrome/Edge: May leave empty style attributes
  • Firefox: Similar behavior
  • Safari: May handle differently

Scenario 4: Font inheritance

  • Chrome/Edge: Font may be inherited from parent elements
  • Firefox: Similar inheritance behavior
  • Safari: Inheritance may work differently

Impact

  • Inconsistent font application
  • Font formatting may not persist
  • Empty style attributes left in DOM
  • Difficulty maintaining consistent typography

Browser Comparison

  • Chrome/Edge: Generally better font handling
  • Firefox: Font persistence may be less reliable
  • Safari: Most inconsistent behavior

Workaround

Implement custom font handling:

element.addEventListener('beforeinput', (e) => {
  if (e.inputType === 'formatFontName') {
    e.preventDefault();
    const selection = window.getSelection();
    if (selection.rangeCount === 0) return;
    
    const range = selection.getRangeAt(0);
    const fontName = e.data || prompt('Enter font name:');
    
    if (fontName) {
      // Apply font to selection
      if (range.collapsed) {
        // Set font for future typing
        document.execCommand('fontName', false, fontName);
      } else {
        // Apply font to selected text
        const span = document.createElement('span');
        span.style.fontFamily = fontName;
        try {
          range.surroundContents(span);
        } catch (e) {
          // If surroundContents fails, extract and wrap
          const contents = range.extractContents();
          span.appendChild(contents);
          range.insertNode(span);
        }
      }
    }
  }
});

References

Scenario flow

Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.

React Flow mini map

Variants

Each row is a concrete case for this scenario, with a dedicated document and playground.

Case OS Device Browser Keyboard Status
ce-0106-font-family-persistence-safari macOS 14.0 Desktop or Laptop Any Safari 17.0 US draft
ce-0135-font-family-empty-style Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0155-font-family-inheritance-issue Windows 11 Desktop or Laptop Any Firefox 120.0 US draft

Browser compatibility

This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.

Confirmed
Draft
No case documented

Cases

Open a case to see the detailed description and its dedicated playground.

Related Scenarios

Other scenarios that share similar tags or category.

Tags: font, typography, css

Font size changes behave inconsistently

Changing font size in contenteditable elements behaves inconsistently across browsers. Font sizes may be applied as inline styles, as font tags, or may not persist when typing new text. The unit (px, em, rem) handling also varies.

3 cases
Tags: css, formatting

Text color changes behave inconsistently

Changing text color in contenteditable elements behaves inconsistently across browsers. Colors may be applied as inline styles, as font tags, or may not persist when typing. The color format (hex, rgb, named colors) handling also varies.

2 cases
Tags: css

Background color changes behave inconsistently

Changing background color (highlighting) in contenteditable elements behaves inconsistently across browsers. Background colors may be applied as inline styles, may not persist when typing, or may interfere with text selection. The behavior differs from text color changes.

3 cases
Tags: formatting

Code block editing behavior varies across browsers

Editing text within code blocks (<pre><code>) in contenteditable elements behaves inconsistently across browsers. Line breaks, indentation, whitespace preservation, and formatting may be handled differently, making it difficult to maintain code formatting.

4 cases
Tags: formatting

execCommand is deprecated but still widely used for formatting

The document.execCommand() API, which is commonly used to apply formatting (bold, italic, etc.) in contenteditable regions, has been deprecated. However, there is no complete replacement, and many implementations still rely on it. This creates uncertainty about future browser support.

1 case

Comments & Discussion

Have questions, suggestions, or want to share your experience? Join the discussion below.