Scenario

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.

formatting
Scenario ID
scenario-line-break-element-type

Details

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.

Observed Behavior

Scenario 1: Pressing Enter creates

  • Chrome/Edge: <div> elements by default
  • Firefox: <p> elements by default
  • Safari: May create <div>, <p>, or <br> depending on context

Scenario 2: Empty line creation

  • Chrome/Edge: Creates empty <div><br></div> structure
  • Firefox: Creates empty <p><br></p> or just <p></p>
  • Safari: May create different structures

Scenario 3: Normalization

  • Chrome/Edge: Requires normalization to convert <div> to <p>
  • Firefox: May need normalization to ensure consistent structure
  • Safari: Requires most normalization work

Scenario 4: CSS styling impact

  • Chrome/Edge: <div> elements may have different default margins than <p>
  • Firefox: <p> elements have default margins
  • Safari: Styling varies based on element type

Impact

  • Inconsistent DOM structure across browsers
  • Need for normalization logic
  • CSS styling differences
  • Difficulty maintaining consistent appearance

Browser Comparison

  • Chrome/Edge: Uses <div> by default
  • Firefox: Uses <p> by default
  • Safari: Most inconsistent

Workaround

Normalize DOM structure:

function normalizeLineBreaks(element) {
  // Convert all <div> elements used for line breaks to <p>
  const divs = element.querySelectorAll('div');
  divs.forEach(div => {
    // Check if div is used as a paragraph (not a container)
    const hasBlockChildren = Array.from(div.children).some(
      child => ['DIV', 'P', 'UL', 'OL', 'BLOCKQUOTE'].includes(child.tagName)
    );
    
    if (!hasBlockChildren) {
      const p = document.createElement('p');
      while (div.firstChild) {
        p.appendChild(div.firstChild);
      }
      div.parentNode.replaceChild(p, div);
    }
  });
  
  // Ensure empty paragraphs have <br> for proper display
  const paragraphs = element.querySelectorAll('p');
  paragraphs.forEach(p => {
    if (!p.textContent.trim() && !p.querySelector('br')) {
      p.appendChild(document.createElement('br'));
    }
  });
}

// Normalize after input
element.addEventListener('input', () => {
  normalizeLineBreaks(element);
});

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-0104-enter-creates-div-chrome Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0168-enter-creates-p-firefox Windows 11 Desktop or Laptop Any Firefox 120.0 US draft

Browser compatibility

This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.

Confirmed
Draft
No case documented

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

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.

1 case
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
Category: formatting

Browser auto-formatting interferes with contenteditable editing

Browsers may automatically format text in contenteditable elements, such as converting URLs to links, capitalizing text, formatting numbers, or applying other transformations. This auto-formatting can interfere with editing, cause cursor positioning issues, and create unwanted markup or style changes.

0 cases

Comments & Discussion

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