如果包含另一个块元素,则将块元素更改为与CSS的内联元素是错误的吗?

发布于 2025-01-22 13:51:34 字数 554 浏览 4 评论 0原文

我知道在内联元素中放置一个块元素是错误的,但是以下内容呢?

想象一下此有效的标记:

<div><p>This is a paragraph</p></div>

现在添加此CSS:

div {
   display:inline;
}

这会创建一个内联元素包含块元素(DIV变为内联元件,默认为b block)的情况

仍然有效吗?

我们如何以及何时判断HTML是否有效 - 应用CSS规则之前或之后?

更新:从那以后我了解到,在HTML5中,将块级别元素放入链接标签中是完全有效的:

<a href="#">
      <h1>Heading</h1>
      <p>Paragraph.</p>
</a>

如果您希望大量的HTML成为链接,那么这实际上真的很有用。

I know it's wrong to put a block element inside an inline element, but what about the following?

Imagine this valid markup:

<div><p>This is a paragraph</p></div>

Now add this CSS:

div {
   display:inline;
}

This creates a situation where an inline element contains a block element (The div becomes inline and the p is block by default)

Are the page elements still valid?

How and when do we judge if the HTML is valid - before or after the CSS rules are applied?

UPDATE: I've since learned that in HTML5 it is perfectly valid to put block level elements inside link tags eg:

<a href="#">
      <h1>Heading</h1>
      <p>Paragraph.</p>
</a>

This is actually really useful if you want a large block of HTML to be a link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

云淡月浅 2025-01-29 13:51:34

来自 css 2.1 Spec

当一个内联框包含一个流量内块级框时,围绕块级框(以及任何连续或连续的块级兄弟姐妹,inline框(及其同一行框中的直列祖先)都会损坏仅通过可折叠的空格和/或流量外元素隔开),将内联框分为两个框(即使任一侧为空),一个在块级别的框的每一侧(ES)。休息前和休息后的线框被包装在匿名块框中,并且块级框成为这些匿名盒子的兄弟姐妹。当这样的直列框受相对定位的影响时,任何结果翻译也会影响内联框中包含的块级框。

如果以下规则,此模型将在以下示例中适用:

  p {display:inline}
跨度{显示:块}
 

与此HTML文档一起使用:

 &lt;!doctype html public' -  // w3c // dtd html 4.01 // en&gt;
&lt; head&gt;
  &lt; title&gt;匿名文本被块&lt;/title&gt;
&lt;/head&gt;
  &lt;身体&gt;
    &lt; p&gt;
      这是跨度之前的匿名文本。
      &lt; span&gt;这是跨度的内容。&lt;/span&gt;
      这是跨度之后的匿名文本。
    &lt;/p&gt;
  &lt;/body&gt;
 

P元素包含匿名文本的块(C1),然后是块级元素,然后是另一个匿名文本的块(C2)。最终的框将是一个代表身体的块框,该框在C1周围包含一个匿名块框,跨度块框和C2周围的另一个匿名块框。

匿名框的属性是从封闭的非匿名框继承的(例如,在“匿名块框”的小节标题下方的示例中,一个用于DIV)。非属性属性具有其初始值。例如,匿名框的字体是从DIV继承的,但边距为0。

设置的属性会导致生成匿名块框的元素,仍然适用于该元素的框和内容。例如,如果在上述示例中的P元素上设置了一个边框,则将在C1周围绘制边框(在线的末端打开)和C2(在行开始时打开)。

某些用户代理在嵌入式上实现了边界,其中包含其他方式,例如,通过将这些嵌套块包裹在“匿名线框”中,从而在此类框中绘制内线边框。由于CSS1和CSS2没有定义此行为,因此仅CSS1和仅CSS2的用户代理可以实现此替代模型,并且仍然声称符合CSS 2.1的这一部分。这本规范发布后不适用于开发的UAS。

将您所要做的。显然,该行为是在CSS中指定的,尽管它涵盖了所有案例,还是在当今浏览器中始终如一地实施。

From the CSS 2.1 Spec:

