Shift+Enter creates br element for line break
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
The behavior of Enter and Shift+Enter keys in contenteditable elements varies across browsers. Enter may create a new paragraph, line break, or div, while Shift+Enter may create a line break or behave differently. The resulting DOM structure also varies.
The behavior of Enter and Shift+Enter keys in contenteditable elements varies across browsers. Enter may create a new paragraph, line break, or div, while Shift+Enter may create a line break or behave differently. The resulting DOM structure also varies.
<div> or <p> element<p> or <br> element<div>, <p>, or <br> depending on context<br> line break<br> line break<br> or behave like Enter<div> for Enter, <br> for Shift+Enter<p> for EnterNormalize Enter key behavior:
element.addEventListener('beforeinput', (e) => {
if (e.inputType === 'insertParagraph') {
e.preventDefault();
const selection = window.getSelection();
if (selection.rangeCount === 0) return;
const range = selection.getRangeAt(0);
const isShiftEnter = e.getModifierState?.('Shift');
if (isShiftEnter) {
// Insert line break
const br = document.createElement('br');
range.deleteContents();
range.insertNode(br);
range.setStartAfter(br);
range.collapse(true);
} else {
// Insert new paragraph
const p = document.createElement('p');
const br = document.createElement('br');
p.appendChild(br);
range.deleteContents();
range.insertNode(p);
range.setStartBefore(br);
range.collapse(true);
}
selection.removeAllRanges();
selection.addRange(range);
}
});
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-0105-shift-enter-creates-br | Windows 11 | Desktop or Laptop Any | Chrome 120.0 | US | draft |
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 →Other scenarios that share similar tags or category.
When creating line breaks in contenteditable elements, browsers use different HTML elements: <br>, <p>, or <div>. This inconsistency makes it difficult to predict and normalize the DOM structure, especially when working with rich text editors.
In Internet Explorer 11, typing after selecting a line of text in a contenteditable div can unexpectedly delete the BR tag, causing lines to merge.
When editing nested lists (lists within list items), the behavior of Enter, Backspace, Delete, and Tab keys varies significantly across browsers. Creating, editing, and deleting nested list items can result in unexpected DOM structures or lost formatting.
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.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.