Enter key creates div elements instead of paragraphs in Chrome
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
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.
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.
<div> elements by default<p> elements by default<div>, <p>, or <br> depending on context<div><br></div> structure<p><br></p> or just <p></p><div> to <p><div> elements may have different default margins than <p><p> elements have default margins<div> by default<p> by defaultNormalize 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);
});
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-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 |
This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.
| Browser | Windows |
|---|---|
| Chrome | |
| Firefox |
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 →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Firefox 120.0 · Keyboard: US
Open case →Other scenarios that share similar tags or category.
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.
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.
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.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.