Scenario

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.

formatting
Scenario ID
scenario-background-color-change

Details

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.

Observed Behavior

Scenario 1: Applying background color to selected text

  • Chrome/Edge: Applies background-color as inline style
  • Firefox: Similar behavior but may not persist
  • Safari: May apply background differently

Scenario 2: Typing after applying background color

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

Scenario 3: Background color and text selection

  • Chrome/Edge: Background may interfere with selection visibility
  • Firefox: Similar issues
  • Safari: Selection behavior may differ

Scenario 4: Removing background color

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

Impact

  • Inconsistent background color application
  • Background formatting may not persist
  • Selection visibility issues
  • Empty style attributes in DOM

Browser Comparison

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

Workaround

Implement custom background color handling:

element.addEventListener('beforeinput', (e) => {
  if (e.inputType === 'formatBackColor') {
    e.preventDefault();
    const selection = window.getSelection();
    if (selection.rangeCount === 0) return;
    
    const range = selection.getRangeAt(0);
    const color = e.data || prompt('Enter background color:');
    
    if (color) {
      if (range.collapsed) {
        // Set background for future typing
        const span = document.createElement('span');
        span.style.backgroundColor = color;
        range.insertNode(span);
        range.setStart(span, 0);
        range.collapse(true);
      } else {
        // Apply background to selected text
        const span = document.createElement('span');
        span.style.backgroundColor = color;
        try {
          range.surroundContents(span);
        } catch (e) {
          const contents = range.extractContents();
          span.appendChild(contents);
          range.insertNode(span);
        }
      }
      
      selection.removeAllRanges();
      selection.addRange(range);
    }
  }
});

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-0121-background-color-selection-issue Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0146-background-color-persistence Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0167-background-color-format-variation Windows 11 Desktop or Laptop Any Chrome 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: color, css

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

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.

3 cases
Tags: 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
Category: formatting

Blockquote editing behavior varies across browsers

Editing text within blockquote elements in contenteditable behaves inconsistently across browsers. Pressing Enter, applying formatting, or pasting content may break the blockquote structure, create nested blockquotes, or behave unexpectedly.

4 cases
Category: formatting

Browser auto-formatting interferes with contenteditable editing

Browsers may automatically format text in contenteditable elements, such as converting URLs to links, capitalizing text, formatting numbers, or applying other transformations. This auto-formatting can interfere with editing, cause cursor positioning issues, and create unwanted markup or style changes.

0 cases

Comments & Discussion

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