button
and span
behave as inline elements, whereas div
behaves as a block level element.
1.)
In the case of
<span>a</span><span>b</span>
,
The letter a
will be displayed on the first line and remain in the normal document flow, while b
will also stay on the same line since they are both enclosed within the span
which is an inline element. Therefore, b
follows the horizontal flow and appears after a
on the same line.
This results in the output:
ab
2.)
For
<div>a</div><span>b</span>
and
<div>a</div><div>b</div>
,
The letter a
will sit on the first line and the normal document flow moves to the next line due to the vertical stacking nature of the first div
element. As a result, b
follows the vertical flow and appears on a new line.
This leads to the following outputs:
a
b
3.)
For
<span>a</span><div>b</div>
,
The letter a
will be on the first line and the normal document flow goes to the next line because of the vertical stacking nature of the adjacent div
element. Consequently, b
follows the vertical flow and sits on a new line.
Resulting in the output:
a
b
*)
Bonus
:
Statement 1
: Block level elements start on a new line.
Statement 2
: Block level elements occupy the full width of their parent element regardless of content size.
Statement 3
: By default, block level elements take up all available space in the inline direction. Even if given a width, they will stack vertically one below the other.
All these statements contribute to maintaining the vertical stacking behavior of block level elements.
This logic forms the foundation behind the scenes...