是否有语法差异和? [重复][英]Is there a semantic difference 's and 's? [duplicate] 本文翻译自  DavidR  查看原文  2010-06-02  1913    html/

时间:2021-10-12 18:02:53

Possible Duplicate:
What is the difference between HTML tags DIV and SPAN?

可能重复:HTML标签DIV和SPAN有什么区别?

I know when coding HTML, I'm supposed to keep semantics in mind, e.g., h1 needs to be a main header, h2 needs to be a subheader, tables need to be tables, use <em> for emphasis instead of <i>, etc. Is there a proper difference between divs and spans except one is a block and the other is in-line?

我知道在编写HTML时,我应该记住语义,例如,h1需要是一个主头,h2需要是一个子头,表需要是表,使用来强调而不是 div和div之间是否有正确的区别,除了一个是块而另一个是内联?

When I was learning I was told that <span>'s were for styling text mid-line. If I had a small blurb of text that I needed positioned at a certain point in my webpage, one that doesn't warrent a <p> tag, would I use a span should I stick with div's? What if that text needs to cover two lines (i.e., it needs a width) if it contains nothing but text, what should I use?

当我在学习的时候,我被告知用于设置文本中线。如果我需要在我的网页中的某个位置放置一小段文字,一个不用

标签的文字,我是否应该使用跨度?如果该文本需要覆盖两行(即,它需要一个宽度),如果它只包含文本,我应该使用什么呢?

7 个解决方案

#1


8  

Semantically, neither <div> nor <span> has any intrinsic meaning. They're "catch-all" tags meant to be used with stuff where no existing tag really fits. Use divs and spans as a last resort, if you care about semantics.

从语义上讲,

都没有任何内在含义。它们是“全能”标签,意在与没有现有标签的东西一起使用。如果您关心语义,请使用div和span作为最后的手段。

The only difference between them is that divs are block-level elements, and spans are inline. Meaning by default, a div will start a whole new block, and technically only inline elements and certain CSS would be allowed inside a span. Most browsers seem to process the tags regardless of the rules (assuming "tag soup"), and you can actually make either act like the other with CSS, but don't do any of that if you care about validation or cross-browser compatibility (which you DO care about, right?).

它们之间唯一的区别是div是块级元素,跨度是内联的。默认情况下,div将启动一个完整的新块,从技术上讲,只允许内联元素和某个CSS在某个范围内。大多数浏览器似乎都处理标签而不管规则(假设是“标签汤”),你实际上可以使用CSS做另一个,但如果您关心验证或跨浏览器兼容性,则不要做任何其他操作(你关心的,对吗?)

#2


6  

The primary difference between a span and a div is that a span is an inline element whereas a div is a block element, like a p or paragraph element. So, in essence

span和div之间的主要区别在于span是内联元素,而div是块元素,如p或段落元素。所以,实质上

span { display: block; }

Is essentially turning all spans into divs. You use a span for just a line of text, like to apply effects or padding or something. Divs are generally for dividing up your web page, so if you had to position a piece of text somewhere I would recommend using a div.

实质上是将所有跨度转换为div。您只使用跨度来表示一行文本,例如应用效果或填充等。 Div通常用于划分你的网页,所以如果你不得不在某个地方放置一段文字,我建议使用div。

#3


5  

Div is a division block, span is for spanning inline text.

Div是一个分区块,span用于跨越内联文本。

So Div is a box/block with height and width, span is inline. Basically.

因此Div是一个高度和宽度的框/块,跨度是内联的。基本上。

If you want to read the spec, here's a link.

如果你想阅读规范,这里是一个链接。

The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.

DIV和SPAN元素与id和class属性一起提供了向文档添加结构的通用机制。这些元素将内容定义为内联(SPAN)或块级(DIV),但不对内容强加其他表示习语。因此,作者可以将这些元素与样式表,lang属性等结合使用,以根据自己的需要和品味定制HTML。

#4


3  

<span> and <div> are both very generic elements. In themselves, they have no meaning.

都是非常通用的元素。他们自己没有任何意义。

The main difference is that <span> is an inline element. That means if you have something like:

主要区别在于是内联元素。这意味着如果你有类似的东西:

<span>Some text.</span> Some other text

You don't have "Some other text" on a new line. If you replaced the spans with <div> (a block element), there would be newlines. Note that it is not proper syntax to have a block element inside an inline element. Therefore, you can have <span>'s inside <div>'s, but not vice versa.

您没有新行上的“其他文字”。如果用

(块元素)替换了跨度,则会有换行符。请注意,在内联元素中包含块元素是不正确的语法。因此,您可以在
内部使用 ,但反之亦然。

See here for more:

请看这里了解更多:

Wikipedia - Span and div

* - 跨度和div

About.com - Span vs. div

About.com - 跨度与div

#5


2  

There is a fundamental difference: <div> is a block-level element, while <span> is an inline element. The difference is that block-level elements start and end with line breaks, and inline elements don't.

有一个根本区别:

是一个块级元素,而 是一个内联元素。区别在于块级元素以换行符开头和结尾,而内联元素则不然。

Perhaps even more importantly, depending on the HTML version, there are different rules for what other elements are valid inside block and inline elements.

也许更重要的是,根据HTML版本,对于块和内联元素中的其他元素有效,有不同的规则。

#6


1  

Well, to give you a fast and simple answer, DIV is a division! The goal is to use it when certain elements are in need to be treated as a group!

好吧,为了给你一个快速而简单的答案,DIV是一个师!目标是在某些元素需要被视为一个群体时使用它!

Ex: Use a div to have a login panel, lets say, hidden @ the left side of the screen, that show's up when the mouse hovers the div :)

