我想要一些简单的例子来展示<div>和<span>的使用。我曾看到它们都被用来用id或类标记页面的一部分,但我感兴趣的是,是否有时会优先使用其中一种而不是另一种。


当前回答

Div是一个块元素 Span是一个内联元素。

这意味着要在语义上使用它们,div应该用于包装文档的各个部分,而span应该用于包装文本、图像等小部分。

例如:

<div>This a large main division, with <span>a small bit</span> of spanned text!</div>

注意,在内联元素中放置块级元素是非法的,因此:

<div>Some <span>text that <div>I want</div> to mark</span> up</div>

...是非法的。


编辑:从HTML5开始,一些块元素可以放在一些内联元素中。请参阅这里的MDN参考,以获得相当清晰的清单。以上仍然是非法的,因为<span>只接受短语内容,而<div>是流内容。


你要求一些具体的例子,所以这是一个从我的保龄球网站,BowlSK:

< div id = "头" > < div id = " userbar " > 嗨,大家好,<span class="username">Chris Marasti-Georg</span> | <a href="/edit-profile.html">Profile</a> | .html < a href = " https://www.bowlsk.com/_ah/logout?..。“>签署< / > < / div > < h1 > < a href = " / " >碗<跨类= >“sk sk < / span > < / > < / h1 > < / div >

Ok, what's going on? At the top of my page, I have a logical section, the "header". Since this is a section, I use a div (with an appropriate id). Within that, I have a couple of sections: the user bar and the actual page title. The title uses the appropriate tag, h1. The userbar, being a section, is wrapped in a div. Within that, the username is wrapped in a span, so that I can change the style. As you can see, I have also wrapped a span around 2 letters in the title - this allows me to change their color in my stylesheet.

还要注意的是,HTML5包含了一组广泛的新元素,这些元素定义了常见的页面结构,如article、section、nav等。

Section 4.4 of the HTML 5 working draft lists them and gives hints as to their usage. HTML5 is still a working spec, so nothing is "final" yet, but it is highly doubtful that any of these elements are going anywhere. There is a javascript hack that you will need to use if you want to style these elements in some older version of IE - You need to create one of each element using document.createElement before any of those elements are specified in your source. There are a bunch of libraries that will take care of this for you - a quick Google search turned up html5shiv.

其他回答

真正重要的区别已经在克里斯的回答中提到了。然而,这并不是对每个人都有明显的影响。

作为一个内联元素,<span>只能包含其他内联元素。因此下面的代码是错误的:

<span><p>This is a paragraph</p></span>

上面的代码无效。要包装块级元素,必须使用另一个块级元素(例如<div>)。另一方面,<div>只能在块级元素合法的地方使用。

此外,这些规则在(X)HTML中是固定的,它们不会因CSS规则的存在而改变!所以下面的代码也是错的!

<span style="display: block"><p>Still wrong</p></span>
<span><p style="display: inline">Just as wrong</p></span>

Div是一个块元素,span是一个内联元素,它的宽度取决于它自己的内容,而Div不是

Div是一个块元素 Span是一个内联元素。

这意味着要在语义上使用它们,div应该用于包装文档的各个部分,而span应该用于包装文本、图像等小部分。

例如:

<div>This a large main division, with <span>a small bit</span> of spanned text!</div>

注意,在内联元素中放置块级元素是非法的,因此:

<div>Some <span>text that <div>I want</div> to mark</span> up</div>

...是非法的。


编辑:从HTML5开始,一些块元素可以放在一些内联元素中。请参阅这里的MDN参考,以获得相当清晰的清单。以上仍然是非法的,因为<span>只接受短语内容,而<div>是流内容。


你要求一些具体的例子,所以这是一个从我的保龄球网站,BowlSK:

< div id = "头" > < div id = " userbar " > 嗨,大家好,<span class="username">Chris Marasti-Georg</span> | <a href="/edit-profile.html">Profile</a> | .html < a href = " https://www.bowlsk.com/_ah/logout?..。“>签署< / > < / div > < h1 > < a href = " / " >碗<跨类= >“sk sk < / span > < / > < / h1 > < / div >

Ok, what's going on? At the top of my page, I have a logical section, the "header". Since this is a section, I use a div (with an appropriate id). Within that, I have a couple of sections: the user bar and the actual page title. The title uses the appropriate tag, h1. The userbar, being a section, is wrapped in a div. Within that, the username is wrapped in a span, so that I can change the style. As you can see, I have also wrapped a span around 2 letters in the title - this allows me to change their color in my stylesheet.

还要注意的是,HTML5包含了一组广泛的新元素,这些元素定义了常见的页面结构,如article、section、nav等。

Section 4.4 of the HTML 5 working draft lists them and gives hints as to their usage. HTML5 is still a working spec, so nothing is "final" yet, but it is highly doubtful that any of these elements are going anywhere. There is a javascript hack that you will need to use if you want to style these elements in some older version of IE - You need to create one of each element using document.createElement before any of those elements are specified in your source. There are a bunch of libraries that will take care of this for you - a quick Google search turned up html5shiv.

这里已经有了很好的、详细的答案,但没有可视化的例子,所以这里有一个简单的例子:

<div>这是一个div <div>这是一个div <div>这是一个div <span>这是一个span <span>这是一个span <span>这是一个span

<div>是块标记,而<span>是行内标记。

这很简单。

span的使用不会影响布局,因为它是内联的(在行中(不应该混淆,因为东西可以被换行)) 使用div会影响布局,里面的内容会出现在新行(block element)中,当你想添加的元素没有特殊的语义,而你想让它出现在新行中时,使用<div>。