简体   繁体   English

如何在一个内联元素中放置一个块级元素?

[英]How to put a block-level element inside an inline element?

In part of my code I put <DIV> element inside <SPAN> . 在我的代码的一部分中,我将<DIV>元素放在<SPAN> With CSS customisation it works without problem, but page is not passing W3 validation. 通过CSS定制,它可以正常工作,但是页面未通过W3验证。

Code looks like this: 代码如下:

<span class="spanclass">
    <div class="divclass">Content</div>
</span>

After validation I got this error: 验证后,我收到此错误:

The mentioned element is not allowed to appear in the context in which you've placed it; 所提到的元素不允许出现在放置它的上下文中。 the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. 提到的其他元素是唯一允许在此使用并且可以包含提到的元素的元素。 This might mean that you need a containing element, or possibly that you've forgotten to close a previous element. 这可能意味着您需要一个包含元素,或者可能您已忘记关闭上一个元素。 One possible cause for this message is that you have attempted to put a block-level element (such as " <p> " or " <table> ") inside an inline element (such as " <a> " <span> ", or " <font> "). 出现此消息的可能原因是,您试图将一个块级元素(例如“ <p> ”或“ <table> ”)放入一个内联元素(例如“ <a><span> “)中,或“ <font> ”)。

I know this can be fixed easy if I'll put <div> instead of <span> , but I'm interested in another solution: 我知道如果我将<div>而不是<span>可以轻松解决,但是我对另一种解决方案感兴趣:

How to force validator with DTD declaration, to allow block-level element in inline element? 如何强制使用DTD声明的验证器,以允许在行内元素中使用块级元素?

You already know that it's invalid HTML. 您已经知道它是无效的HTML。 So, to conform to the standard practice and still achieve what you want reuse your spanclass CSS to define a new CSS class as 因此,为了符合标准惯例并仍然实现您想要的功能,请重用您的spanclass CSS将新的CSS类定义为

spanclass, divlikespanclass {
  /* span css data */
}

and change your HTML to 并将HTML更改为

<div class="divlikespanclass">
    <div class="divclass">Content</div>
</div>

or, simply 或者,简单地

<div class="divclass divlikespanclass">Content</div>

EDIT : (in response to OP's comments; not recommended though) 编辑 :(响应OP的评论;尽管不推荐)

Re-declare <SPAN> as <SPAN>重新声明为

<!ELEMENT SPAN - - ((%inline;) | DIV)*  -- generic language/style container -->

or, to apply to all inline elements change 或者,应用于所有内联元素更改

<!ENTITY % inline "#PCDATA | DIV | %fontstyle; | %phrase; | %special; | %formctrl;">

You can't (or shouldn't) since it's against spec. 您不能(或不应该),因为它违反规范。

A span can only hold "phrasing content". 一个跨度只能保存“短语内容”。 Basically this comes down to: <abbr>, <audio>, <b>, <bdo>, <br>, <button>, <canvas>, <cite>, <code>, <command>, <datalist>, <dfn>, <em>, <embed>, <i>, <iframe>, <img>, <input>, <kbd>, <keygen>, <label>, <mark>, <math>, <meter>, <noscript>, <object>, <output>, <progress>, <q>, <ruby>, <samp>, <script>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <svg>, <textarea>, <time>, <var>, <video>, <wbr> or plain text . 基本上可以归结为: <abbr>, <audio>, <b>, <bdo>, <br>, <button>, <canvas>, <cite>, <code>, <command>, <datalist>, <dfn>, <em>, <embed>, <i>, <iframe>, <img>, <input>, <kbd>, <keygen>, <label>, <mark>, <math>, <meter>, <noscript>, <object>, <output>, <progress>, <q>, <ruby>, <samp>, <script>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <svg>, <textarea>, <time>, <var>, <video>, <wbr> or plain text See https://developer.mozilla.org/en-US/docs/Web/HTML/Content_categories#Phrasing_content for more info 有关更多信息,请参见https://developer.mozilla.org/en-US/docs/Web/HTML/Content_categories#Phrasing_content

You (and most of the answers here) appear to be conflating three quite distinct concepts: validity, validity against the HTML (or XHTML) DTD, and conformance to the HTML (or XHTML) specification. 您(以及此处的大多数答案)似乎正在混淆三个截然不同的概念:有效性,针对HTML(或XHTML)DTD的有效性以及与HTML(或XHTML)规范的一致性。

  • A document is valid against a particular DTD if and only if it conforms to the rules expressed in that DTD. 当且仅当文件符合该DTD中表达的规则时,该文件才对特定DTD有效。

    You can make any document valid (in some absolute sense) by supplying a DTD against which the document is valid. 您可以通过提供对文档有效的DTD来使任何文档有效(从某种绝对意义上来说)。 This allows the data to pass mindless and meaningless tests which require that XML be valid at some particular point in its life cycle, without specifying what schema it is to be valid against. 这使数据可以通过无意义的测试,而这些测试要求XML在其生命周期中的某个特定点有效,而无需指定要针对哪种模式进行验证。 (You sound as if you are seeking to make the data pass such a test, though it's not clear whether this mindless and meaningless test has been imposed on you by others or by you.) (您听起来好像是在试图使数据通过这样的测试,尽管尚不清楚是否是别人或您对您进行了这种无意义而毫无意义的测试。)

    Concretely, you can cause a generic XML validator to report that your input is valid by telling the validator to use an appropriate DTD (or other schema). 具体而言,您可以通过告诉通用验证器使用适当的DTD(或其他架构)来使通用XML验证器报告您的输入有效。 If you make a modified version of the XHTML DTD, and point to it from your document, the document you describe can easily be reported as valid against that DTD. 如果您制作了XHTML DTD的修改版本,并从您的文档中指向它,则可以轻松地将您描述的文档报告为对该DTD有效。 (This will not necessarily work with the W3C validation service for HTML, because that service is not a generic DTD-based validator and it uses special rules for HTML input.) (这不一定与用于HTML的W3C验证服务一起使用,因为该服务不是基于DTD的通用验证器,并且它对HTML输入使用特殊规则。)

  • A document is valid against (a given flavor of) the (X)HTML DTD if and only if it conforms to the rules expressed in that (flavor of that) DTD. 当且仅当文档符合该DTD(该DFT的风味)中所表达的规则时,该文档才针对(X)HTML DTD(该给定的风味)有效。

    You cannot cause an HTML document which embeds div elements inside span elements to be valid against that DTD, because as it is defined that DTD forbids such embeddings. 您不能使在span元素内嵌入div元素的HTML文档对该DTD有效,因为定义为DTD禁止此类嵌入。 Modifying the DTD to make it accept such embeddings has the natural result of producing a new and different DTD, which is no longer the (X)HTML DTD. 修改DTD以使其接受此类嵌入具有自然的结果,即产生了一个新的且不同的DTD,该DTD不再是(X)HTML DTD。

  • A document conforms to the (X)HTML spec if and only if it meets the conformance requirements for documents laid out in that specification. 当且仅当文档满足该规范中列出的文档的符合性要求时,该文档才符合(X)HTML规范。

    It is unlikely that you can cause a document which embeds div elements within span elements to be regarded as conformant. 不可能将在span元素中嵌入div元素的文档视为一致。 A conclusive demonstration would require a lot more time with the HTML spec than I am inclined to spend this morning. 一个结论性的演示将需要更多的HTML规范时间,而不是我今天上午打算花费的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM