Browser zoom causes inaccurate caret positioning in contenteditable
OS: Any Any · Device: Desktop or Laptop Any · Browser: Firefox Latest · Keyboard: US
Open case →Scenario
When the browser is zoomed (or content is scaled via CSS transforms), caret position and text selection in contenteditable elements can become inaccurate. Clicking at a certain position places the caret elsewhere, and selection highlights may not match the visual selection.
When the browser is zoomed (or content is scaled via CSS transforms), caret position and text selection in contenteditable elements can become inaccurate. Clicking at a certain position places the caret elsewhere, and selection highlights may not match the visual selection.
transform: scale() is used, caret becomes invisiblecontenteditable="false" elements with zoomHelps Chrome with caret placement:
[contenteditable="true"] {
display: inline-block;
}
Prevents Firefox caret invisibility:
if (editableElement.innerHTML.trim() === '') {
editableElement.innerHTML = '<br>';
}
Around contenteditable="false" elements:
function insertZWS(element) {
const zws = document.createTextNode('\u200B');
element.parentNode.insertBefore(zws, element.nextSibling);
}
Use font-size or layout adjustments instead:
/* Instead of: */
.editor {
transform: scale(1.5);
}
/* Use: */
.editor {
font-size: 150%;
}
For custom UI calculations:
function getViewportAdjustedRect(element) {
const rect = element.getBoundingClientRect();
const viewport = window.visualViewport;
return {
top: rect.top - viewport.offsetTop,
left: rect.left - viewport.offsetLeft,
width: rect.width / viewport.scale,
height: rect.height / viewport.scale
};
}
Prevents dead-space clicking issues:
[contenteditable="true"] {
line-height: 1.5;
font-size: 16px;
}
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-0563-browser-zoom-caret-positioning | Any Any | Desktop or Laptop Any | Firefox Latest | US | draft |
Open a case to see the detailed description and its dedicated playground.
OS: Any Any · Device: Desktop or Laptop Any · Browser: Firefox Latest · Keyboard: US
Open case →Other scenarios that share similar tags or category.
Right-to-left (RTL) and mixed-direction text in contenteditable causes caret misalignment, scroll failures, and select-all behavior that differs from LTR and from spec.
When a range of text is selected inside a `contenteditable` element, clicking outside the element
When editing content inside an element with `position:relative`, the text caret (cursor) is completely invisible. Text can be typed and appears in the editor, but there's no visual feedback of where the insertion point is located.
On Chrome Mobile for Android, typing certain punctuation characters (commas, colons, semicolons, quotes, etc.) in the middle of a word causes the cursor to jump to the end of the word instead of staying at the insertion point.
When deleting the last character before a non-editable "pill" or tag element (contenteditable="false") in a contenteditable div in Chrome, the caret (cursor) jumps to the end of the entire contenteditable div instead of staying adjacent to the remaining content.
Have questions, suggestions, or want to share your experience? Join the discussion below.