例如:使用div有一个登录面板,让我们说,隐藏在屏幕的左侧@当鼠标悬停在div时显示为up :)

#7


1  

You've got it. Span = inline, Div = block. That's all.

你有它。 Span = inline,Div = block。就这样。

If a blurb of text needs it's own layout, (You want to put it somewhere on the screen) then it's a div.

如果文本模糊需要它自己的布局,(你想把它放在屏幕上的某个地方)那么它就是一个div。

If a blurb of text participates in the layout of the other text right next to it, then it's a span.

如果文本的模糊参与其旁边的其他文本的布局,那么它就是一个跨度。

An inline element can't have its own layout -- then it wouldn't be inline.

内联元素不能有自己的布局 - 那么它就不是内联的。

#1


8  

Semantically, neither <div> nor <span> has any intrinsic meaning. They're "catch-all" tags meant to be used with stuff where no existing tag really fits. Use divs and spans as a last resort, if you care about semantics.

从语义上讲,

都没有任何内在含义。它们是“全能”标签,意在与没有现有标签的东西一起使用。如果您关心语义,请使用div和span作为最后的手段。

The only difference between them is that divs are block-level elements, and spans are inline. Meaning by default, a div will start a whole new block, and technically only inline elements and certain CSS would be allowed inside a span. Most browsers seem to process the tags regardless of the rules (assuming "tag soup"), and you can actually make either act like the other with CSS, but don't do any of that if you care about validation or cross-browser compatibility (which you DO care about, right?).

它们之间唯一的区别是div是块级元素,跨度是内联的。默认情况下,div将启动一个完整的新块,从技术上讲,只允许内联元素和某个CSS在某个范围内。大多数浏览器似乎都处理标签而不管规则(假设是“标签汤”),你实际上可以使用CSS做另一个,但如果您关心验证或跨浏览器兼容性,则不要做任何其他操作(你关心的,对吗?)

#2


6  

The primary difference between a span and a div is that a span is an inline element whereas a div is a block element, like a p or paragraph element. So, in essence

span和div之间的主要区别在于span是内联元素,而div是块元素,如p或段落元素。所以,实质上

span { display: block; }

Is essentially turning all spans into divs. You use a span for just a line of text, like to apply effects or padding or something. Divs are generally for dividing up your web page, so if you had to position a piece of text somewhere I would recommend using a div.

实质上是将所有跨度转换为div。您只使用跨度来表示一行文本,例如应用效果或填充等。 Div通常用于划分你的网页,所以如果你不得不在某个地方放置一段文字,我建议使用div。

#3


5  

Div is a division block, span is for spanning inline text.

Div是一个分区块,span用于跨越内联文本。

So Div is a box/block with height and width, span is inline. Basically.

因此Div是一个高度和宽度的框/块,跨度是内联的。基本上。

If you want to read the spec, here's a link.

如果你想阅读规范,这里是一个链接。

The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.

DIV和SPAN元素与id和class属性一起提供了向文档添加结构的通用机制。这些元素将内容定义为内联(SPAN)或块级(DIV),但不对内容强加其他表示习语。因此,作者可以将这些元素与样式表,lang属性等结合使用,以根据自己的需要和品味定制HTML。

#4


3  

<span> and <div> are both very generic elements. In themselves, they have no meaning.

都是非常通用的元素。他们自己没有任何意义。

The main difference is that <span> is an inline element. That means if you have something like:

主要区别在于是内联元素。这意味着如果你有类似的东西:

<span>Some text.</span> Some other text

You don't have "Some other text" on a new line. If you replaced the spans with <div> (a block element), there would be newlines. Note that it is not proper syntax to have a block element inside an inline element. Therefore, you can have <span>'s inside <div>'s, but not vice versa.

您没有新行上的“其他文字”。如果用

(块元素)替换了跨度,则会有换行符。请注意,在内联元素中包含块元素是不正确的语法。因此,您可以在
内部使用 ,但反之亦然。

See here for more:

请看这里了解更多:

Wikipedia - Span and div

* - 跨度和div

About.com - Span vs. div

About.com - 跨度与div

#5


2  

There is a fundamental difference: <div> is a block-level element, while <span> is an inline element. The difference is that block-level elements start and end with line breaks, and inline elements don't.

有一个根本区别:

是一个块级元素,而 是一个内联元素。区别在于块级元素以换行符开头和结尾,而内联元素则不然。

Perhaps even more importantly, depending on the HTML version, there are different rules for what other elements are valid inside block and inline elements.

也许更重要的是,根据HTML版本,对于块和内联元素中的其他元素有效,有不同的规则。

#6


1  

Well, to give you a fast and simple answer, DIV is a division! The goal is to use it when certain elements are in need to be treated as a group!

好吧,为了给你一个快速而简单的答案,DIV是一个师!目标是在某些元素需要被视为一个群体时使用它!

Ex: Use a div to have a login panel, lets say, hidden @ the left side of the screen, that show's up when the mouse hovers the div :)

例如:使用div有一个登录面板,让我们说,隐藏在屏幕的左侧@当鼠标悬停在div时显示为up :)

#7


1  

You've got it. Span = inline, Div = block. That's all.

你有它。 Span = inline,Div = block。就这样。

If a blurb of text needs it's own layout, (You want to put it somewhere on the screen) then it's a div.

如果文本模糊需要它自己的布局,(你想把它放在屏幕上的某个地方)那么它就是一个div。

If a blurb of text participates in the layout of the other text right next to it, then it's a span.

如果文本的模糊参与其旁边的其他文本的布局,那么它就是一个跨度。

An inline element can't have its own layout -- then it wouldn't be inline.

内联元素不能有自己的布局 - 那么它就不是内联的。