Deleting image leaves empty wrapper elements
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
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.
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.
<img> tags or wrapper divsImplement 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);
}
}
});
Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.
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 |
This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.
| Browser | Windows |
|---|---|
| Chrome | |
| Firefox |
Open a case to see the detailed description and its dedicated playground.
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Firefox 120.0 · Keyboard: US
Open case →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Other scenarios that share similar tags or category.
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.
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.
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.
After deleting an empty or inline element (e.g. span, b) inside contenteditable, typing causes the browser to recreate the deleted element, leading to unpredictable DOM and editor state.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.