SPAN与DIV(inline-block)的区别

145

使用<div style="display:inline-block">布局网页的原因是什么,与使用<span>相比有何区别?

我可以在标签内嵌套内容吗?哪些是有效的,哪些是无效的?

使用这个方法来创建3x2表格布局是可以的吗?

<div>
   <span> content1(divs,p, spans, etc) </span>
   <span> content2(divs,p, spans, etc) </span>
   <span> content3(divs,p, spans, etc) </span>
</div>
<div>
   <span> content4(divs,p, spans, etc) </span>
   <span> content5(divs,p, spans, etc) </span>
   <span> content6(divs,p, spans, etc) </span>
</div>

19
如果您想要一个有效的xhtml文档,那么您不能将块级元素放在内联元素中。 - moorej
1
HTML元素的维基百科页面:http://en.wikipedia.org/wiki/HTML_element - moorej
6个回答

190

根据HTML规范, <span>是一个内联元素,<div>是一个块级元素。现在可以使用display CSS属性来更改它们,但有一个问题:从HTML验证的角度来看,您不能将块级元素放入内联元素中,因此:

<p>...<div>foo</div>...</p>

即使将<div>更改为inlineinline-block,也不是严格有效的。因此,如果你的元素是inlineinline-block,请使用<span>。如果它是块级元素,请使用<div>

1
我倾向于认同inline-blockinline的关系比与block更密切。 - Bob Aman
11
原问题询问什么是有效的,对于验证而言,<span><div>确实不同,因为<span>是一个内联元素(例如在<p>中有效),而<div>是一个块级元素(在<p>中无效)。 - Brian Campbell
1
@cletus 实际上,在所有(前五个)浏览器中,渲染方面不会有任何区别,对吧? - Pacerier
10
@cletus,“<p>”不是块级元素吗? - Aris
10
<p>是一个块级元素,"不能包含块级元素"(链接),所以尽管示例无效,但不是因为<p>是内联元素。 - Pero P.
显示剩余2条评论

22

如果你想要一个有效的xhtml文档,那么就不能将div放在段落中。

此外,具有display: inline-block属性的div与span有所不同。span默认是内联元素,您无法设置宽度、高度和其他与块相关的属性。另一方面,具有inline-block属性的元素仍将“流动”与任何周围的文本,但您可以设置诸如宽度、高度等属性。具有display:block属性的span将不会像inline-block元素一样流动,而会创建一个回车并具有默认边距。

请注意,不是所有浏览器都支持inline-block。例如,在Firefox 2及更早版本中,必须使用:

display: -moz-inline-stack;

在Firefox 3中,显示效果略有不同于内联块元素。

这里有一篇很好的文章(点击这里),介绍了如何创建跨浏览器的内联块元素。


-moz-inline-block 不等同于 inline-block。 - moorej
如果你想让它在FF3中更像是内联块显示,你应该同时使用inline-stack。 - moorej
+1 非常有趣的链接。有时候,inline-block 可以解决许多问题。 - Tom

5
  1. 行内块元素是介于将元素的显示设置为inline或block之间的一种状态。 它像display:inline一样保持元素在文档的行内流中,但您可以像使用display:block一样操作元素的框属性(宽度、高度和垂直边距)。

  2. 我们不能在行内元素中使用块级元素。这是无效的,并且没有理由这样做。


3
我知道这个问题很老了,但为什么不使用所有的DIV而不是SPAN呢?这样一切都可以愉快地一起播放。
例子:
<div> 
   <div> content1(divs,p, spans, etc) </div> 
   <div> content2(divs,p, spans, etc) </div> 
   <div> content3(divs,p, spans, etc) </div> 
</div> 
<div> 
   <div> content4(divs,p, spans, etc) </div> 
   <div> content5(divs,p, spans, etc) </div> 
   <div> content6(divs,p, spans, etc) </div> 
</div>

1
我认为目标是尽可能保持所有内容简洁和语义化。因此,如果您有一个标题并且想要一个内部包装器div-似乎使用header {}和header span {}比使用header {}和.inner {}更具语义。然而...如果您使用.inner,则可以多次使用它-spans很可能必须独立样式化。底线-您希望尽可能少地使用标记-因此人们正在尝试找出避免div> div> div> div> div等的方法。 - sheriffderek

