Reputation: 2394
<div style="display: inline"></div>
<span></span>
<div></div>
<span style="display:block></span>
<div style="display: inline-block"></div>
<span style="display: inline-block"></span>
If I write like this, what's the different between <div>
and <span>
Upvotes: 4
Views: 2910
Reputation: 723448
In terms of CSS, there is no difference. You can style any element regardless of its semantics however you like.
Likewise, in terms of HTML, how you style them with CSS does not affect their nature. A <div>
always accepts any flow content, which is typically represented as block-level elements in CSS, or phrasing content, which is typically represented as inline elements in CSS. Note however that there isn't a necessary correlation between the two concepts. A <span>
on the other hand can only have phrasing content.
In valid HTML, this means while a <div>
can contain a <span>
, a <span>
cannot contain a <div>
.
Both elements are similar in that they carry no semantics by themselves; they simply function as grouping elements for their contents, typically used for styling purposes.
See the HTML5 spec for <div>
and <span>
for more details.
Upvotes: 5
Reputation: 11506
div
<p>
span
From a rendering point of view,
<span> == <div style="display: inline">
and
<div> == <span style="display: block">
As for HTML syntax, however, a div
cannot be nested inside an inline
element, whereas a span
cannot contain block-level elements.
The mysterious "display: inline-block"
block vs inline vs inline-block
Below are a bunch of with different display: settings.
As you can see, inline-block
is a hybrid that:
For more information
Div's with display inline-block display weird (CSS)
Difference between DIV as-is and a SPAN with display:block
http://quirksmode.org/css/css2/display.html
Upvotes: 4
Reputation: 12961
The point is based on the HTML spec, span
tags are inline
elements and div
tags are a block
element, and these 2 can get changed using display
rule in CSS.
So if you change the display
there won't be any difference.
But in terms of the way these are usually used, you can consider div
to wrap the sections in the DOM and span
tags mostly are used to wrap texts and contents.
Check the HTML spec document out for more info.
Upvotes: 1