Phenomenon
When typing multiple consecutive spaces in a contenteditable element, all browsers collapse them into a single space by default (following HTML whitespace rules). This differs from native text inputs and can be unexpected for users.
Reproduction example
- Focus a contenteditable element
- Type multiple spaces (e.g., press Space 5 times)
- Observe the DOM
Observed behavior
- All consecutive spaces are collapsed to a single space in the DOM
- This follows HTML whitespace collapsing rules
- Visual appearance shows only one space
- Different from
<input>and<textarea>behavior
Expected behavior
- Multiple spaces should be preserved (if needed)
- Or behavior should be clearly documented
- Users should understand why spaces are collapsed
Browser Comparison
- All browsers: Collapse consecutive spaces (HTML standard)
- Workaround: Use
white-space: pre-wrapCSS or entities
Notes and possible direction for workarounds
- Use CSS
white-space: pre-wrapto preserve spaces - Intercept space insertion and use
for multiple spaces - Document this behavior for users
- Consider if preserving spaces is necessary for the use case