When an inline box contains an in-flow block-level box, the inline box (and its inline ancestors within the same line box) are broken around the block-level box (and any block-level siblings that are consecutive or separated only by collapsible whitespace and/or out-of-flow elements), splitting the inline box into two boxes (even if either side is empty), one on each side of the block-level box(es). The line boxes before the break and after the break are enclosed in anonymous block boxes, and the block-level box becomes a sibling of those anonymous boxes. When such an inline box is affected by relative positioning, any resulting translation also affects the block-level box contained in the inline box.

This model would apply in the following example if the following rules:

p    { display: inline }
span { display: block }

were used with this HTML document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HEAD>
  <TITLE>Anonymous text interrupted by a block</TITLE>
</HEAD>
  <BODY>
    <P>
      This is anonymous text before the SPAN.
      <SPAN>This is the content of SPAN.</SPAN>
      This is anonymous text after the SPAN.
    </P>
  </BODY>

The P element contains a chunk (C1) of anonymous text followed by a block-level element followed by another chunk (C2) of anonymous text. The resulting boxes would be a block box representing the BODY, containing an anonymous block box around C1, the SPAN block box, and another anonymous block box around C2.

The properties of anonymous boxes are inherited from the enclosing non-anonymous box (e.g., in the example just below the subsection heading "Anonymous block boxes", the one for DIV). Non-inherited properties have their initial value. For example, the font of the anonymous box is inherited from the DIV, but the margins will be 0.

Properties set on elements that cause anonymous block boxes to be generated still apply to the boxes and content of that element. For example, if a border had been set on the P element in the above example, the border would be drawn around C1 (open at the end of the line) and C2 (open at the start of the line).

Some user agents have implemented borders on inlines containing blocks in other ways, e.g., by wrapping such nested blocks inside "anonymous line boxes" and thus drawing inline borders around such boxes. As CSS1 and CSS2 did not define this behavior, CSS1-only and CSS2-only user agents may implement this alternative model and still claim conformance to this part of CSS 2.1. This does not apply to UAs developed after this specification was released.

Make of that what you will. Clearly the behaviour is specified in CSS, although whether it covers all cases, or is implemented consistently across today's browsers is unclear.

错爱 2025-01-29 13:51:34

无论是否有效,元素结构都是错误的。您不将块元素放入内联元素的原因是,浏览器可以易于预测的方式呈现元素。

即使它没有打破HTML或CSS的任何规则,它仍然会创建无法按预期渲染的元素。浏览器必须处理元素,就像HTML代码无效一样。

Regardless if it's valid or not, the element structure is wrong. The reason that you don't put block elements inside inline elements is so that the browser can render the elements in an easily predictable way.

Even if it doesn't break any rules for either HTML or CSS, still it creates elements that can't be rendered as intended. The browser has to handle the elements just as if the HTML code was invalid.

沧笙踏歌 2025-01-29 13:51:34

HTML和CSS仍将有效。理想情况下,您不必做这样的事情,但是CSS实际上是一种方便的(句法有效但在语义上是有效的),可以使Internet Explorer的双重差距bug获取而不诉诸于有条件的样式表或黑客无效您的CSS。 (x)HTML比CSS具有更多的语义值,因此CSS在语义上有效并不重要。在我看来,这是可以接受的,因为它可以解决令人讨厌的浏览器问题而不会使您的代码无效。

The HTML and the CSS will both still be valid. Ideally, you wouldn't have to do something like this, but that particular bit of CSS is actually a handy (and syntactically valid but not semantically valid) way for getting Internet Explorer's double margin bug without resorting to conditional stylesheets or hacks that will invalidate your CSS. The (X)HTML has more semantic value than the CSS, so it's less important that the CSS is semantically valid. In my mind, it's acceptable because it solves an annoying browser issue without invalidating your code.

ま柒月 2025-01-29 13:51:34

HTML独立于CSS验证,因此页面仍然有效。我很确定CSS规格也没有说什么,但不要引用我的话。但是,使用这样的技术,我会非常谨慎,因为它可能会像某些浏览器中呈现,但您需要测试所有内容 - 我看不到做出很多保证。

