Case ce-0023-double-line-break-chrome · Scenario scenario-double-line-break

Pressing Enter inserts two line breaks in contenteditable

OS: macOSLinuxWindows Any Device: Desktop Any Browser: ChromeEdgeOpera Latest Keyboard: US Status: confirmed
line-break enter chromium double-br

Phenomenon

When a user presses the Enter key in a contenteditable container, Chromium-based browsers (Chrome, Edge) sometimes insert two <br> elements instead of one. This behavior typically occurs when the browser attempts to ensure the new line is “visible” or when it mismanages the “default paragraph separator”. This leads to a “double-spacing” effect that is difficult to manage in custom editors.

Reproduction example

  1. Create a contenteditable div with no initial content or a single line of text.
  2. Type some text (e.g., “Hello”).
  3. Press the Enter key.
  4. Inspect the DOM. In many cases, you will see Hello<br><br> if the editor is in a state that forces <br> usage.

Observed behavior

  • Double
    Insertion
    : A single keydown for Enter results in two <br> tags being appended.
  • Mix of Div/Br: Depending on the defaultParagraphSeparator setting, Chrome might wrap lines in <div> but still insert a <br> inside or between them, leading to inconsistent vertical spacing.
  • Platform Specificity: This is particularly prevalent on macOS and Linux, where the default system line-ending expectations might conflict with Chrome’s internal DOM conversion logic.

Expected behavior

  • Pressing Enter should insert exactly one logical line break.
  • If document.execCommand('defaultParagraphSeparator', false, 'p') or 'div' is set, it should consistently wrap the new block without stray <br> elements causing extra height.
  • The visual result should match standard word processor behavior (single spacing unless specifically configured otherwise).

Solution and Workarounds

  1. Manual Enter Handling:
    • Intercept the keydown event for Enter (keyCode 13).
    • Use event.preventDefault() to stop the browser’s default insertion.
    • Programmatically insert a single <br> using document.execCommand('insertHTML', false, '<br>') or by manually manipulating the text nodes and selection.
  2. Default Paragraph Separator:
    • Set document.execCommand('defaultParagraphSeparator', false, 'p') globally when the editor initializes. This encourages Chrome to use <p> tags for new lines, which is generally more stable than raw <br> tags.
  3. Trailing Br Management:
    • Some editors maintain a “padding” <br> at the end of every block to ensure the height is rendered correctly even if the block is empty. Ensure your logic accounts for this “magic” <br> so it doesn’t get duplicated.
Before
Hello
First line
After Enter (Bug)
Hello

Single Enter inserts two &lt;br&gt; elements (Chromium macOS/Linux)
vs
✅ Expected
Hello
Expected: Single Enter inserts only one line break

Browser compatibility matrix

This matrix shows which browser and OS combinations have documented cases for this scenario. The current case is highlighted. Click on a cell to view other cases.

Browser macOS macOSLinuxWindows
ChromeEdgeOpera
Safari
Current case
Confirmed
Draft
No case documented

All variants (detailed table)

Complete list of all cases for this scenario with full environment details.

Case OS Device Browser Keyboard Status
ce-0003-double-line-break macOS 14.0 Laptop MacBook Pro Safari 17.0 US draft
ce-0023-double-line-break-chrome macOSLinuxWindows Any Desktop Any ChromeEdgeOpera Latest US confirmed

Playground for this case

Use the reported environment as a reference and record what happens in your environment while interacting with the editable area.

Reported environment
OS: macOSLinuxWindows Any
Device: Desktop Any
Browser: ChromeEdgeOpera Latest
Keyboard: US
Your environment
Sample HTML:
Event log
Use this log together with the case description when filing or updating an issue.
0 events
Interact with the editable area to see events here.

Comments & Discussion

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