Html 我应该在标记中使用 <p /> 标签吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:33:07  来源:igfitidea点击:

Should I use the <p /> tag in markup?

htmllayouttagsmarkup

提问by maxp

I have always used either <br />or a <div/>tag when something more advanced is necessary.

当需要更高级的东西时,我总是使用<br /><div/>标签。

Is use of the <p/>tag still encouraged?

是否<p/>仍然鼓励使用该标签?

回答by Christian Hayter

Modern HTML semantics are:

现代 HTML 语义是:

  • Use <p></p>to contain a paragraph of text in a document.
  • Use <br />to indicate a line break insidea paragraph (i.e. a new line without the paragraph block margins or padding).
  • Use <div></div>to contain a piece of application UI that happens to have block layout.
  • 用于<p></p>在文档中包含一段文本。
  • 使用<br />指示换行一个段落(即新线没有段落块保证金或填充)。
  • 使用<div></div>含有一块,恰好有块布局应用程序的UI。

Don't use <div />or <p />on its own, those tags are meant to contain content. They appear to work as paragraph breaks only because when the browser sees them, it "helpfully" closes the current block tag before opening the empty one.

不要使用<div />或单独使用<p />,这些标签旨在包含内容。它们似乎作为分段符工作,只是因为当浏览器看到它们时,它“有帮助地”在打开空的之前关闭了当前的块标记。

回答by Tyler Carter

A <p>tag wraps around something, unlike an <input/>tag, which is a singular item. Therefore, there isn't a reason to use a <p/>tag..

一个<p>标签回绕的东西,不像<input/>标签,这是一个奇异的项目。因此,没有理由使用<p/>标签..



I've been told that im using <br />when i should use <p />instead. – maxp 49 secs ago

有人告诉我<br />,我应该在应该使用的时候使用它<p />。– maxp 49 秒前

If you need to use <p>tags, I suggest wrapping the entire paragraph inside a <p>tag, which will give you a line break at the end of a paragraph. But I don't suggest just substituting something like <p/>for <br/>

如果您需要使用<p>标签,我建议将整个段落包裹在一个<p>标签中,这样可以在段落末尾换行。但我不建议只用类似的东西<p/>代替<br/>

<p>tags are for paragraphs and signifying the end of a paragraph. <br/>tags are for line breaks. If you need a new line then use a <br/>tag. If you need a new paragraph, then use a <p>tag.

<p>标签用于段落并表示段落的结束。<br/>标签用于换行。如果您需要新行,请使用<br/>标签。如果您需要一个新段落,请使用<p>标签。

回答by Robert Koritnik

Paragraph is a paragraph, break is a break.

段是段,断是断。

A <p>is like a regular Returnin Office Word.
A <br>is like a soft return Shift + Returnin Office Word.

A<p>就像ReturnOffice Word 中的常规。
A<br>就像是Shift + ReturnOffice Word 中的软回车。

the first one sets all paragraph settings/styles, the second one barely breaks a line of text.

第一个设置所有段落设置/样式,第二个几乎没有中断一行文本。

Yes <p>elements are encouraged,and won't get deprecated any time soon.

Yes<p>元素受到鼓励,并且不会很快被弃用。

回答by Zack Marrapese

A <p>signifies a paragraph. It should be used only to wrap a paragraph of text.

A<p>表示一个段落。它应该仅用于包装一段文本。

It is more appropriate to use the <p>tag for this as opposed to <div>, because this is semantically correct and expected for things such as screen readers, etc.

<p>与 相比<div>,为此使用标记更合适,因为这在语义上是正确的,并且适用于屏幕阅读器等。

回答by Greg

Using <p />has never been encouraged:

使用<p />从未鼓励:

From XHTML HTML Compatibility Guidelines

来自XHTML HTML 兼容性指南

C.3. Element Minimization and Empty Element Content

Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use <p> </p>and not <p />).

C.3. 元素最小化和空元素内容

给定内容模型不是 EMPTY 的元素的空实例(例如,空标题或段落)不要使用最小化形式(例如 use<p> </p>和 not <p />)。

回答by lance

Interestingly, Counting paragraph tagsstates that a random sample of 833,866 HTML documents found that 50.1% of the documents sampled contain only <p>...</p>, 4.41% contain only ...<p>..., and a mere 0.21% contain only ...<p/>....

有趣的是,计数段落标签指出,对 833,866 个 HTML 文档的随机样本发现,50.1% 的样本文档仅包含 <p>...</p>,4.41% 仅包含...<p>...,并且只有 0.21% 只包含...<p/>....

回答by christopheml

From the HTML 4.01 Specification:

来自HTML 4.01 规范

We discourage authors from using empty P elements. User agents should ignore empty P elements.

我们不鼓励作者使用空的 P 元素。用户代理应该忽略空的 P 元素。

While they are syntactically correct, empty P elements serve no real purpose and should be avoided.

虽然它们在语法上是正确的,但空的 P 元素没有真正的用途,应该避免。

回答by Martin Liversage

The HTML DTD does not prohibit you from using an empty <p>(a <p>element may contain PCDATAincluding the empty string) , but it doesn't make much sense to have an empty paragraph.

HTML DTD 不禁止您使用空<p><p>元素可能包含PCDATA包括空字符串),但使用空段落没有多大意义。

回答by Philippe

The <p>tag defines a paragraph. There's no reason for an empty paragraph.

<p>标签定义一个段落。没有理由有一个空的段落。

回答by Marius

Use it for what? All tags have their own little purpose in life, but no tag should be used for everything. Find out what you are trying to make, and then decide on what tag fits that idea best:

用它做什么?所有的标签在生活中都有自己的小目的,但没有一个标签应该用于一切。找出您要尝试制作的内容,然后确定最适合该想法的标签:

If it is a paragraph of text, or at least a few lines, then wrap it in <p></p>

如果是一段文字,或者至少几行,那么把它包起来 <p></p>

If you need a line break between two lines of text, then use <br />

如果您需要在两行文本之间换行,请使用 <br />

If you need to wrap many other elements in one element, then use the <div></div>tags.

如果您需要将许多其他元素包装在一个元素中,请使用<div></div>标签。