HTML 中不需要 P-end-tag (</p>)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8460993/
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 11:56:05  来源:igfitidea点击:

P-end-tag (</p>) is not needed in HTML

htmlparagraph

提问by Hakan

</p>is only required in XHTML but not in HTML. Some times you have to close it anyway , e.g. when you align the paragraph left/right/center.

</p>仅在 XHTML 中需要,在 HTML 中不需要。有时您无论如何都必须关闭它,例如当您将段落左/右/中心对齐时。

Would mixing the usage of </p>as in the following be a bad idea? Note that there is no ending </p>tag in the 1st and 3rd paragraph.

</p>在下面混合使用as会不会是一个坏主意?请注意,</p>第 1 段和第 3 段中没有结束标记。

<h1>My article</h1>
<p>This is my 1st paragraph.
<p class="align=right">This is my 2nd paragraph</p>
<p>This is my 3rd paragraph.

Reasons why I don't want to close the P-tags:

我不想关闭 P 标签的原因:

  • Easy to work with in my CMS (more demanding code to make it XHTML)
  • 1kb lighter files
  • 易于在我的 CMS 中使用(要求更高的代码以使其成为 XHTML)
  • 1kb 更轻的文件

回答by Quentin

P-end-tag is only required in XHTML, not in HTML.

P-end-tag 只在 XHTML 中需要,在 HTML 中不需要。

Correct

正确的

But some times you have to close it any way eg. when you align the paragraph left/right/center.

但有时你必须以任何方式关闭它,例如。当您将段落左/右/中心对齐时。

Incorrect. The only time you need an explicit end tag is when you want to end the paragraph and immediately follow it by something that is allowed inside a paragraph (such as text or an inline element). This is usually a bad idea.

不正确。唯一需要显式结束标记的时候是当您想结束段落并立即跟随段落中允许的内容(例如文本或内联元素)时。这通常是一个坏主意。

Would it for any reason be a bad idea to mix the usage of P-end-tag

无论出于何种原因,混合使用 P-end-tag 都是一个坏主意吗?

Only that consistency is a virtue which aids in code maintenance.

只有这种一致性才是有助于代码维护的美德。

回答by ziesemer

I can't think of any reasons why you couldn't do this, but would strongly encourage just using both the beginning and end tags everywhere for consistency. It'll also make it easier for when you do decide to start following the XHTML standards.

我想不出任何你不能这样做的原因,但强烈建议在任何地方都使用开始和结束标签以保持一致性。当您决定开始遵循 XHTML 标准时,它也会更容易。

回答by Boda

You only need to finish the last <p>tag. Example:

你只需要完成最后一个<p>标签。例子:

<p align="left">Left
<p align="center">Center
<p align="right">Right</p> <!-If you don't put an ending tag, the text will continue in the same line
<p>End</p>

Plus it's faster and reduces the memory of the HTML file.

此外,它速度更快并减少了 HTML 文件的内存。