Understanding Display Behavior for Replaced vs. Non-Replaced Elements in CSS
In CSS, elements are classified as replaced or non-replaced. Replaced elements have their content defined outside CSS (e.g., <img>, <video>, <iframe>, <input>), while non-replaced elements have content defined in the DOM and styled with CSS. The display property affects them differently.
Default display values differ: most non-replaced elements default to block or inline, while replaced elements often behave like inline-block.
Replaced elements respect width and height more strictly; setting display: block or inline-block affects their box model predictably.
Non-replaced elements can contain children; replaced elements generally cannot, so display: flex or grid on a replaced element may have limited effect.
Vertical alignment behaves differently: replaced elements align to the baseline differently than inline non-replaced elements, especially when mixed in text.
In this example, the div is a non-replaced element styled as inline-block, allowing it to contain content. The image is a replaced element, and its dimensions are handled by the browser, respecting width/height and display rules.
Understand whether an element is replaced or non-replaced to predict how display values will behave.
Use inline-block or block for replaced elements when you need predictable sizing.
Use flex or grid only on non-replaced containers to align children.
Remember that some CSS properties have limited effect on replaced elements (e.g., padding affects layout differently than on non-replaced elements).