Scenario

Image deletion behavior varies across browsers

Deleting images from contenteditable elements behaves differently across browsers. Some browsers delete the image cleanly, while others may leave empty elements, break the DOM structure, or require multiple delete operations.

formatting
Scenario ID
scenario-image-deletion

Details

Deleting images from contenteditable elements behaves differently across browsers. Some browsers delete the image cleanly, while others may leave empty elements, break the DOM structure, or require multiple delete operations.

Observed Behavior

Scenario 1: Selecting and deleting an image

  • Chrome/Edge: Image is deleted, but may leave empty parent elements
  • Firefox: May delete image or require multiple operations
  • Safari: Behavior varies, may break DOM structure

Scenario 2: Backspace on selected image

  • Chrome/Edge: Deletes image, cursor position may be unexpected
  • Firefox: May delete image or move cursor unexpectedly
  • Safari: Behavior inconsistent

Scenario 3: Delete key on selected image

  • Chrome/Edge: Deletes image forward
  • Firefox: Similar behavior
  • Safari: May behave differently

Scenario 4: Empty image elements

  • Chrome/Edge: May create empty <img> tags or wrapper divs
  • Firefox: Similar issues
  • Safari: May create different empty structures

Impact

  • Inconsistent deletion experience
  • Risk of creating malformed HTML
  • Empty elements left in DOM
  • Cursor position issues after deletion

Browser Comparison

  • Chrome/Edge: Generally deletes images but may leave empty elements
  • Firefox: More likely to leave empty structures
  • Safari: Most inconsistent behavior

Workaround

Implement custom image deletion handling:

element.addEventListener('beforeinput', (e) => {
  if (e.inputType === 'deleteContentBackward' || e.inputType === 'deleteContentForward') {
    const selection = window.getSelection();
    if (selection.rangeCount === 0) return;
    
    const range = selection.getRangeAt(0);
    const img = range.startContainer.nodeType === Node.ELEMENT_NODE 
      ? range.startContainer.querySelector('img')
      : range.startContainer.parentElement?.querySelector('img');
    
    if (img && (range.intersectsNode(img) || selection.containsNode(img, true))) {
      e.preventDefault();
      
      // Clean up image and any empty wrapper elements
      const parent = img.parentElement;
      img.remove();
      
      // Remove empty wrapper divs
      if (parent && parent.tagName !== 'BODY' && 
          (!parent.textContent || parent.textContent.trim() === '') &&
          parent.children.length === 0) {
        parent.remove();
      }
      
      // Set cursor position after deletion
      const newRange = document.createRange();
      if (parent && parent.nextSibling) {
        newRange.setStartBefore(parent.nextSibling);
      } else if (parent && parent.previousSibling) {
        newRange.setStartAfter(parent.previousSibling);
      } else {
        newRange.setStart(parent || element, 0);
      }
      newRange.collapse(true);
      selection.removeAllRanges();
      selection.addRange(newRange);
    }
  }
});

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-0118-image-deletion-leaves-empty Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0125-image-deletion-cursor-position Windows 11 Desktop or Laptop Any Firefox 120.0 US draft
ce-0165-image-selection-deletion 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: deletion, backspace, delete

List item deletion behavior varies across browsers

When pressing Backspace or Delete at the beginning or end of a list item, the behavior varies significantly across browsers. Some browsers delete the list item and merge with adjacent content, while others may delete the entire list or create unexpected DOM structures.

3 cases
Tags: image

Image insertion behavior varies across browsers

When inserting images into contenteditable elements, the behavior varies significantly across browsers. Images may be inserted as <img> tags, as base64 data URLs, or may not be supported at all. The size, positioning, and editing behavior also differs.

2 cases
Tags: image

Image resizing in contenteditable is limited or inconsistent

Resizing images within contenteditable elements is limited or behaves inconsistently across browsers. Some browsers support native resize handles, while others require manual implementation. The resize behavior may also affect the DOM structure unexpectedly.

2 cases
Category: formatting

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

Comments & Discussion

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