空段落标签 - 我应该在 HTML 编辑器中允许还是不允许?

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

Empty paragraph tags - should I allow in HTML editor or not?

htmlblank-line

提问by ingredient_15939

I'm using CKEditor 4, which seems to have a default setting of using <p></p>to indicate a blank line. Up to now, I've always seen blank lines in HTML represented by <p>&nbsp;</p>- ie. not an empty tag.

我正在使用 CKEditor 4,它似乎具有<p></p>用于指示空行的默认设置。到目前为止,我一直在 HTML 中看到由<p>&nbsp;</p>-表示的空行。不是空标签。

So CKEditor's default output representing a blank line is, eg:

所以 CKEditor 的默认输出代表一个空行,例如:

<p>paragraph before blank line</p>
<p></p>
<p>paragraph after blank line</p>

The problem is, for a change, IE7,8,9 seems to render this "correctly" as a blank line, whereas Firefox and Chrome do not - they seem to ignore empty <p>and <div>tags, in terms of layout.

问题是,对于更改,IE7、8、9 似乎将其“正确地”呈现为空行,而 Firefox 和 Chrome 则没有——它们似乎在布局方面忽略了空<p><div>标签。

So, at the moment, I have to tell CKEditor to include &nbsp;in blank lines (not its default setting for some reason) and, at the back-end, replace any occurrences of empty <p>tags that may slip through.

所以,目前,我必须告诉 CKEditor 包含&nbsp;在空行中(由于某种原因不是它的默认设置),并在后端替换任何<p>可能漏掉的空标签。

My question is, what is the gold standard for representing blank lines in HTML? The good-old <p>&nbsp;</p>, or something else?

我的问题是,在 HTML 中表示空行的黄金标准是什么?好老<p>&nbsp;</p>,还是别的什么?

Also, given that CKEditor's default setting is an empty <p>tag for blank lines, are Chrome and Firefox wrong to ignore them? Or is IE wrong to render them as blank lines, and CKEditor's default should really be to use <p>&nbsp;</p>?

另外,鉴于 CKEditor 的默认设置是<p>空白行的空标签,Chrome 和 Firefox 是否错误地忽略了它们?或者 IE 错误地将它们呈现为空行,而 CKEditor 的默认值真的应该使用<p>&nbsp;</p>

回答by Jukka K. Korpela

As per HTML 4.01 on the pelement, <p></p>is valid but should not be used, and browsers should ignore it. Browser behavior is inconsistent and generally does not obey the spec. You test this with <hr><hr><p></p><hr>for example – on Chrome, it shows that although the pelement has zero height, it has top and bottom margins, so it affects rendering, i.e. is not ignored. – The HTML5 CR seems to be silent about this issue, i.e. it does not say anything specific about a pelement with empty content. Update:HTML5 has a general statementabout elements with phrasing content as the content mode (such as p): such an element should contain “should have either at least one descendant Text node that is not inter-element whitespace, or at least one descendant element node that is embedded content”. This means that <p></p>should not be used; but this is a recommendation, not a conformance requirement (“should”, not “shall”).

根据HTML 4.01 的pelement<p></p>有效但不应使用,浏览器应忽略它。浏览器行为不一致,通常不遵守规范。<hr><hr><p></p><hr>例如,您对此进行测试- 在 Chrome 上,它显示虽然p元素的高度为零,但它具有顶部和底部边距,因此它会影响渲染,即不会被忽略。– HTML5 CR 似乎对这个问题保持沉默,即它没有说明任何关于p具有空内容的元素的具体内容。更新:HTML5 有一个关于以短语内容作为内容模式的元素的一般声明(例如p):这样的元素应该包含“应该至少有一个不是元素间空白的后代 Text 节点,或者至少有一个嵌入内容的后代元素节点”。这意味着<p></p>不应该使用;但这是一个建议,而不是一致性要求(“应该”,而不是“应该”)。

The effect of <p>&nbsp;</p>is undefined by the spec (the rendering of the no-break space character has not been defined), but browsers generally treat no-break space as yet another graphic character, which just happens to be rendered with an empty glyph. So in practice it generates a line box, with a height determined by the computed value of line-height.

<p>&nbsp;</p>规范未定义的效果(未定义不间断空格字符的渲染),但浏览器通常将不间断空格视为另一个图形字符,恰好用空字形渲染。所以在实践中它会生成一个线框,其高度由 的计算值确定line-height

By default, there are margins between pelements, corresponding to one empty line. So any need for a blank line between pelements seems to have been caused by overriding the default styling, by something like p { margin: 0 }in CSS. In order to override that, additional CSS rather than an artificial pelement should be used. That is, some of pelements should have a suitable nonzero bottom or top margin.

默认情况下,p元素之间有边距,对应一个空行。所以在p元素之间需要一个空行似乎是由覆盖默认样式引起的,比如p { margin: 0 }在 CSS 中。为了覆盖它,p应该使用额外的 CSS 而不是人工元素。也就是说,一些p元素应该有一个合适的非零下边距或上边距。

The HTML 4.01 defines “white space characters”as referring to Ascii space, Ascii tab, Ascii form feed, and zero-width space (thus, notto no-break space for example), defines their rendering, and adds: “This specification does not indicate the behavior, rendering or otherwise, of space characters other than those explicitly identified here as white space characters. For this reason, authors should use appropriate elements and styles to achieve visual formatting effects that involve white space, rather than space characters.”

HTML 4.01 将“空白字符”定义为引用 Ascii 空格、Ascii 制表符、Ascii 换页符和零宽度空格(例如,不是不间断空格),定义了它们的呈现方式,并添加了:“本规范不表示除此处明确标识为空白字符之外的空格字符的行为、渲染或其他方式。出于这个原因,作者应该使用适当的元素和样式来实现涉及空白而不是空格字符的视觉格式效果。”

回答by Mikael H?rsj?

I would say that IE has this one wrong, empty paragraphs makes no sense to me.

我会说 IE 有这个错误,空段落对我来说毫无意义。

From w3.org:

来自 w3.org:

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

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

I would suggest that when you users create blank lines you wrap their text in paragraphs.

我建议当您的用户创建空行时,您将他们的文本包裹在段落中。

So instead of:

所以而不是:

before line break<p>&nbsp;</p>after line break

You should generate

你应该生成

<p>before line break</p><p>after line break</p>

Makes sense?

说得通?

回答by Spook SEO

When coding using standards compliant HTML & CSS coding the key factor of website text content will be placed in a normal paragraph tag. This is written as <p>. To make SEO friendly paragraph text when writing paragraph text it pays to look at the previous sub heading and then create web page content that explains and expands on this heading text. Yes you have to use the Empty paragraph tags in HTML editor.

当使用符合标准的 HTML 和 CSS 编码进行编码时,网站文本内容的关键因素将被放置在一个普通的段落标签中。这写成<p>. 要在编写段落文本时制作对 SEO 友好的段落文本,请查看前一个子标题,然后创建解释和扩展此标题文本的网页内容。是的,您必须在 HTML 编辑器中使用空段落标记。

回答by newpatriks

I think that for SEO is not good that the navigators render a empty <p>. I replace it with js or php before save it on ddbb :)

我认为对于 SEO 来说,导航器呈现一个空的<p>. 在将其保存在 ddbb 上之前,我将其替换为 js 或 php :)