<预>标签使浏览器关闭段落

发布于 2024-09-14 23:24:50 字数 955 浏览 2 评论 0原文

我对下面的 HTML 有疑问:

<html>
  <body>
    <p style="font-size: large"> 
        Some paragraph text 
        <span><pre style="display:inline">some span text</pre></span> 
        additional paragraph text that continues ...
    </p>

  </body>
</html>

所以这只是一个中间包含一些预格式化文本的段落。我遇到的问题是 Opera 和 Chrome 都没有按照我期望的方式显示。具体来说,他们关闭跨度之前的段落标签并强制换行。什么?!

使用 chromes HTML 检查选项,它显示

标记正在关闭,并插入了一个空的 ,而不是封闭 ><前>。如果删除了跨度,chrome 仍然会关闭

标签,强制换行。

我需要显示以下标签结构,而不强制显示任何换行符

< /p>。这是否可能,或者还有其他选择或解决方法吗?

编辑: 我被锁定为

 标签,因为它是 WordPress 语法突出显示插件的一部分。

笔记: 我认为最好的建议是验证您的 HTML。我遇到的很多问题都是由于无效的 HTML 造成的,某些浏览器可以很好地处理这些无效的 HTML,而其他浏览器则不能很好地处理这些问题。验证意味着您可以从头开始工作。

I'm having an issue with the HTML below:

<html>
  <body>
    <p style="font-size: large"> 
        Some paragraph text 
        <span><pre style="display:inline">some span text</pre></span> 
        additional paragraph text that continues ...
    </p>

  </body>
</html>

So this is just a paragraph that contains some preformatted text in the middle. The problem that I am having is that both Opera and Chrome don't display this the way I expect. Specifically they close the paragraph tag before the span and force a new line. WTH?!

Using chromes HTML inspection options it shows that the <p> tag is being closed and an empty <span></span> inserted instead of enclosing the <pre>. If the span is removed chrome still closes the <p> tag, forcing a newline.

I need to have the following tag structure display without any newlines being forced <p><span><pre><code></code></pre></span></p>. Is this at all possible or is there another option or workaround?

EDIT:
I'm locked into having the <pre> tag as it is part of a syntax highlighting plugin for wordpress.

NOTE:
I think the best piece of advice here is to validate your HTML. A lot of the problems I was having was because of invalid HTML that was being handled gracefully by some browsers and not gracefully by others. Validating means you have a clean slate to work from.

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

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

发布评论

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

评论(3

绾颜 2024-09-21 23:24:50

完全去掉 pre 标签,只给你的 span style="white-space:pre" 。有关其他空白此页面 > 选项。

 基本上是在说 
;你想要的是

Get rid of the pre tag altogether, and just give your span style="white-space:pre". See this page for a description of other white-space options.

<pre> is basically saying <div style="white-space:pre">; what you want is <span style="white-space:pre">.

迟月 2024-09-21 23:24:50

段落标签不能包含块级元素:

http:// www.w3.org/TR/html401/struct/text.html#h-9.3.1

pre 是块级元素。

如果您希望在内联元素中具有类似 pre 的行为,您可以在 Span 上设置 CSS 样式 white-space: pre

Paragraph tags cannot contain block-level elements:

http://www.w3.org/TR/html401/struct/text.html#h-9.3.1

pre is a block level element.

You could instead set the CSS style white-space: pre on your span if you desired to have pre-like behavior in an inline element.

咋地 2024-09-21 23:24:50

pre 是一个块级元素,如果内存正常,则不允许它位于 span 内。

您可以通过验证 HTML 来找出答案。

pre is a block level element, it isn't allowed to be inside a span if memory serves right.

You can find out by validating your HTML.

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