4

What is the difference between div with the property display:inline-block and span with display:inline-block?

j08691
  • 204,283
  • 31
  • 260
  • 272
sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    Insight: http://stackoverflow.com/questions/1611065/span-vs-div-inline-block – calebds Jan 28 '12 at 19:38
  • This is helpful too https://stackoverflow.com/questions/183532/what-is-the-difference-between-html-tags-div-and-span – frederj Dec 04 '19 at 16:56

3 Answers3

15

There is two differences between div and span elements:

  • The div has display:block as default, while span has display:inline as default.
  • The 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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Just wanted to add some historical context to how there came to be span vs div

History of span:

On July 3, 1995, Benjamin C. W. Sittler proposes a generic text container tag for applying styles to certain blocks of text. The rendering is neutral except if used in conjunction of a stylesheet. There is a debate around versus about readability, meaning. Bert Bos is mentioning the extensibility nature of the element through the class attribute (with values such as city, person, date, etc.). Paul Prescod is worried that both elements will be abused. He is opposed to text mentioning that "any new element should be on an old one" and adding "If we create a tag with no semantics it can be used anywhere without ever being wrong. We must force authors to properly tag the semantics of their document. We must force editor vendors to make that choice explicit in their interfaces."

- Source (w3 wiki)

From the RFC draft that introduces span:

First, a generic container is needed to carry the LANG and BIDI attributes in cases where no other element is appropriate; the SPAN element is introduced for that purpose.

- Source (IETF Draft)

History of div:

CENTER was introduced by Netscape before they added support for the HTML 3.0 DIV element. It is retained in HTML 3.2 on account of its widespread deployment.

HTML 3.2 Spec

In a nutshell, both elements arose out of a need for a more generic container that didn't have attached semantics. Span was proposed as a more generic replacement for a <text> element to style text. Div was proposed as a generic way to divide pages and had the added benefit of replacing the <center> tag for center-aligning content. Div has always been a block element because of its history as a page divider. Span has always been an inline element because its original purpose was text styling and today div and span have both arrived at being generic elements with default block and inline display properties respectively.

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
0

The difference between < div > and < span > si that < span > doesn't have any default styling, where < div > has a paragraph break. Hence both tags are extremely similar, applying the css property display:inline-block will have a similar effect, but combined with the vertical-align can have a different effect.

Have a look at this link: http://www.brunildo.org/test/inline-block.html

  • 1
    A div does not have a break of any kind. You are confusing block level elements that occupy the full width of the browser causing other elements to drop beneath it. A div also does not have any styling. It is a generic container. – Rob Jan 28 '12 at 19:56
  • Well, have a look at the official W3 specifications: http://www.w3.org/TR/1999/REC-html401-19991224/struct/global.html#h-7.5.4 , find the following text: "Visual user agents generally place a line break before and after DIV elements, for instance...." – andrei.stoleru Jan 28 '12 at 20:03
  • 1
    It shows that your comment was not completely true "a div does have a line break". Or W3 is wrong? Anyway, our comments doesn't help answering the main question. – andrei.stoleru Jan 28 '12 at 20:44
  • Nothing in your link suggests a div is styled with a line break except its comments about "most user agents" put one in but that's not part of the spec and, in any case, I would believe is an error in that statement and I will look through the errata and speak to some I know in the W3C. – Rob Jan 28 '12 at 21:59