和之间是否存在语义差异?
可能的重复:
HTML 标签 DIV 和 SPAN 有什么区别?< /a>
我知道在编码 HTML 时,我应该牢记语义,例如,h1 需要是主标题,h2 需要是子标题,表格需要是表格,使用 ;
表示强调,而不是 等。div 和 span 之间是否有适当的区别,除了一个是块而另一个是内联?
当我学习时,我被告知 用于设置文本中线的样式。如果我需要在网页中的某个位置放置一小段文本,并且不需要
标记,那么如果我坚持使用 div 的话,我会使用跨度吗?如果该文本需要覆盖两行(即,它需要宽度),如果它只包含文本,我应该使用什么?
Possible Duplicate:
What is the difference between HTML tags DIV and 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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你已经明白了。 Span = 内联,Div = 块。就这样。
如果一段文本需要它自己的布局(你想把它放在屏幕上的某个地方),那么它就是一个 div。
如果一段文本参与了它旁边的其他文本的布局,那么它就是一个跨度。
内联元素不能有自己的布局——那么它就不是内联的。
You've got it. Span = inline, Div = block. That's all.
If a blurb of text needs it's own layout, (You want to put it somewhere on the screen) then it's a 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.
有一个根本的区别:
是 块级元素,而
是一个内联元素。 区别是块级元素的开始和结束有换行符,而内联元素则没有。
也许更重要的是,根据 HTML 版本,对于块元素和内联元素内的其他元素有效有不同的规则。
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.
好吧,给你一个快速而简单的答案,DIV 是一个除法!目标是当某些元素需要被视为一个组时使用它!
前任:
使用 div 拥有一个登录面板,比如说,隐藏在屏幕左侧,当鼠标悬停在 div 上时就会显示出来:)
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!
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 :)
和
都是非常通用的元素。它们本身没有任何意义。
主要区别在于
是内联元素。这意味着如果您有类似的内容:
您在新行上没有“其他文本”。如果将跨度替换为
(块元素),则会出现换行符。请注意,在内联元素中包含块元素并不是正确的语法。因此,您可以将
放在
内部,但反之则不然。
请参阅此处了解更多信息:
维基百科 - Span 和 div
About.com - Span 与 div
<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: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
About.com - Span vs. div
span 和 div 之间的主要区别在于,span 是内联元素,而 div 是块元素,如
p
或段落元素。所以,本质上就是把所有的span变成div。您仅将跨度用于一行文本,例如应用效果或填充或其他内容。 Div 通常用于划分网页,因此如果您必须将一段文本放置在某处,我建议使用 div。
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 essenceIs 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 是分割块,span 用于跨越内联文本。
所以 Div 是一个具有高度和宽度的盒子/块,span 是内联的。基本上。
如果您想阅读规范,这里有一个链接。< /a>
Div is a division block, span is for spanning inline text.
So Div is a box/block with height and width, span is inline. Basically.
If you want to read the spec, here's a link.
从语义上讲,
和
都没有任何内在含义。它们是“包罗万象”的标签,旨在与现有标签不适合的内容一起使用。如果您关心语义,请使用 div 和 span 作为最后的手段。
它们之间唯一的区别是 div 是块级元素,而 span 是内联元素。这意味着默认情况下,div 将启动一个全新的块,从技术上讲,跨度内只允许内联元素和某些 CSS。大多数浏览器似乎都会不管规则如何处理标签(假设“标签汤”),并且您实际上可以使用 CSS 使其中一个像另一个一样运行,但如果您关心验证或跨浏览器兼容性,则不要执行任何操作(你确实关心哪个,对吧?)。
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.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?).