What is the difference between HTML tags
and ?
我想问一些简单的例子,展示 和 的用法。 我已经看到它们都用来标记一个带有id 或class 的页面的一部分,但是我有兴趣知道是否有时候一个优先于另一个。
div 是块元素,span 是内联的。
这意味着要在语义上使用它们,div应该用于包装文档的各个部分,而跨度应该用于包装文本,图像等的一小部分。
例如:
1
This a large main division, with <span>a small bit</span> of spanned text!
请注意,将块级元素放在内联元素中是违法的,因此:
1
Some <span>text that I want to mark</span> up
......是非法的。
编辑:从HTML5开始,一些块元素可以放在一些内联元素中。有关非常清晰的列表,请参阅此处的MDN参考。以上仍然是非法的,因为 只接受短语内容, 是流内容。
你问了一些具体的例子,所以从我的保龄球网站BowlSK中取出一个:
1
2
3
4
5
Hi there, <span class="username">Chris Marasti-Georg</span> |
Profile |
Sign out
Bowl<span class="sk">SK</span>
好的,发生了什么?在我的页面顶部,我有一个逻辑部分,"标题"。由于这是一个部分,我使用div(具有适当的id)。在其中,我有几个部分:用户栏和实际页面标题。标题使用适当的标记h1 。用户栏是一个部分,包含在div 中。在其中,用户名包含在span 中,以便我可以更改样式。正如你所看到的,我还在标题中包含了一个span 大约2个字母 - 这允许我在样式表中更改它们的颜色。
另请注意,HTML5包含一组广泛的新元素,用于定义常见的页面结构,例如文章,部分,导航等.HTML 5工作草案的第4.4节列出了它们,并提供了有关其用法的提示。 HTML5仍然是一个工作规范,所以没有什么是"最终的",但是任何这些元素都在任何地方都是非常值得怀疑的。如果你想在一些旧版本的IE中设置这些元素的样式,你需要使用javascript hack - 在你的源代码中指定任何元素之前,你基本上需要使用document.createElement 创建每个元素之一。有很多库会为你解决这个问题 - 快速谷歌搜索出现了html5shiv。
仅仅为了完整起见,我邀请你这样思考:
-
HTML中定义了许多块元素(前后换行符),以及许多内联标记(没有换行符)。
-
但是在现代HTML中,所有元素都应该具有含义:
是段落,
-
是列表项等等,我们应该使用正确的标记用于正确的目的 - 而不是像在我们使用
whether the content was a quote or not.
-
So, what do you do when there is no meaning to the thing you're trying to do? There's no meaning to a 400px-wide column, is there? You just want your column of text to be 400px wide because that suits your design.
-
For this reason, they added two more elements to HTML: the generic, or meaningless elements
and , because otherwise, people would go back to abusing the elements which do have meanings.
wmh
wmh
There are already good, detailed answers here, but no visual examples, so here's a quick illustration:
is a block tag, while is an inline tag.
wmh
是块级元素, 是内联元素。
如果你想对某些内联文本做一些事情,那么 就是要走的路,因为它不会引入 会出现的换行符。
正如其他人所指出的那样,每一个都隐含着一些语义,最重要的是a 意味着文档中的逻辑划分,类似于文档的某个部分或某些东西,la:
1
2
3
4
5
6
<p>
Lorem ipsum dolor sit amet, <span id="SomeSpecialText1">consectetuer adipiscing</span> elit. Duis congue vehicula purus.
</p>
<p>
Nam <span id="SomeSpecialText2">eget magna nec</span> sapien fringilla euismod. Donec hendrerit.
</p>
克里斯的回答中已经提到了真正重要的区别。但是,对每个人来说,影响并不明显。
作为内联元素, 可能只包含其他内联元素。因此,以下代码是错误的:
1
2
3
<span><p>
This is a paragraph
</p></span>
以上代码无效。要包装块级元素,必须使用另一个块级元素(例如 )。另一方面, 只能用于块级元素合法的地方。
此外,这些规则在(X)HTML中修复,并且CSS规则的存在不会改变它们!所以下面的代码也错了!
1
2
3
4
5
<span style="display: block"><p>
Still wrong
</p></span>
<span><p style="display: inline">Just as wrong
</p></span>
wmh
The significance of"block element" is implied but never stated explicitly. If we ignore all the theory (theory is good) then the following is a pragmatic comparison. The following:
1
2
3
4
5
6
<p>
This paragraph <span>has</span> a span.
</p>
<p>
This paragraph has a div.
</p>
produces:
1
2
3
4
5
6
This paragraph has a span.
This paragraph
has
a div.
That shows that not only should a div not be used inline, it simply won't produce the desired affect.
wmh
正如其他答案中所提到的,默认情况下,div 将呈现为块元素,而span 将在其上下文中呈现为内联。但两者都没有任何语义价值;它们的存在允许您将样式和标识应用于任何给定的内容。使用样式,您可以使div 像span 一样,反之亦然。
div 的一个有用样式是inline-block
例子:
http://dustwell.com/div-span-inline-block.html
CSS显示:内联vs内联块
我在游戏网络项目中使用inline-block 取得了巨大的成功。
wmh
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 mentionning that"any new
element should be on an old one" and adding"If we create a tag with
no semantics it can be used anywehere 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 con-
tainer is needed to carry the LANG and BIDI attributes in
cases where no other element is appropriate; the SPAN ele-
ment is introduced for that purpose.
- 来源(IETF草案)
div 的历史:
DIV elements can be used to structure HTML documents as a hierarchy of divisions.
...
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规范
简而言之,这两个元素都源于对更具语义通用的容器的需求。 Span被提议作为样式文本的 元素的更通用替代。 Div被提议作为划分页面的通用方法,并且具有替换 标签以进行居中对齐内容的额外好处。 Div一直是一个块元素,因为它作为页面分隔符的历史。 Span一直是内联元素,因为它的最初目的是文本样式,而今天div和span都分别是默认块和内联显示属性的通用元素。
wmh
Div is a block element and span is an inline element and its width depends upon the content of it self where div does not
wmh
我会说,如果你知道一点西班牙语来看这个页面,那么在哪里得到适当的解释。
但是,快速定义是div 用于划分部分,span 用于将某种样式应用于另一个块元素(如div )中的元素。
记住,基本上,他们自己也不执行任何功能。
它们并非仅仅通过标签来定义功能。
它们只能在CSS的帮助下进行定制。
现在提出你的问题:
SPAN标签和一些样式将有助于在html中保持在一行中,例如在段落中。
这是HTML中的行级别或语句级别。
例:
1
2
3
<p>
Am writing<span class="time">this answer</span> in my free time of my day.
</p>
所说的DIV标签功能只能通过样式来支持,可以容纳大量的HTML代码。
DIV是Block级别
例:
1
2
3
4
<p>
Am writing <span class="time"> this answer</span> in my free time of my day.
</p>
根据您的要求,两者都有时间和案例。
希望我的回答清楚。
谢谢。
wmh
In HTML there are tags that add structure or semantics to content. For example the
tag is used to identify a paragraph. Another example is the tag for an ordered list.
When there is no suitable tag available in HTML as shown above, the and tags are usually resorted to.
The tag is used to identify a blocklevel section/division of a document that has a line break both before and after it.
Examples of where div tags can be used are headers, footers, navigations etc. However in HTML 5 these tags have already been provided.
The tag is used to identify an inline section/division of a document.
For example a span tag can be used to add inline pictographs to an element.
wmh
我想问一些简单的例子,展示
这意味着要在语义上使用它们,div应该用于包装文档的各个部分,而跨度应该用于包装文本,图像等的一小部分。
例如:
1 | This a large main division, with <span>a small bit</span> of spanned text! |
请注意,将块级元素放在内联元素中是违法的,因此:
1 | Some <span>text that I want to mark</span> up |
......是非法的。
编辑:从HTML5开始,一些块元素可以放在一些内联元素中。有关非常清晰的列表,请参阅此处的MDN参考。以上仍然是非法的,因为
你问了一些具体的例子,所以从我的保龄球网站BowlSK中取出一个:
1 2 3 4 5 | Hi there, <span class="username">Chris Marasti-Georg</span> | Profile | Sign out Bowl<span class="sk">SK</span> |
好的,发生了什么?在我的页面顶部,我有一个逻辑部分,"标题"。由于这是一个部分,我使用div(具有适当的id)。在其中,我有几个部分:用户栏和实际页面标题。标题使用适当的标记
另请注意,HTML5包含一组广泛的新元素,用于定义常见的页面结构,例如文章,部分,导航等.HTML 5工作草案的第4.4节列出了它们,并提供了有关其用法的提示。 HTML5仍然是一个工作规范,所以没有什么是"最终的",但是任何这些元素都在任何地方都是非常值得怀疑的。如果你想在一些旧版本的IE中设置这些元素的样式,你需要使用javascript hack - 在你的源代码中指定任何元素之前,你基本上需要使用
仅仅为了完整起见,我邀请你这样思考:
- HTML中定义了许多块元素(前后换行符),以及许多内联标记(没有换行符)。
-
但是在现代HTML中,所有元素都应该具有含义:
- So, what do you do when there is no meaning to the thing you're trying to do? There's no meaning to a 400px-wide column, is there? You just want your column of text to be 400px wide because that suits your design.
-
For this reason, they added two more elements to HTML: the generic, or meaningless elements
and , because otherwise, people would go back to abusing the elements which do have meanings.
wmh
wmh
There are already good, detailed answers here, but no visual examples, so here's a quick illustration:
wmh
如果你想对某些内联文本做一些事情,那么
正如其他人所指出的那样,每一个都隐含着一些语义,最重要的是a
1 2 3 4 5 6 | <p> Lorem ipsum dolor sit amet, <span id="SomeSpecialText1">consectetuer adipiscing</span> elit. Duis congue vehicula purus. </p> <p> Nam <span id="SomeSpecialText2">eget magna nec</span> sapien fringilla euismod. Donec hendrerit. </p> |
克里斯的回答中已经提到了真正重要的区别。但是,对每个人来说,影响并不明显。
作为内联元素,
1 2 3 | <span><p> This is a paragraph </p></span> |
以上代码无效。要包装块级元素,必须使用另一个块级元素(例如
此外,这些规则在(X)HTML中修复,并且CSS规则的存在不会改变它们!所以下面的代码也错了!
1 2 3 4 5 | <span style="display: block"><p> Still wrong </p></span> <span><p style="display: inline">Just as wrong </p></span> |
wmh
The significance of"block element" is implied but never stated explicitly. If we ignore all the theory (theory is good) then the following is a pragmatic comparison. The following:
1 2 3 4 5 6 | <p> This paragraph <span>has</span> a span. </p> <p> This paragraph has a div. </p> |
produces:
1 2 3 4 5 6 | This paragraph has a span. This paragraph has a div. |
That shows that not only should a div not be used inline, it simply won't produce the desired affect.
wmh
正如其他答案中所提到的,默认情况下,
例子:
http://dustwell.com/div-span-inline-block.html
CSS显示:内联vs内联块
我在游戏网络项目中使用
wmh
Just wanted to add some historical context to how there came to be
History of
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 mentionning that"any new
element should be on an old one" and adding"If we create a tag with
no semantics it can be used anywehere 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
First, a generic con-
tainer is needed to carry the LANG and BIDI attributes in
cases where no other element is appropriate; the SPAN ele-
ment is introduced for that purpose.
- 来源(IETF草案)
DIV elements can be used to structure HTML documents as a hierarchy of divisions.
...
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规范
简而言之,这两个元素都源于对更具语义通用的容器的需求。 Span被提议作为样式文本的
wmh
Div is a block element and span is an inline element and its width depends upon the content of it self where div does not
wmh
我会说,如果你知道一点西班牙语来看这个页面,那么在哪里得到适当的解释。
但是,快速定义是
记住,基本上,他们自己也不执行任何功能。
它们并非仅仅通过标签来定义功能。
它们只能在CSS的帮助下进行定制。
现在提出你的问题:
SPAN标签和一些样式将有助于在html中保持在一行中,例如在段落中。
这是HTML中的行级别或语句级别。
例:
1 2 3 | <p> Am writing<span class="time">this answer</span> in my free time of my day. </p> |
所说的DIV标签功能只能通过样式来支持,可以容纳大量的HTML代码。
DIV是Block级别
例:
1 2 3 4 | <p> Am writing <span class="time"> this answer</span> in my free time of my day. </p> |
根据您的要求,两者都有时间和案例。
希望我的回答清楚。
谢谢。
wmh
In HTML there are tags that add structure or semantics to content. For example the
tag is used to identify a paragraph. Another example is the
When there is no suitable tag available in HTML as shown above, the
The
Examples of where div tags can be used are headers, footers, navigations etc. However in HTML 5 these tags have already been provided.
The
For example a span tag can be used to add inline pictographs to an element.
wmh