3
正如其他人所回答的那样,div是一个“块级元素”(现在重新定义为流内容),而span是一个“内联元素”(短语内容)。是的,您可以更改这些元素的默认显示方式,但是“流动”与“块”以及“短语”与“内联”之间存在差异。
被归类为流内容的元素只能在期望流内容的位置使用,而被归类为短语内容的元素可以在期望短语内容的任何位置使用。由于所有短语内容都是流内容,因此短语元素也可以在期望流内容的任何位置使用。 规范提供了更详细的信息
所有短语元素(例如strongem只能包含其他短语元素:例如,您不能将table放在中。大多数流内容,例如divli,可以包含所有类型的流内容(以及短语内容),但有一些例外情况:ppreth是非短语流内容(“块级元素”)的示例,它们只能包含短语内容(“内联元素”)。当然,还有正常的元素限制,例如dltable只允许包含某些元素。
虽然divp都是非短语流内容,但div可以包含其他流内容子元素(包括更多的divp),而p只能包含短语内容子元素。这意味着您不能将div放在p中,即使两者都是非短语流元素。
现在这里是关键。这些语义规范与元素的显示方式无关。因此,如果您在CSS中使用span {display: block;}div {display: inline;},即使您在span中放置了一个div,您也会收到验证错误。

内联块元素嵌套内联元素,以及内联元素内嵌块级元素的情况怎么处理呢? - user764754
只要您遵循规范,就可以随意为任何元素设置样式,而且仍然有效。 (inline-block 是 CSS 样式,不是元素或内容模型的类型。) - chharvey

3
我认为了解内联元素(例如span)和块级元素(例如div)之间的基本区别将有助于您理解为什么“display: inline-block”如此有用。
问题:内联元素(例如span、a、button、input等)仅在水平方向上(margin-left和margin-right)具有“margin”,而在垂直方向上不起作用。垂直间距仅适用于块级元素(或如果设置了"display:block")。
解决方案:只有通过"display:inline-block"才能同时考虑垂直距离(上下)。原因是:内联元素Span在外部表现得像块级元素,但在内部表现得像内联元素。
以下是示例代码:

 /* Inlineelement */

        div,
        span {
            margin: 30px;
        }

        span {
            outline: firebrick dotted medium;
            background-color: antiquewhite;
        }

        span.mitDisplayBlock {
            background: #a2a2a2;
            display: block;
            width: 200px;
            height: 200px;
        }

        span.beispielMargin {
            margin: 20px;
        }

        span.beispielMarginDisplayInlineBlock {
            display: inline-block;
        }

        span.beispielMarginDisplayInline {
            display: inline;
        }

        span.beispielMarginDisplayBlock {
            display: block;
        }

        /* Blockelement */

        div {
            outline: orange dotted medium;
            background-color: deepskyblue;
        }

        .paddingDiv {
            padding: 20px;
            background-color: blanchedalmond;
        }

        .marginDivWrapper {
            background-color: aliceblue;

        }

        .marginDiv {
            margin: 20px;
            background-color: blanchedalmond;
        }
    </style>
    <style>
        /* Nur für das w3school Bild */

        #w3_DIV_1 {
            bottom: 0px;
            box-sizing: border-box;
            height: 391px;
            left: 0px;
            position: relative;
            right: 0px;
            text-size-adjust: 100%;
            top: 0px;
            width: 913.984px;
            perspective-origin: 456.984px 195.5px;
            transform-origin: 456.984px 195.5px;
            background: rgb(241, 241, 241) none repeat scroll 0% 0% / auto padding-box border-box;
            border: 2px dashed rgb(187, 187, 187);
            font: normal normal 400 normal 15px / 22.5px Lato, sans-serif;
            padding: 45px;
            transition: all 0.25s ease-in-out 0s;
        }

        /*#w3_DIV_1*/

        #w3_DIV_1:before {
            bottom: 349.047px;
            box-sizing: border-box;
            content: '"Margin"';
            display: block;
            height: 31px;
            left: 0px;
            position: absolute;
            right: 0px;
            text-align: center;
            text-size-adjust: 100%;
            top: 6.95312px;
            width: 909.984px;
            perspective-origin: 454.984px 15.5px;
            transform-origin: 454.984px 15.5px;
            font: normal normal 400 normal 21px / 31.5px Lato, sans-serif;
        }

        /*#w3_DIV_1:before*/

        #w3_DIV_2 {
            bottom: 0px;
            box-sizing: border-box;
            color: black;
            height: 297px;
            left: 0px;
            position: relative;
            right: 0px;
            text-decoration: none solid rgb(255, 255, 255);
            text-size-adjust: 100%;
            top: 0px;
            width: 819.984px;
            column-rule-color: rgb(255, 255, 255);
            perspective-origin: 409.984px 148.5px;
            transform-origin: 409.984px 148.5px;
            caret-color: rgb(255, 255, 255);
            background: rgb(76, 175, 80) none repeat scroll 0% 0% / auto padding-box border-box;
            border: 0px none rgb(255, 255, 255);
            font: normal normal 400 normal 15px / 22.5px Lato, sans-serif;
            outline: rgb(255, 255, 255) none 0px;
            padding: 45px;
        }

        /*#w3_DIV_2*/

        #w3_DIV_2:before {
            bottom: 258.578px;
            box-sizing: border-box;
            content: '"Border"';
            display: block;
            height: 31px;
            left: 0px;
            position: absolute;
            right: 0px;
            text-align: center;
            text-size-adjust: 100%;
            top: 7.42188px;
            width: 819.984px;
            perspective-origin: 409.984px 15.5px;
            transform-origin: 409.984px 15.5px;
            font: normal normal 400 normal 21px / 31.5px Lato, sans-serif;
        }

        /*#w3_DIV_2:before*/

        #w3_DIV_3 {
            bottom: 0px;
            box-sizing: border-box;
            height: 207px;
            left: 0px;
            position: relative;
            right: 0px;
            text-size-adjust: 100%;
            top: 0px;
            width: 729.984px;
            perspective-origin: 364.984px 103.5px;
            transform-origin: 364.984px 103.5px;
            background: rgb(241, 241, 241) none repeat scroll 0% 0% / auto padding-box border-box;
            font: normal normal 400 normal 15px / 22.5px Lato, sans-serif;
            padding: 45px;
        }

        /*#w3_DIV_3*/

        #w3_DIV_3:before {
            bottom: 168.344px;
            box-sizing: border-box;
            content: '"Padding"';
            display: block;
            height: 31px;
            left: 3.64062px;
            position: absolute;
            right: -3.64062px;
            text-align: center;
            text-size-adjust: 100%;
            top: 7.65625px;
            width: 729.984px;
            perspective-origin: 364.984px 15.5px;
            transform-origin: 364.984px 15.5px;
            font: normal normal 400 normal 21px / 31.5px Lato, sans-serif;
        }

        /*#w3_DIV_3:before*/

        #w3_DIV_4 {
            bottom: 0px;
            box-sizing: border-box;
            height: 117px;
            left: 0px;
            position: relative;
            right: 0px;
            text-size-adjust: 100%;
            top: 0px;
            width: 639.984px;
            perspective-origin: 319.984px 58.5px;
            transform-origin: 319.984px 58.5px;
            background: rgb(191, 201, 101) none repeat scroll 0% 0% / auto padding-box border-box;
            border: 2px dashed rgb(187, 187, 187);
            font: normal normal 400 normal 15px / 22.5px Lato, sans-serif;
            padding: 20px;
        }

        /*#w3_DIV_4*/

        #w3_DIV_4:before {
            box-sizing: border-box;
            content: '"Content"';
            display: block;
            height: 73px;
            text-align: center;
            text-size-adjust: 100%;
            width: 595.984px;
            perspective-origin: 297.984px 36.5px;
            transform-origin: 297.984px 36.5px;
            font: normal normal 400 normal 21px / 73.5px Lato, sans-serif;
        }

        /*#w3_DIV_4:before*/
   <h1> The Box model - content, padding, border, margin</h1>
    <h2> Inline element - span</h2>
    <span>Info: A span element can not have height and width (not without "display: block"), which means it takes the fixed inline size </span>

    <span class="beispielMargin">
        <b>Problem:</b> inline elements (eg span, a, button, input etc.) take "margin" only vertically (margin-left and margin-right)
        on, not horizontal. Vertical spacing works only on block elements (or if display: block is set) </span>

    <span class="beispielMarginDisplayInlineBlock">
        <b>Solution</b> Only through
        <b> "display: inline-block" </ b> will also take the vertical distance (top and bottom). Reason: Inline element Span,
        behaves now like a block element to the outside, but like an inline element inside</span>

    <span class="beispielMarginDisplayInline">Example: here "display: inline". See the margin with Inspector!</span>

    <span class="beispielMarginDisplayBlock">Example: here "display: block". See the margin with Inspector!</span>

    <span class="beispielMarginDisplayInlineBlock">Example: here "display: inline-block". See the margin with Inspector! </span>

    <span class="mitDisplayBlock">Only with the "Display" -property and "block" -Value in addition, a width and height can be assigned. "span" is then like
        a "div" block element.  </span>

    <h2>Inline-Element - Div</h2>
    <div> A div automatically takes "display: block." </ div>
    <div class = "paddingDiv"> Padding is for padding </ div>

    <div class="marginDivWrapper">
Wrapper encapsulates the example "marginDiv" to clarify the "margin" (distance from inner element "marginDiv" to the text)
        of the outer element "marginDivWrapper". Here 20px;)
        
    <div class = "marginDiv"> margin is for the margins </ div>
        And there, too, 20px;
    </div>

    <h2>w3school sample image </h2>
    source:
    <a href="https://www.w3schools.com/css/css_boxmodel.asp">CSS Box Model</a>
    <div id="w3_DIV_1">
        <div id="w3_DIV_2">
            <div id="w3_DIV_3">
                <div id="w3_DIV_4">
                </div>
            </div>
        </div>
    </div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接