Html 如何将 DIV 放入 P 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5441639/
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
How can I put DIV in P?
提问by János
How can I put div in paragraph? I changed the display to inline, but the browser divide the base paragraph to two element, and div will not member of paragraph.
如何将 div 放在段落中?我将显示更改为内联,但浏览器将基本段落划分为两个元素,而 div 不会成为段落的成员。
回答by Gergely Fehérvári
form the w3 site:
形成 w3 站点:
The Pelement represents a paragraph. It cannot contain block-levelelements (including P itself).
回答by clairesuzy
no you cannot nest anything other than an inline element in a <p>
if you want the code to validate
不,<p>
如果您希望代码验证,则不能在 a 中嵌套除内联元素以外的任何内容
from the property def:
<!ELEMENT P - O (%inline;)* -- paragraph -->
<!ELEMENT P - O (%inline;)* -- paragraph -->
I know it's hard to understand those recs at times, but the bit in brackets means the <p>
can only contain inline
elements
我知道有时很难理解这些 recs,但括号中的位表示<p>
只能包含inline
元素
you can (and should) use a <span>
inside a <p>
and if required you could change it's CSS display property to block
or inline-block
and that would be perfectly valid, as CSS properties do not change the actual definitions of an element.. in your case it sounds like you need an inline element so just use a <span>
您可以(并且应该)使用<span>
inside a <p>
,如果需要,您可以将其 CSS 显示属性更改为block
orinline-block
这将是完全有效的,因为 CSS 属性不会更改元素的实际定义.. 在您的情况下,这听起来像您需要一个内联元素,所以只需使用<span>
回答by Julian
Make a span, set it's style to a block.
制作一个跨度,将它的样式设置为一个块。
<p>
paragraph text
<span style="display: block;">Span stuff</span>
</p>
回答by You
You cannot nest a <div>
element inside a <p>
element according to the HTML standard. Consider why you even want to do this; it should neverbe necessary. A <p>
element can only, and logically should only contain inline elements and text.
根据 HTML 标准,您不能在<div>
元素内嵌套元素<p>
。想想你为什么要这样做;它应该永远是必要的。一个<p>
元素只能,并且在逻辑上应该只包含内联元素和文本。
回答by Alohci
For those who think that validation is decisive, there are in fact two ways to get a div
inside a p
.
对于那些认为验证是决定性的,实际上有两种方法可以div
在p
.
One way is to use dom manipulation in script. For example
一种方法是在脚本中使用 dom 操作。例如
var theDiv = document.createElement("div");
theDiv.appendChild(document.createTextNode("inserted"));
var theP = document.getElementsByTagName("p")[0];
theP.insertBefore(theDiv, theP.firstChild);
See it in action here: http://www.alohci.net/text/html/div-in-p-by-script.htm.ashx
在这里查看它的实际效果:http: //www.alohci.net/text/html/div-in-p-by-script.htm.ashx
The other way is to use XHTML and serve it with an XML content type. (No support in IE prior to IE9)
另一种方法是使用 XHTML 并使用 XML 内容类型为其提供服务。(IE9 之前的 IE 不支持)
See that here : http://www.alohci.net/application/xhtml+xml/div-in-p-by-mime.htm.ashx
在这里看到:http: //www.alohci.net/application/xhtml+xml/div-in-p-by-mime.htm.ashx
(Do note however, that while it is possiblethis way - it is still not valid.)
(但是请注意,虽然这种方式是可能的- 它仍然无效。)
But Youmakes the vital point. Semantically, it's nonsense. You wouldn't put a block of something in the middle of a paragraph if you were writing text on to paper, so there should be no need to do it in HTML either.
但你提出了要点。从语义上讲,这是无稽之谈。如果您在纸上书写文本,则不会在段落中间放置一段内容,因此也无需在 HTML 中进行。
回答by danidacar
Div is a block. Span is inline. Both of them are containers. You have to set display:inline for the div with css. Of course it won't pass validation so better use span ( inline ) to achieve the same thing.
Div 是一个块。跨度是内联的。两者都是容器。您必须使用 css 为 div 设置 display:inline。当然它不会通过验证,所以更好地使用 span ( inline ) 来实现同样的事情。