Scenario

Enter vs Shift+Enter behavior differs across browsers

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.

formatting
Scenario ID
scenario-enter-vs-shift-enter

Details

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.

Observed Behavior

Scenario 1: Pressing Enter

  • Chrome/Edge: Creates a new <div> or <p> element
  • Firefox: Creates a new <p> or <br> element
  • Safari: May create <div>, <p>, or <br> depending on context

Scenario 2: Pressing Shift+Enter

  • Chrome/Edge: Creates a <br> line break
  • Firefox: Creates a <br> line break
  • Safari: May create <br> or behave like Enter

Scenario 3: Enter in different contexts

  • Chrome/Edge: Behavior may differ in lists, blockquotes, etc.
  • Firefox: Similar context-dependent behavior
  • Safari: More inconsistent across contexts

Scenario 4: Empty paragraph/div creation

  • Chrome/Edge: May create empty elements that need cleanup
  • Firefox: Similar behavior
  • Safari: May create different empty structures

Impact

  • Inconsistent line break behavior
  • Unexpected DOM structure creation
  • Need to normalize DOM after Enter key
  • Difficulty implementing consistent editing behavior

Browser Comparison

  • Chrome/Edge: Generally creates <div> for Enter, <br> for Shift+Enter
  • Firefox: More likely to create <p> for Enter
  • Safari: Most inconsistent behavior

Workaround

Normalize 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);
  }
});

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-0105-shift-enter-creates-br Windows 11 Desktop or Laptop Any Chrome 120.0 US draft

Cases

Open a case to see the detailed description and its dedicated playground.

Related Scenarios

Other scenarios that share similar tags or category.

Tags: line-break, paragraph

Line break element type varies across browsers

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.

2 cases
Tags: enter

Nested list editing behavior is inconsistent

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.

3 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
Category: formatting

Blockquote editing behavior varies across browsers

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.

4 cases

Comments & Discussion

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