What is the difference between div
with the property display:inline-block
and span
with display:inline-block
?
There is two differences between div
and span
elements:
div
has display:block
as default, while span has display:inline
as default.div
is a block element, and can contain block elements and inline elements, while span
is an inline element and can only contain other inline elements.Once you have applied the display:inline-block
they behave the same.
When the HTML code is parsed, the style is not considered. While you can change the display
style to make the elements behave the same, you still have to follow the rules in the HTML code.
This for example is valid:
<div>
<div>
<span></span>
</div>
<span></span>
</div>
This for example is invalid:
<span>
<div>
<span></span>
</div>
<div></div>
</span>
The browser will try to rearrange the elements in the invalid code, which usually means that it moves the div
elements outside the span
elements. As the HTML specification (prior to version 5) only told how correct code should be handled, each browser has its own way of dealing with incorrect code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With