Html 范围内没有 p 元素,但看到了 ap 结束标记。 w3c 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21084870/
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
No p element in scope but a p end tag seen.w3c validation
提问by Sam IR
My HTML is as as below. I have opened all elements and closed them. Still when I check it on w3c it shows error. I cant figure it out.
我的 HTML 如下所示。我已经打开了所有元素并关闭了它们。仍然当我在 w3c 上检查它时显示错误。我想不通。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<p>
<div class="inr_content clearfix">
<div class="col2 first fl">
to provide a drive-in services.
</div>
<div class="col2 last fr">
to provide a drive-in services.
</div>
</div>
</p>
</body>
</html>
回答by Mr. Alien
That's because you are nesting a block level element inside the p
tag which is invalid. You can only nest inline elements such as span
, a
and img
inside p
tag. So your markup is invalid, consider making something like
那是因为您在p
无效的标签内嵌套了一个块级元素。您只能嵌套内联元素,例如span
,a
和img
insidep
标记。所以你的标记是无效的,考虑做类似的事情
<div class="inr_content clearfix">
<div class="col2 first fl">
<p>to provide a drive-in services.</p>
</div>
<div class="col2 last fr">
<p>to provide a drive-in services.</p>
</div>
</div>
From W3C[1]:
来自 W3C [1]:
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
P 元素代表一个段落。它不能包含块级元素(包括 P 本身)。
1 - Reference
1 -参考
回答by Jukka K. Korpela
Since the syntax of the p
elementdoes not allow a div
child, and the end tag </p>
may be omitted, the validator (and a browser) implies </p>
when it encounters a <div>
tag when parsing a p
element. That is, when p
is being parsed (or “is open”), the start tag of a div
element implicitly closes it, as if the markup had:
由于p
元素的语法不允许有div
子元素,并且</p>
可能会省略结束标记,因此验证器(和浏览器)在解析元素</p>
时会暗示何时遇到<div>
标记p
。也就是说,当p
被解析(或“打开”)时,div
元素的开始标签隐式地关闭它,就好像标记有:
<body>
<p>
</p><div class="inr_content clearfix">
<div class="col2 first fl">
This means that there is a p
element with only whitespace in it. The </p>
tag that appears later thus has no matching start tag, and it is reported as invalid. Browsers ignore such homeless end tags, but validators have to report them.
这意味着有一个p
元素中只有空格。</p>
稍后出现的标签因此没有匹配的开始标签,并被报告为无效。浏览器会忽略这种无家可归的结束标签,但验证器必须报告它们。
The minimal change is to remove the </p>
tag. Whether this is adequate depends on what you want. Removing the <p>
tag as well would remove the p
element, and this would affect rendering. Even though the p
element has no content rendered (content height is 0), it has default top and bottom margin, possible creating some empty vertical space.
最小的变化是删除</p>
标签。这是否足够取决于你想要什么。删除<p>
标签也会删除p
元素,这会影响渲染。即使p
元素没有渲染内容(内容高度为 0),它也有默认的顶部和底部边距,可能会创建一些空白的垂直空间。
If you do not want such space, just remove the <p>
tag (along with </p>
of course). If you do want some space, it is usually still best to remove the tag, but then you would additionally set, in CSS, some suitable margin-top
value on the top-level div
element.
如果您不想要这样的空间,只需删除<p>
标签(</p>
当然还有)。如果您确实需要一些空间,通常仍然最好删除标记,但是您将另外在 CSS 中margin-top
在顶级div
元素上设置一些合适的值。
Even though p
elements containing only whitespace are allowed in HTML5, they are not recommended. This is part of the general recommendation related to so-called palpable content: “elements whose content model allows any flow content or phrasing content should have at least one node in its contents that is palpable content”. And text is usually palpable content, but not if it consists of whitespace only.
尽管p
HTML5 中允许仅包含空格的元素,但不推荐使用它们。这是与所谓的可触知内容相关的一般建议的一部分:“内容模型允许任何流内容或短语内容的元素应在其内容中至少有一个节点是可触知的内容”。文本通常是可触知的内容,但如果它仅由空格组成,则不是。
回答by probablyup
You can't semantically put a div inside a <p>
tag.
您不能在语义上将 div 放在<p>
标签中。