The HTML is validated independently of the CSS, so the page would still be valid. I'm fairly sure that the CSS spec says nothing about it either, but don't quote me on that one. However, I'd be very careful using a technique like this, as while it might render as intended in some browsers, you'd need to test 'em all—I don't see many guarantees being made.

ζ澈沫 2025-01-29 13:51:34

页面元素仍然有效吗?

从HTML意义上说,“有效”,是的; HTML对CSS一无所知。

但是,您在浏览器中获得的渲染是通过CSS规范“不确定的”,因此看起来完全可以。尽管您可以在针对一个特定浏览器的CSS黑客中包含这样的规则(您知道该浏览器是如何呈现此情况的),但不应将其用于浏览器。

Are the page elements still valid?

“Valid” in an HTML sense, yes; HTML knows nothing about CSS.

The rendering you get in the browser, however, is ‘undefined’ by the CSS specification, so it could look like anything at all. Whilst you could include such a rule in CSS hacks aimed at one particular browser (where you know how that browser renders this case), it shouldn't be served to browsers in general.

夏天碎花小短裙 2025-01-29 13:51:34

我不知道这是否验证了任何规则,但我建议使用 w3c html valiorator 和 w3c css验证器以确定这一点。希望这很有帮助!

I don't know off the top of my head if this validates any rules but I would recommend using the W3C HTML Validator and the W3C CSS Validator to determine that. Hope this is helpful!

﹏半生如梦愿梦如真 2025-01-29 13:51:34

如果您遵循逻辑,并且您最终会这样实现,那不是错误的。工作的事情不是“错误”,只是因为它们很奇怪。是的,这是非常不寻常的,但是有帮助,这不是一个错误。这是故意的。 HTML和CSS应该为您服务,而不是相反,因此不要听评论告诉您不要仅仅因为丑陋而这样做。

通常,将解决方案称为“无效”,并在街区周围有很长的路要走。有时您可以重新考虑自己的所作所为。但是您所做的事情可能有很多原因,他们不考虑它们。

我确实会定期使用内线内部的块。它是有效的并且在工作 - 在大多数情况下,它不是必需的。所以呢。请记住,当XHTML告诉我们始终在属性周围列出报价时(如果您不这样做的话,每个人都对您大喊!),现在HTML5允许在内部没有空间的情况下省略它们。在奇异标签之后,最后一击怎么了? “&lt; br /&gt;” ?快点。标准改变。但是浏览器也一直支持非标准的事情。中心被弃用;我们在2013年,仍然有效。垂直中心的表?有时这是唯一的方法。在A内部div以使其按照您的计划悬停?只是继续。

专注于重要的事情。

If there is a logic you follow and you end up implementing it like this, it's NOT WRONG. Working things are not "wrong" just because they're weird. Yes, it's quite unusual but it HELPS and it's not a mistake. It's intentional. HTML and CSS should serve you, not the other way around so don't ever listen to comments telling you not to do it just because it's ugly.

It's typical to call a solution "invalid" and suggest a long way around the block. Sometimes you can rethink what you did. But there can be many reasons for what you did and they don't consider them.

I do use blocks inside inlines regularly. It's valid and it's working - it's just not necessary in most cases. So what. Remember when XHTML told us to always put quotes around properties (and everyone yelled at you if you didn't!), now HTML5 allows to omit them if there's no space inside. What happened to that last slash after singular tags? "<br />" ? Come on. Standards change. But browsers keep supporting non-standard things as well. CENTER is deprecated; we're in 2013 and it still works. TABLE for vertical centering? Sometimes it's the only way. DIV inside A to make it hover as you planned? Just go ahead.

Focus on important things.

没有你我更好 2025-01-29 13:51:34

我认为(x)HTML有效,CSS是有效的。结果有效吗?是的,如果它根据需要在浏览器中查看。

I think, (x)html is valid, css is valid. Is the result valid? Yes, if it is looking in the browser as You want.

太阳公公是暖光 2025-01-29 13:51:34

不,这不是一个错误的选择。我们可以根据要求使用。

No, It is not a wrong choice. We can use as per requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文