HTML标签p和div的不同
- - CSDN博客Web前端推荐文章and
标题:HTML标签p和div的不同
原文:What is the difference between <p> and <div>?
链接: http://stackoverflow.com/questions/2226562/what-is-the-difference-between-p-and-div
作者: Andrew Hare
译者:MilkCu( http://blog.csdn.net/milkcu)
在用CSDN博客的网页编辑器 xhEditor写文章的时候,发现回车键由原来的生成<p>标签变为<div>标签了。对于<p>、<span>、<div>各标签的区别我还是明白的,但为什么编辑器会用这种变化呢。在我看来,这样在从其它地方粘贴内容的时候更容易保留样式吧,因为<div>比<p>支持的样式更多。在搜索资料的时候,发现 Stack Overflow的一则问答不错的,分享一下。
它们有语义上的不同,<div>标签用来作数据的容器,而<p>标签用来描述内容段落。
语义的不同让所有不同。HTML是一种标记语言,这意味着它要用有意义的方式来标记内容。大多数的开发者认为,语法是被动的,浏览器作用于这些标记,但是事实不是这样。
你选择的标记标签应能描述你的内容。不要在考虑外观的基础上标记你的文档,而是在它是什么的基础上标记你的文档。
如果你标记容器,就用<div>。如果你需要描述你的文章段落,就用<p>。
注意:理解<div>和<p>都是块状元素很重要,这意味着,大多数浏览器都会以相似的形式对待它们。
They have semantic difference - a <div>
element
is designed to describe a container of data whereas a <p>
element
is designed to describe a paragraph of content.
The semantics make all the difference. HTML is a markup language which means that it is designed to "mark up" content in a way that is meaningful to the consumer of the markup. Most developers believe that the semantics of the document are the default styles and rendering that browsers apply to these elements but that is not the case.
The elements that you choose to mark up your content should describe the content. Don't mark up your document based on how it should look - mark it up based on what it is.
If you need a generic container purely for layout purposes then use a <div>
.
If you need an element to describe a paragraph of content then use a <p>
.
Note: It is important to understand that
both <div>
and <p>
are block-level
elements which means that most browsers will treat them in a similar fashion.
原文是段很美妙的文字,也许被我翻译的凌乱了许多。怕别人看不懂我写的,特放上原文。这样的翻译就当作自娱自乐吧,在做的时候应该能学到点什么吧,虽然思想无法完全用语言表达。
(全文完)
and