HTML標簽p和div的不同


標題: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.

后記

原文是段很美妙的文字,也許被我翻譯的凌亂了許多。怕別人看不懂我寫的,特放上原文。這樣的翻譯就當作自娛自樂吧,在做的時候應該能學到點什么吧,雖然思想無法完全用語言表達。

(全文完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM